]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: don't panic if targs don't match tparams when substituting
authorRobert Findley <rfindley@google.com>
Wed, 22 Sep 2021 00:20:36 +0000 (20:20 -0400)
committerRobert Findley <rfindley@google.com>
Wed, 22 Sep 2021 13:29:32 +0000 (13:29 +0000)
Invalid code may produce instances where the number of type arguments
does not match the number of type parameters. Such code will cause an
error, but should not cause a panic when substituting in those invalid
instances.

Fixes #48529

Change-Id: Ie5ff5ace55921540a7224cc5022ef9ff7649361a
Reviewed-on: https://go-review.googlesource.com/c/go/+/351337
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>

src/go/types/named.go
src/go/types/subst.go
src/go/types/testdata/fixedbugs/issue46461.go2 [new file with mode: 0644]
src/go/types/testdata/fixedbugs/issue48529.go2 [new file with mode: 0644]

index 07c837d14a44f6d694a4e323bb50093e7f2b9208..d29c67d4eb3f618f03c2dd0d5a9098d7534e1efc 100644 (file)
@@ -332,7 +332,7 @@ func (check *Checker) completeMethod(env *Environment, m *Func) {
 // TODO(rfindley): eliminate this function or give it a better name.
 func safeUnderlying(typ Type) Type {
        if t, _ := typ.(*Named); t != nil {
-               return t.resolve(nil).underlying
+               return t.underlying
        }
        return typ.Underlying()
 }
index 999099572c7539cf181c0c291673550a172ec2f1..25629dca8ac541cc7b294790aae51c6f716e28d1 100644 (file)
@@ -177,7 +177,9 @@ func (subst *subster) typ(typ Type) Type {
                }
 
                var newTArgs []Type
-               assert(t.targs.Len() == t.orig.TypeParams().Len())
+               if t.targs.Len() != t.orig.TypeParams().Len() {
+                       return Typ[Invalid] // error reported elsewhere
+               }
 
                // already instantiated
                dump(">>> %s already instantiated", t)
diff --git a/src/go/types/testdata/fixedbugs/issue46461.go2 b/src/go/types/testdata/fixedbugs/issue46461.go2
new file mode 100644 (file)
index 0000000..bfeaf3a
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2021 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 T[U interface{ M() T[U] }] int
+
+type X int
+
+func (X) M() T[X] { return 0 }
diff --git a/src/go/types/testdata/fixedbugs/issue48529.go2 b/src/go/types/testdata/fixedbugs/issue48529.go2
new file mode 100644 (file)
index 0000000..4f92dec
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2021 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 T[U interface{ M() T /* ERROR "got 2 arguments but 1 type parameters" */ [U, int] }] int
+
+type X int
+
+func (X) M() T[X] { return 0 }