[dev.go2go] go/types: don't crash when receiver type doesn't declare type parameters

Fixes #39664.

Change-Id: I0cf585f0c704bdaa163a7ebc9e7c82a734fcd5e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/238625
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Robert Griesemer 2020-06-17 17:32:25 -07:00
parent 57cec2f259
commit 2c4c09e4ff
3 changed files with 29 additions and 0 deletions

View File

@ -120,6 +120,11 @@ var tests = [][]string{
{"examples/functions.go2"},
{"examples/methods.go2"},
{"examples/types.go2"},
// Go 2 prototype bug fixes
// TODO(gri) Eliminate the need to enumerate these tests here.
// Should just traverse that directory.
{"fixedbugs/issue39664.go2"},
}
var fset = token.NewFileSet()

View File

@ -0,0 +1,15 @@
// Copyright 2020 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(type _) struct {}
func (T /* ERROR instantiation */ ) m()
func _() {
var x interface { m() }
x = T(int){}
_ = x
}

View File

@ -396,6 +396,15 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
// type parameters of ftyp with V's instantiation type arguments.
// This lazily instantiates the signature of method f.
if Vn != nil && len(Vn.targs) > 0 {
// Be careful: The number of type arguments may not match
// the number of receiver parameters. If so, an error was
// reported earlier but the length discrepancy is still
// here. Exit early in this case to prevent an assertion
// failure in makeSubstMap.
// TODO(gri) Can we avoid this check by fixing the lengths?
if len(ftyp.rparams) != len(Vn.targs) {
return
}
ftyp = check.subst(token.NoPos, ftyp, makeSubstMap(ftyp.rparams, Vn.targs)).(*Signature)
}