Skip to content

Commit 18e0503

Browse files
committed
[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 <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 8989747 commit 18e0503

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

src/go/types/methodset_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ func TestNewMethodSet(t *testing.T) {
4646

4747
genericTests := map[string][]method{
4848
// By convention, look up a in the scope of "g"
49-
"type C interface{ f() }; func g[T C](a T){}": {{"f", []int{0}, true}},
50-
"type C interface{ f() }; func g[T C]() { var a T; _ = a }": {{"f", []int{0}, true}},
51-
"type C interface{ f() }; func g[T C]() { var a struct{T}; _ = a }": {{"f", []int{0, 0}, true}},
49+
"type C interface{ f() }; func g[T C](a T){}": {{"f", []int{0}, true}},
50+
"type C interface{ f() }; func g[T C]() { var a T; _ = a }": {{"f", []int{0}, true}},
5251

53-
// Issue #45639: We don't allow this anymore. Keep this code in case we
54-
// decide to revisit this decision.
52+
// Issue #43621: We don't allow this anymore. Keep this code in case we
53+
// decide to revisit this decision.
54+
// "type C interface{ f() }; func g[T C]() { var a struct{T}; _ = a }": {{"f", []int{0, 0}, true}},
55+
56+
// Issue #45639: We also don't allow this anymore.
5557
// "type C interface{ f() }; func g[T C]() { type Y T; var a Y; _ = a }": {},
5658
}
5759

src/go/types/struct.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,23 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) {
136136

137137
check.later(func() {
138138
t, isPtr := deref(embeddedTyp)
139-
switch t := optype(t).(type) {
139+
switch t := under(t).(type) {
140140
case *Basic:
141141
if t == Typ[Invalid] {
142142
// error was reported before
143143
return
144144
}
145145
// unsafe.Pointer is treated like a regular pointer
146146
if t.kind == UnsafePointer {
147-
check.errorf(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be unsafe.Pointer")
147+
check.error(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be unsafe.Pointer")
148148
}
149149
case *Pointer:
150-
check.errorf(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a pointer")
150+
check.error(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a pointer")
151+
case *TypeParam:
152+
check.error(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a (pointer to a) type parameter")
151153
case *Interface:
152154
if isPtr {
153-
check.errorf(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a pointer to an interface")
155+
check.error(embeddedPos, _InvalidPtrEmbed, "embedded field type cannot be a pointer to an interface")
154156
}
155157
}
156158
})

src/go/types/testdata/check/typeparams.go2

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ var _ *int = new[int]()
7979

8080
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
8181

82-
func f1[T1 any](struct{T1}) int
82+
func f1[T1 any](struct{T1 /* ERROR cannot be a .* type parameter */ }) int
8383
var _ = f1[int](struct{T1}{})
8484
type T1 = int
8585

86-
func f2[t1 any](struct{t1; x float32}) int
86+
func f2[t1 any](struct{t1 /* ERROR cannot be a .* type parameter */ ; x float32}) int
8787
var _ = f2[t1](struct{t1; x float32}{})
8888
type t1 = int
8989

src/go/types/testdata/fixedbugs/issue39938.go2

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ package p
88

99
type E0[P any] P
1010
type E1[P any] *P
11-
type E2[P any] struct{ P }
12-
type E3[P any] struct{ *P }
11+
type E2[P any] struct{ _ P }
12+
type E3[P any] struct{ _ *P }
1313

1414
type T0 /* ERROR illegal cycle */ struct {
1515
_ E0[T0]

0 commit comments

Comments
 (0)