]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: disallow multiple blank type parameters
authorRobert Griesemer <gri@golang.org>
Thu, 6 Jan 2022 22:10:45 +0000 (14:10 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 7 Jan 2022 00:02:57 +0000 (00:02 +0000)
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>

src/cmd/compile/internal/types2/decl.go
src/go/types/decl.go
test/typeparam/issue50481.go [new file with mode: 0644]

index d61d2a8b0d98ad99a409b56d5afc8af30aba62ce..69388f78be37248f6ac99fce5180ecec39c4d9e4 100644 (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
index 02af0d5f3ea2113978bb8aa3efeb0699d1a52f75..bbd3f04b7e9a9093deb52e3a68c14ad6bea8b14c 100644 (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
diff --git a/test/typeparam/issue50481.go b/test/typeparam/issue50481.go
new file mode 100644 (file)
index 0000000..22d61ee
--- /dev/null
@@ -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