cmd/compile/internal/types2: disallow lone type parameter on RHS of type declaration

We may revisit this decision in a future release. By disallowing this
for Go 1.18 we are ensuring that we don't lock in the generics design
in a place that may need to change later. (Type declarations are the
primary construct where it crucially matters what the underlying type
of a type parameter is.)

Comment out all tests that rely on this feature; add comments referring
to issue so we can find all places easily should we change our minds.

Fixes #45639.

Change-Id: I730510e4da66d3716d455a9071c7778a1e4a1152
Reviewed-on: https://go-review.googlesource.com/c/go/+/359177
Trust: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Dan Scales <danscales@google.com>
This commit is contained in:
Robert Griesemer 2021-10-27 13:55:15 -07:00
parent 79ff663754
commit a91d0b649c
20 changed files with 297 additions and 309 deletions

View file

@ -59,12 +59,13 @@ type Ints interface {
~int32 | ~int
}
type StringInt[T Ints] T
//go:noinline
func (m StringInt[T]) String() string {
return strconv.Itoa(int(m))
}
// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
// type StringInt[T Ints] T
//
// //go:noinline
// func (m StringInt[T]) String() string {
// return strconv.Itoa(int(m))
// }
type StringStruct[T Ints] struct {
f T
@ -84,22 +85,23 @@ func main() {
panic(fmt.Sprintf("got %s, want %s", got, want))
}
x2 := []StringInt[myint]{StringInt[myint](5), StringInt[myint](7), StringInt[myint](6)}
// stringify on an instantiated type, whose bound method is associated with
// the generic type StringInt[T], which maps directly to T.
got2 := stringify(x2)
want2 := []string{ "5", "7", "6" }
if !reflect.DeepEqual(got2, want2) {
panic(fmt.Sprintf("got %s, want %s", got2, want2))
}
// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
// x2 := []StringInt[myint]{StringInt[myint](5), StringInt[myint](7), StringInt[myint](6)}
//
// // stringify on an instantiated type, whose bound method is associated with
// // the generic type StringInt[T], which maps directly to T.
// got2 := stringify(x2)
// want2 := []string{"5", "7", "6"}
// if !reflect.DeepEqual(got2, want2) {
// panic(fmt.Sprintf("got %s, want %s", got2, want2))
// }
// stringify on an instantiated type, whose bound method is associated with
// the generic type StringStruct[T], which maps to a struct containing T.
x3 := []StringStruct[myint]{StringStruct[myint]{f: 11}, StringStruct[myint]{f: 10}, StringStruct[myint]{f: 9}}
got3 := stringify(x3)
want3 := []string{ "11", "10", "9" }
want3 := []string{"11", "10", "9"}
if !reflect.DeepEqual(got3, want3) {
panic(fmt.Sprintf("got %s, want %s", got3, want3))
}