go/types, types2: disallow multiple blank type parameters

Work-around for #50481: report an error for multiple
blank type parameters. It's always possible to use
non-blank names in those cases.

We expect to lift this restriction for 1.19.

For #50481.

Change-Id: Ifdd2d91340aac1da3387f7d80d46e44f5997c2a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/376058
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2022-01-06 14:10:45 -08:00
parent 042548b1fd
commit c295137ad8
3 changed files with 45 additions and 0 deletions

View File

@ -632,8 +632,19 @@ func (check *Checker) collectTypeParams(dst **TypeParamList, list []*syntax.Fiel
// Declare type parameters up-front.
// The scope of type parameters starts at the beginning of the type parameter
// list (so we can have mutually recursive parameterized type bounds).
nblanks := 0
for i, f := range list {
tparams[i] = check.declareTypeParam(f.Name)
// Issue #50481: For now, disallow multiple blank type parameters because
// it causes problems with export data. Report an error unless we are in
// testing mode ("assert" is defined).
// We expect to lift this restriction for Go 1.19.
if f.Name.Value == "_" {
nblanks++
if nblanks == 2 && Universe.Lookup("assert") == nil {
check.softErrorf(f, "cannot have multiple blank type parameters (temporary restriction, see issue #50481)")
}
}
}
// Set the type parameters before collecting the type constraints because

View File

@ -684,8 +684,21 @@ func (check *Checker) collectTypeParams(dst **TypeParamList, list *ast.FieldList
// Declare type parameters up-front, with empty interface as type bound.
// The scope of type parameters starts at the beginning of the type parameter
// list (so we can have mutually recursive parameterized interfaces).
nblanks := 0
for _, f := range list.List {
tparams = check.declareTypeParams(tparams, f.Names)
// Issue #50481: For now, disallow multiple blank type parameters because
// it causes problems with export data. Report an error unless we are in
// testing mode ("assert" is defined).
// We expect to lift this restriction for Go 1.19.
for _, name := range f.Names {
if name.Name == "_" {
nblanks++
if nblanks == 2 && Universe.Lookup("assert") == nil {
check.softErrorf(name, _InvalidBlank, "cannot have multiple blank type parameters (temporary restriction, see issue #50481)")
}
}
}
}
// Set the type parameters before collecting the type constraints because

View File

@ -0,0 +1,21 @@
// errorcheck
// Copyright 2022 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.
package p
type _[_ any] struct{}
type _[_, _ any] struct{} // ERROR "cannot have multiple blank type parameters"
type _[_, _, _ any] struct{} // ERROR "cannot have multiple blank type parameters"
type _[a, _, b, _, c, _ any] struct{} // ERROR "cannot have multiple blank type parameters"
func _[_ any]() {}
func _[_, _ any]() {} // ERROR "cannot have multiple blank type parameters"
func _[_, _, _ any]() {} // ERROR "cannot have multiple blank type parameters"
func _[a, _, b, _, c, _ any]() {} // ERROR "cannot have multiple blank type parameters"
type S[P1, P2 any] struct{}
func (_ S[_, _]) m() {} // this is ok