Skip to content

Commit 4545287

Browse files
committed
[dev.go2go] go/types: don't panic if receiver type arguments cannot be inferred
This can happen due to earlier errors. This change avoids the panic but reports a follow-up error. Not ideal, but good enough for now. Fixes #40056. Change-Id: I500d4fc2b058cdc70f28883ff8d004df4b43fe4e Reviewed-on: https://go-review.googlesource.com/c/go/+/241130 Reviewed-by: Robert Griesemer <[email protected]>
1 parent 17564d8 commit 4545287

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/go/types/call.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
522522
// The method may have a pointer receiver, but the actually provided receiver
523523
// may be a (hopefully addressable) non-pointer value, or vice versa. Here we
524524
// only care about inferring receiver type parameters; to make the inferrence
525-
// work, match up pointer-ness of reveiver and argument.
525+
// work, match up pointer-ness of receiver and argument.
526526
arg := x
527527
if ptrRecv := isPointer(sig.recv.typ); ptrRecv != isPointer(arg.typ) {
528528
copy := *arg
@@ -533,12 +533,13 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
533533
}
534534
arg = &copy
535535
}
536-
targs := check.infer(sig.recv.pos, sig.rparams, NewTuple(sig.recv), []*operand{arg})
536+
targs := check.infer(sig.rparams[0].Pos(), sig.rparams, NewTuple(sig.recv), []*operand{arg})
537537
//check.dump("### inferred targs = %s", targs)
538-
if len(targs) == 0 {
539-
// TODO(gri) Provide explanation as to why we can't possibly
540-
// reach here (consider invalid receivers, etc.).
541-
panic("internal error: receiver type parameter inference failed")
538+
if targs == nil {
539+
// We may reach here if there were other errors (see issue #40056).
540+
// check.infer will report a follow-up error.
541+
// TODO(gri) avoid the follow-up error or provide better explanation.
542+
goto Error
542543
}
543544
// Don't modify m. Instead - for now - make a copy of m and use that instead.
544545
// (If we modify m, some tests will fail; possibly because the m is in use.)

src/go/types/fixedbugs/issue40056.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+
func _() {
8+
NewS() /* ERROR cannot infer T */ .M()
9+
}
10+
11+
type S struct {}
12+
13+
func NewS(type T)() *S
14+
15+
func (_ *S /* ERROR S is not a generic type */ (T /* ERROR cannot infer T */ )) M()

0 commit comments

Comments
 (0)