-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/compile/internal/types2: accept constraint literals with elided i…
…nterfaces When collecting type parameters, wrap constraint literals of the form ~T or A|B into interfaces so the type checker doesn't have to deal with these type set expressions syntactically anywhere else but in interfaces (i.e., union types continue to appear only as embedded elements in interfaces). Since a type constraint doesn't need to be an interface anymore, we can remove the respective restriction. Instead, when accessing the constraint interface via TypeParam.iface, wrap non-interface constraints at that point and update the constraint so it happens only once. By computing the types sets of all type parameters at before the end of type-checking, we ensure that type constraints are in their final form when accessed through the API. For #48424. Change-Id: I3a47a644ad4ab20f91d93ee39fcf3214bb5a81f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/353139 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
- Loading branch information
Showing
6 changed files
with
106 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/cmd/compile/internal/types2/testdata/examples/typesets.go2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// This file shows some examples of constraint literals with elided interfaces. | ||
// These examples are permitted if proposal issue #48424 is accepted. | ||
|
||
package p | ||
|
||
// Constraint type sets of the form T, ~T, or A|B may omit the interface. | ||
type ( | ||
_[T int] struct{} | ||
_[T ~int] struct{} | ||
_[T int|string] struct{} | ||
_[T ~int|~string] struct{} | ||
) | ||
|
||
func min[T int|string](x, y T) T { | ||
if x < y { | ||
return x | ||
} | ||
return y | ||
} | ||
|
||
func lookup[M ~map[K]V, K comparable, V any](m M, k K) V { | ||
return m[k] | ||
} | ||
|
||
func deref[P ~*E, E any](p P) E { | ||
return *p | ||
} | ||
|
||
func _() int { | ||
p := new(int) | ||
return deref(p) | ||
} | ||
|
||
func addrOfCopy[V any, P ~*V](v V) P { | ||
return &v | ||
} | ||
|
||
func _() *int { | ||
return addrOfCopy(0) | ||
} | ||
|
||
// A type parameter may not be embedded in an interface; | ||
// so it can also not be used as a constraint. | ||
func _[A any, B A /* ERROR cannot use a type parameter as constraint */ ]() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters