]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: ensure that signature type bounds are interfaces
authorRobert Griesemer <gri@golang.org>
Wed, 5 Jan 2022 20:25:43 +0000 (12:25 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 6 Jan 2022 21:38:57 +0000 (21:38 +0000)
Do this by running verification for instantiated signatures
later, after the delayed type parameter set-up had a chance
to wrap type bounds in implicit interfaces where needed.

Fixes #50450

Change-Id: If3ff7dc0be6af14af854830bfddb81112ac575cb
Reviewed-on: https://go-review.googlesource.com/c/go/+/375737
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue50450.go2 [new file with mode: 0644]
src/go/types/call.go
src/go/types/testdata/fixedbugs/issue50450.go2 [new file with mode: 0644]

index ed8b67c6074c2d572575e04499fa01e52ca115fc..d93805e9c745e9639e2961b4b610a18e181fbff4 100644 (file)
@@ -74,17 +74,21 @@ func (check *Checker) instantiateSignature(pos syntax.Pos, typ *Signature, targs
 
        inst := check.instance(pos, typ, targs, check.bestContext(nil)).(*Signature)
        assert(len(xlist) <= len(targs))
-       tparams := typ.TypeParams().list()
-       if i, err := check.verify(pos, tparams, targs); err != nil {
-               // best position for error reporting
-               pos := pos
-               if i < len(xlist) {
-                       pos = syntax.StartPos(xlist[i])
+
+       // verify instantiation lazily (was issue #50450)
+       check.later(func() {
+               tparams := typ.TypeParams().list()
+               if i, err := check.verify(pos, tparams, targs); err != nil {
+                       // best position for error reporting
+                       pos := pos
+                       if i < len(xlist) {
+                               pos = syntax.StartPos(xlist[i])
+                       }
+                       check.softErrorf(pos, "%s", err)
+               } else {
+                       check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist)
                }
-               check.softErrorf(pos, "%s", err)
-       } else {
-               check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist)
-       }
+       })
 
        return inst
 }
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue50450.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue50450.go2
new file mode 100644 (file)
index 0000000..bae3111
--- /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 p
+
+type S struct{}
+
+func f[P S]() {}
+
+var _ = f[S]
index 4156d56d9ff8dd3c9a10b85f811321079c26454c..ec6efd2379e358e7c1984bf48f716f5c2bbd1b80 100644 (file)
@@ -75,17 +75,21 @@ func (check *Checker) instantiateSignature(pos token.Pos, typ *Signature, targs
 
        inst := check.instance(pos, typ, targs, check.bestContext(nil)).(*Signature)
        assert(len(xlist) <= len(targs))
-       tparams := typ.TypeParams().list()
-       if i, err := check.verify(pos, tparams, targs); err != nil {
-               // best position for error reporting
-               pos := pos
-               if i < len(xlist) {
-                       pos = xlist[i].Pos()
-               }
-               check.softErrorf(atPos(pos), _InvalidTypeArg, "%s", err)
-       } else {
-               check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist)
-       }
+
+       // verify instantiation lazily (was issue #50450)
+       check.later(func() {
+               tparams := typ.TypeParams().list()
+               if i, err := check.verify(pos, tparams, targs); err != nil {
+                       // best position for error reporting
+                       pos := pos
+                       if i < len(xlist) {
+                               pos = xlist[i].Pos()
+                       }
+                       check.softErrorf(atPos(pos), _InvalidTypeArg, "%s", err)
+               } else {
+                       check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist)
+               }
+       })
 
        return inst
 }
diff --git a/src/go/types/testdata/fixedbugs/issue50450.go2 b/src/go/types/testdata/fixedbugs/issue50450.go2
new file mode 100644 (file)
index 0000000..bae3111
--- /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 p
+
+type S struct{}
+
+func f[P S]() {}
+
+var _ = f[S]