]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: add missing shape check in (*Tsubster).tinter
authorDan Scales <danscales@google.com>
Thu, 27 Jan 2022 01:58:08 +0000 (17:58 -0800)
committerDan Scales <danscales@google.com>
Thu, 27 Jan 2022 05:30:27 +0000 (05:30 +0000)
Add a missing shape check in (*Tsubster).tinter when substituting on a
generic type which is an empty interface, analogous to same check in
(*Tsubster).tstruct. Empty structs/interfaces that have rparams (i.e.
are a generic type or a shape type) need to get a new type of their
rparams - they will be different even though they don't have any
fields/methods. Without this shape check, we were not correctly
completing the Token[int] type during substitution in the example in the
issue. This issue only happens for a generic type which is an empty
interface (i.e. doesn't actually use the type param, hence quite unusual).

Added the test case already created by Keith.

Fixes #50841

Change-Id: Ia985b9f52c0e87ed0647b46373e44c51cb748ba4
Reviewed-on: https://go-review.googlesource.com/c/go/+/381175
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/typecheck/subr.go
test/typeparam/issue50841.dir/a.go [new file with mode: 0644]
test/typeparam/issue50841.dir/b.go [new file with mode: 0644]
test/typeparam/issue50841.go [new file with mode: 0644]

index ac90d87f2617eb317624d0e5d46922fe0ded4195..93812ebda5c941c601c1b99e96e859c967b851cb 100644 (file)
@@ -1326,9 +1326,9 @@ func (ts *Tsubster) typ1(t *types.Type) *types.Type {
 func (ts *Tsubster) tstruct(t *types.Type, force bool) *types.Type {
        if t.NumFields() == 0 {
                if t.HasTParam() || t.HasShape() {
-                       // For an empty struct, we need to return a new type,
-                       // since it may now be fully instantiated (HasTParam
-                       // becomes false).
+                       // For an empty struct, we need to return a new type, if
+                       // substituting from a generic type or shape type, since it
+                       // will change HasTParam/HasShape flags.
                        return types.NewStruct(t.Pkg(), nil)
                }
                return t
@@ -1387,10 +1387,10 @@ func (ts *Tsubster) tstruct(t *types.Type, force bool) *types.Type {
 // tinter substitutes type params in types of the methods of an interface type.
 func (ts *Tsubster) tinter(t *types.Type, force bool) *types.Type {
        if t.Methods().Len() == 0 {
-               if t.HasTParam() {
-                       // For an empty interface, we need to return a new type,
-                       // since it may now be fully instantiated (HasTParam
-                       // becomes false).
+               if t.HasTParam() || t.HasShape() {
+                       // For an empty interface, we need to return a new type, if
+                       // substituting from a generic type or shape type, since
+                       // since it will change HasTParam/HasShape flags.
                        return types.NewInterface(t.Pkg(), nil, false)
                }
                return t
diff --git a/test/typeparam/issue50841.dir/a.go b/test/typeparam/issue50841.dir/a.go
new file mode 100644 (file)
index 0000000..37e0233
--- /dev/null
@@ -0,0 +1,22 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package a
+
+func Marshal[foobar any]() {
+       _ = NewEncoder[foobar]()
+}
+
+func NewEncoder[foobar any]() *Encoder[foobar] {
+       return nil
+}
+
+type Encoder[foobar any] struct {
+}
+
+func (e *Encoder[foobar]) EncodeToken(t Token[foobar]) {
+
+}
+
+type Token[foobar any] any
diff --git a/test/typeparam/issue50841.dir/b.go b/test/typeparam/issue50841.dir/b.go
new file mode 100644 (file)
index 0000000..f2f7022
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package b
+
+import "a"
+
+func F() {
+       a.Marshal[int]()
+}
diff --git a/test/typeparam/issue50841.go b/test/typeparam/issue50841.go
new file mode 100644 (file)
index 0000000..060a121
--- /dev/null
@@ -0,0 +1,7 @@
+// compiledir -G=3
+
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package ignored