Skip to content

Commit 35ec948

Browse files
committed
go/types,types2: fix panic in reverse type inference when -lang<go1.18
Due to reverse type inference, we may not have an index expression when type-checking a function instantiation. Fix a panic when the index expr is nil. Fixes #59639 Change-Id: Ib5de5e49cdb7b339653e4fb775bf5c5fdb3c6907 Reviewed-on: https://go-review.googlesource.com/c/go/+/484757 Reviewed-by: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]>
1 parent 7c2550b commit 35ec948

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/cmd/compile/internal/types2/call.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ func (check *Checker) funcInst(tsig *Signature, pos syntax.Pos, x *operand, inst
2424
assert(tsig != nil || inst != nil)
2525

2626
if !check.allowVersion(check.pkg, pos, 1, 18) {
27-
check.versionErrorf(inst.Pos(), "go1.18", "function instantiation")
27+
var posn poser
28+
if inst != nil {
29+
posn = inst.Pos()
30+
} else {
31+
posn = pos
32+
}
33+
check.versionErrorf(posn, "go1.18", "function instantiation")
2834
}
2935

3036
// targs and xlist are the type arguments and corresponding type expressions, or nil.

src/go/types/call.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ func (check *Checker) funcInst(tsig *Signature, pos token.Pos, x *operand, ix *t
2626
assert(tsig != nil || ix != nil)
2727

2828
if !check.allowVersion(check.pkg, pos, 1, 18) {
29-
check.softErrorf(inNode(ix.Orig, ix.Lbrack), UnsupportedFeature, "function instantiation requires go1.18 or later")
29+
var posn positioner
30+
if ix != nil {
31+
posn = inNode(ix.Orig, ix.Lbrack)
32+
} else {
33+
posn = atPos(pos)
34+
}
35+
check.softErrorf(posn, UnsupportedFeature, "function instantiation requires go1.18 or later")
3036
}
3137

3238
// targs and xlist are the type arguments and corresponding type expressions, or nil.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// -reverseTypeInference -lang=go1.17
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
func f[P /* ERROR "requires go1.18" */ interface{}](P) {}
10+
11+
var v func(int) = f /* ERROR "requires go1.18" */

0 commit comments

Comments
 (0)