Skip to content

Commit c295137

Browse files
committed
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 <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Trust: Dan Scales <[email protected]> Reviewed-by: Dan Scales <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 042548b commit c295137

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/cmd/compile/internal/types2/decl.go

+11
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,19 @@ func (check *Checker) collectTypeParams(dst **TypeParamList, list []*syntax.Fiel
632632
// Declare type parameters up-front.
633633
// The scope of type parameters starts at the beginning of the type parameter
634634
// list (so we can have mutually recursive parameterized type bounds).
635+
nblanks := 0
635636
for i, f := range list {
636637
tparams[i] = check.declareTypeParam(f.Name)
638+
// Issue #50481: For now, disallow multiple blank type parameters because
639+
// it causes problems with export data. Report an error unless we are in
640+
// testing mode ("assert" is defined).
641+
// We expect to lift this restriction for Go 1.19.
642+
if f.Name.Value == "_" {
643+
nblanks++
644+
if nblanks == 2 && Universe.Lookup("assert") == nil {
645+
check.softErrorf(f, "cannot have multiple blank type parameters (temporary restriction, see issue #50481)")
646+
}
647+
}
637648
}
638649

639650
// Set the type parameters before collecting the type constraints because

src/go/types/decl.go

+13
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,21 @@ func (check *Checker) collectTypeParams(dst **TypeParamList, list *ast.FieldList
684684
// Declare type parameters up-front, with empty interface as type bound.
685685
// The scope of type parameters starts at the beginning of the type parameter
686686
// list (so we can have mutually recursive parameterized interfaces).
687+
nblanks := 0
687688
for _, f := range list.List {
688689
tparams = check.declareTypeParams(tparams, f.Names)
690+
// Issue #50481: For now, disallow multiple blank type parameters because
691+
// it causes problems with export data. Report an error unless we are in
692+
// testing mode ("assert" is defined).
693+
// We expect to lift this restriction for Go 1.19.
694+
for _, name := range f.Names {
695+
if name.Name == "_" {
696+
nblanks++
697+
if nblanks == 2 && Universe.Lookup("assert") == nil {
698+
check.softErrorf(name, _InvalidBlank, "cannot have multiple blank type parameters (temporary restriction, see issue #50481)")
699+
}
700+
}
701+
}
689702
}
690703

691704
// Set the type parameters before collecting the type constraints because

test/typeparam/issue50481.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// errorcheck
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
type _[_ any] struct{}
10+
type _[_, _ any] struct{} // ERROR "cannot have multiple blank type parameters"
11+
type _[_, _, _ any] struct{} // ERROR "cannot have multiple blank type parameters"
12+
type _[a, _, b, _, c, _ any] struct{} // ERROR "cannot have multiple blank type parameters"
13+
14+
func _[_ any]() {}
15+
func _[_, _ any]() {} // ERROR "cannot have multiple blank type parameters"
16+
func _[_, _, _ any]() {} // ERROR "cannot have multiple blank type parameters"
17+
func _[a, _, b, _, c, _ any]() {} // ERROR "cannot have multiple blank type parameters"
18+
19+
type S[P1, P2 any] struct{}
20+
21+
func (_ S[_, _]) m() {} // this is ok

0 commit comments

Comments
 (0)