Skip to content

Commit 2c4c09e

Browse files
committed
[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 <[email protected]>
1 parent 57cec2f commit 2c4c09e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/go/types/check_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ var tests = [][]string{
120120
{"examples/functions.go2"},
121121
{"examples/methods.go2"},
122122
{"examples/types.go2"},
123+
124+
// Go 2 prototype bug fixes
125+
// TODO(gri) Eliminate the need to enumerate these tests here.
126+
// Should just traverse that directory.
127+
{"fixedbugs/issue39664.go2"},
123128
}
124129

125130
var fset = token.NewFileSet()

src/go/types/fixedbugs/issue39664.go2

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
type T(type _) struct {}
8+
9+
func (T /* ERROR instantiation */ ) m()
10+
11+
func _() {
12+
var x interface { m() }
13+
x = T(int){}
14+
_ = x
15+
}

src/go/types/lookup.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,15 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
396396
// type parameters of ftyp with V's instantiation type arguments.
397397
// This lazily instantiates the signature of method f.
398398
if Vn != nil && len(Vn.targs) > 0 {
399+
// Be careful: The number of type arguments may not match
400+
// the number of receiver parameters. If so, an error was
401+
// reported earlier but the length discrepancy is still
402+
// here. Exit early in this case to prevent an assertion
403+
// failure in makeSubstMap.
404+
// TODO(gri) Can we avoid this check by fixing the lengths?
405+
if len(ftyp.rparams) != len(Vn.targs) {
406+
return
407+
}
399408
ftyp = check.subst(token.NoPos, ftyp, makeSubstMap(ftyp.rparams, Vn.targs)).(*Signature)
400409
}
401410

0 commit comments

Comments
 (0)