]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: embedded type cannot be a (pointer...
authorRobert Griesemer <gri@golang.org>
Mon, 26 Jul 2021 19:13:45 +0000 (12:13 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 26 Jul 2021 20:53:17 +0000 (20:53 +0000)
Change-Id: I5eb03ae349925f0799dd866e207221429bc9fb3c
Reviewed-on: https://go-review.googlesource.com/c/go/+/337353
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/struct.go
src/cmd/compile/internal/types2/testdata/check/typeparams.go2
src/cmd/compile/internal/types2/testdata/fixedbugs/issue39938.go2
test/typeparam/interfacearg.go
test/typeparam/lockable.go

index f1d82fb50c0c59f793344039781fe284fd4b6c4e..f0c27c015043975601acf2c023575e6ac0b76ad9 100644 (file)
@@ -135,7 +135,7 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) {
                        embeddedPos := pos
                        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
@@ -147,6 +147,8 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) {
                                        }
                                case *Pointer:
                                        check.error(embeddedPos, "embedded field type cannot be a pointer")
+                               case *TypeParam:
+                                       check.error(embeddedPos, "embedded field type cannot be a (pointer to a) type parameter")
                                case *Interface:
                                        if isPtr {
                                                check.error(embeddedPos, "embedded field type cannot be a pointer to an interface")
index 2755a539e527193a73ae4aac43dd24476bf283b9..54efd1485b1d5087056727738269949c5e29a35c 100644 (file)
@@ -79,11 +79,11 @@ var _ *int = new[int]()
 
 func _[T any](map[T /* ERROR invalid 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]
index e2d85e3647b3742dca2f7acaa22ba11ef8cf990b..1d194993187f4a7bcde00fd12daa25765a0597f8 100644 (file)
@@ -9,14 +9,14 @@ package main
 type I interface{}
 
 type _S[T any] struct {
-       *T
+       *T
 }
 
 // F is a non-generic function, but has a type _S[I] which is instantiated from a
 // generic type. Test that _S[I] is successfully exported.
 func F() {
        v := _S[I]{}
-       if v.T != nil {
+       if v.x != nil {
                panic(v)
        }
 }
@@ -33,9 +33,9 @@ func _F1[T interface{ M() }](t T) {
 }
 
 func F2() {
-        _F1(&S1{})
-        _F1(S2{})
-        _F1(&S2{})
+       _F1(&S1{})
+       _F1(S2{})
+       _F1(&S2{})
 }
 
 func main() {
index 3a03652cd8a5720f191f6916db58ced87f3ac991..9372c76b4db7db29728adae11cb0b5f401ad0581 100644 (file)
@@ -11,7 +11,7 @@ import "sync"
 // A Lockable is a value that may be safely simultaneously accessed
 // from multiple goroutines via the Get and Set methods.
 type Lockable[T any] struct {
-       T
+       T
        mu sync.Mutex
 }
 
@@ -19,18 +19,18 @@ type Lockable[T any] struct {
 func (l *Lockable[T]) get() T {
        l.mu.Lock()
        defer l.mu.Unlock()
-       return l.T
+       return l.x
 }
 
 // set sets the value in a Lockable.
 func (l *Lockable[T]) set(v T) {
        l.mu.Lock()
        defer l.mu.Unlock()
-       l.T = v
+       l.x = v
 }
 
 func main() {
-       sl := Lockable[string]{T: "a"}
+       sl := Lockable[string]{x: "a"}
        if got := sl.get(); got != "a" {
                panic(got)
        }
@@ -39,7 +39,7 @@ func main() {
                panic(got)
        }
 
-       il := Lockable[int]{T: 1}
+       il := Lockable[int]{x: 1}
        if got := il.get(); got != 1 {
                panic(got)
        }