mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[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:
parent
89897473e2
commit
18e0503724
4 changed files with 17 additions and 13 deletions
|
|
@ -48,10 +48,12 @@ func TestNewMethodSet(t *testing.T) {
|
||||||
// By convention, look up a in the scope of "g"
|
// 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](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 T; _ = a }": {{"f", []int{0}, true}},
|
||||||
"type C interface{ f() }; func g[T C]() { var a struct{T}; _ = a }": {{"f", []int{0, 0}, true}},
|
|
||||||
|
|
||||||
// Issue #45639: We don't allow this anymore. Keep this code in case we
|
// Issue #43621: We don't allow this anymore. Keep this code in case we
|
||||||
// decide to revisit this decision.
|
// 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 }": {},
|
// "type C interface{ f() }; func g[T C]() { type Y T; var a Y; _ = a }": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) {
|
||||||
|
|
||||||
check.later(func() {
|
check.later(func() {
|
||||||
t, isPtr := deref(embeddedTyp)
|
t, isPtr := deref(embeddedTyp)
|
||||||
switch t := optype(t).(type) {
|
switch t := under(t).(type) {
|
||||||
case *Basic:
|
case *Basic:
|
||||||
if t == Typ[Invalid] {
|
if t == Typ[Invalid] {
|
||||||
// error was reported before
|
// 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
|
// unsafe.Pointer is treated like a regular pointer
|
||||||
if t.kind == UnsafePointer {
|
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:
|
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:
|
case *Interface:
|
||||||
if isPtr {
|
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
4
src/go/types/testdata/check/typeparams.go2
vendored
4
src/go/types/testdata/check/typeparams.go2
vendored
|
|
@ -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 _[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}{})
|
var _ = f1[int](struct{T1}{})
|
||||||
type T1 = int
|
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}{})
|
var _ = f2[t1](struct{t1; x float32}{})
|
||||||
type t1 = int
|
type t1 = int
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ package p
|
||||||
|
|
||||||
type E0[P any] P
|
type E0[P any] P
|
||||||
type E1[P any] *P
|
type E1[P any] *P
|
||||||
type E2[P any] struct{ P }
|
type E2[P any] struct{ _ P }
|
||||||
type E3[P any] struct{ *P }
|
type E3[P any] struct{ _ *P }
|
||||||
|
|
||||||
type T0 /* ERROR illegal cycle */ struct {
|
type T0 /* ERROR illegal cycle */ struct {
|
||||||
_ E0[T0]
|
_ E0[T0]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue