Skip to content

Commit

Permalink
go/types, types2: constraints may be parenthesized and that includes …
Browse files Browse the repository at this point in the history
…"any"

Change-Id: I9a234cc1f04ca762375b51ec8ef009fb264c7ed1
Reviewed-on: https://go-review.googlesource.com/c/go/+/295689
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
griesemer committed Feb 23, 2021
1 parent 7490355 commit 0458d8c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types2/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ func (check *Checker) collectTypeParams(list []*syntax.Field) (tparams []*TypeNa
// The predeclared identifier "any" is visible only as a constraint
// in a type parameter list. Look for it before general constraint
// resolution.
if tident, _ := f.Type.(*syntax.Name); tident != nil && tident.Value == "any" && check.lookup("any") == nil {
if tident, _ := unparen(f.Type).(*syntax.Name); tident != nil && tident.Value == "any" && check.lookup("any") == nil {
bound = universeAny
} else {
bound = check.typ(f.Type)
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/compile/internal/types2/testdata/typeparams.go2
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func _[_ any](x int) int
func _[T any](T /* ERROR redeclared */ T)()
func _[T, T /* ERROR redeclared */ any]()

// Constraints (incl. any) may be parenthesized.
func _[_ (any)]() {}
func _[_ (interface{})]() {}

func reverse[T any](list []T) []T {
rlist := make([]T, len(list))
i := len(list)
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ func (check *Checker) collectTypeParams(list *ast.FieldList) (tparams []*TypeNam
// The predeclared identifier "any" is visible only as a constraint
// in a type parameter list. Look for it before general constraint
// resolution.
if tident, _ := f.Type.(*ast.Ident); tident != nil && tident.Name == "any" && check.lookup("any") == nil {
if tident, _ := unparen(f.Type).(*ast.Ident); tident != nil && tident.Name == "any" && check.lookup("any") == nil {
bound = universeAny
} else {
bound = check.typ(f.Type)
Expand Down
4 changes: 4 additions & 0 deletions src/go/types/testdata/typeparams.go2
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func _[_ any](x int) int
func _[T any](T /* ERROR redeclared */ T)()
func _[T, T /* ERROR redeclared */ any]()

// Constraints (incl. any) may be parenthesized.
func _[_ (any)]() {}
func _[_ (interface{})]() {}

func reverse[T any](list []T) []T {
rlist := make([]T, len(list))
i := len(list)
Expand Down

0 comments on commit 0458d8c

Please sign in to comment.