[dev.typeparams] go/types: embedded type cannot be a (pointer to) a type parameter

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>
This commit is contained in:
Rob Findley 2021-08-03 16:32:23 -04:00 committed by Robert Findley
parent 89897473e2
commit 18e0503724
4 changed files with 17 additions and 13 deletions

View 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 }": {},
}

View 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")
}
}
})

View 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

View 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]