]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] go/types: embedded type cannot be a (pointer to) a type parameter
authorRob Findley <rfindley@google.com>
Tue, 3 Aug 2021 20:32:23 +0000 (16:32 -0400)
committerRobert Findley <rfindley@google.com>
Wed, 4 Aug 2021 11:06:10 +0000 (11:06 +0000)
This is a port of CL 337353 to go/types, adjusted for the error API and
to comment out a test for MethodSet.

Some nearby error messages that were using errorf rather than error were
also adjusted.

Fixes #43621

Change-Id: I28c9747e044ec7a2863f6890db69475fb8c29231
Reviewed-on: https://go-review.googlesource.com/c/go/+/339651
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/methodset_test.go
src/go/types/struct.go
src/go/types/testdata/check/typeparams.go2
src/go/types/testdata/fixedbugs/issue39938.go2

index 5b29b2f0fea266567e0565ddc53689b4d128640e..73a8442f21417de6c3c3f01019551b2ae825fc0d 100644 (file)
@@ -46,12 +46,14 @@ func TestNewMethodSet(t *testing.T) {
 
        genericTests := map[string][]method{
                // By convention, look up a in the scope of "g"
-               "type C interface{ f() }; func g[T C](a T){}":                       {{"f", []int{0}, true}},
-               "type C interface{ f() }; func g[T C]() { var a T; _ = a }":         {{"f", []int{0}, true}},
-               "type C interface{ f() }; func g[T C]() { var a struct{T}; _ = a }": {{"f", []int{0, 0}, true}},
+               "type C interface{ f() }; func g[T C](a T){}":               {{"f", []int{0}, true}},
+               "type C interface{ f() }; func g[T C]() { var a T; _ = a }": {{"f", []int{0}, true}},
 
-               // Issue #45639: We don't allow this anymore. Keep this code in case we
-               //               decide to revisit this decision.
+               // Issue #43621: We don't allow this anymore. Keep this code in case we
+               // decide to revisit this decision.
+               // "type C interface{ f() }; func g[T C]() { var a struct{T}; _ = a }": {{"f", []int{0, 0}, true}},
+
+               // Issue #45639: We also don't allow this anymore.
                // "type C interface{ f() }; func g[T C]() { type Y T; var a Y; _ = a }": {},
        }
 
index d1fb813c1476ab0cfdc19ac14d1c485e2747f351..48b07346dc30cc7c3ba3038ca7b1a780bd41829d 100644 (file)
@@ -136,7 +136,7 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) {
 
                        check.later(func() {
                                t, isPtr := deref(embeddedTyp)
-                               switch t := optype(t).(type) {
+                               switch t := under(t).(type) {
                                case *Basic:
                                        if t == Typ[Invalid] {
                                                // error was reported before
@@ -144,13 +144,15 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) {
                                        }
                                        // unsafe.Pointer is treated like a regular pointer
                                        if t.kind == UnsafePointer {
-                                               check.errorf(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be unsafe.Pointer")
+                                               check.error(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be unsafe.Pointer")
                                        }
                                case *Pointer:
-                                       check.errorf(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a pointer")
+                                       check.error(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a pointer")
+                               case *TypeParam:
+                                       check.error(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a (pointer to a) type parameter")
                                case *Interface:
                                        if isPtr {
-                                               check.errorf(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a pointer to an interface")
+                                               check.error(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a pointer to an interface")
                                        }
                                }
                        })
index b03725ff2aa0b2bcb0bf5e4d4f27443c359bc524..77cd65d19a8b57b141fccfbd1bbf954d79133747 100644 (file)
@@ -79,11 +79,11 @@ var _ *int = new[int]()
 
 func _[T any](map[T /* ERROR incomparable map key type T \(missing comparable constraint\) */]int) // w/o constraint we don't know if T is comparable
 
-func f1[T1 any](struct{T1}) int
+func f1[T1 any](struct{T1 /* ERROR cannot be a .* type parameter */ }) int
 var _ = f1[int](struct{T1}{})
 type T1 = int
 
-func f2[t1 any](struct{t1; x float32}) int
+func f2[t1 any](struct{t1 /* ERROR cannot be a .* type parameter */ ; x float32}) int
 var _ = f2[t1](struct{t1; x float32}{})
 type t1 = int
 
index 76e7e369ca12bb6718272a2de41dfc7f6617d848..0da6e103fd1139b48d6d26e6d9b9ec9cee7ac5db 100644 (file)
@@ -8,8 +8,8 @@ package p
 
 type E0[P any] P
 type E1[P any] *P
-type E2[P any] struct{ P }
-type E3[P any] struct{ *P }
+type E2[P any] struct{ P }
+type E3[P any] struct{ *P }
 
 type T0 /* ERROR illegal cycle */ struct {
         _ E0[T0]