From: Robert Griesemer Date: Wed, 22 Sep 2021 20:38:05 +0000 (-0700) Subject: cmd/compile/internal/types2: don't panic if targs don't match tparams when substituting X-Git-Tag: go1.18beta1~1233 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=41bb7446dcc5179047512aa10c5e6d01eb870b54;p=gostls13.git cmd/compile/internal/types2: don't panic if targs don't match tparams when substituting This is a clean port of CL 351337 from go/types to types2. Change-Id: I974bf79fcc1ec0016c38e4c0b361d05f7b44e649 Reviewed-on: https://go-review.googlesource.com/c/go/+/351466 Trust: Robert Griesemer Run-TryBot: Robert Griesemer Reviewed-by: Robert Findley TryBot-Result: Go Bot --- diff --git a/src/cmd/compile/internal/types2/named.go b/src/cmd/compile/internal/types2/named.go index 1db993afc9..bc4d4f89c5 100644 --- a/src/cmd/compile/internal/types2/named.go +++ b/src/cmd/compile/internal/types2/named.go @@ -330,7 +330,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() } diff --git a/src/cmd/compile/internal/types2/subst.go b/src/cmd/compile/internal/types2/subst.go index ee68f22653..5e057a6f80 100644 --- a/src/cmd/compile/internal/types2/subst.go +++ b/src/cmd/compile/internal/types2/subst.go @@ -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/cmd/compile/internal/types2/testdata/fixedbugs/issue46461.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue46461.go2 new file mode 100644 index 0000000000..bfeaf3a966 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue46461.go2 @@ -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/cmd/compile/internal/types2/testdata/fixedbugs/issue48529.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48529.go2 new file mode 100644 index 0000000000..4f92dec7fe --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48529.go2 @@ -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 }