]> Cypherpunks repositories - gostls13.git/commitdiff
go/types,types2: fix panic in reverse type inference when -lang<go1.18
authorRob Findley <rfindley@google.com>
Fri, 14 Apr 2023 18:25:53 +0000 (14:25 -0400)
committerRobert Findley <rfindley@google.com>
Fri, 14 Apr 2023 18:40:36 +0000 (18:40 +0000)
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 <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>

src/cmd/compile/internal/types2/call.go
src/go/types/call.go
src/internal/types/testdata/fixedbugs/issue59639.go [new file with mode: 0644]

index 08c90b9f8f9a33ac16e9960c4f3ed574ac033796..bf561f2c872aa5decc69f6f3c33fb687a8309f96 100644 (file)
@@ -24,7 +24,13 @@ func (check *Checker) funcInst(tsig *Signature, pos syntax.Pos, x *operand, inst
        assert(tsig != nil || inst != nil)
 
        if !check.allowVersion(check.pkg, pos, 1, 18) {
-               check.versionErrorf(inst.Pos(), "go1.18", "function instantiation")
+               var posn poser
+               if inst != nil {
+                       posn = inst.Pos()
+               } else {
+                       posn = pos
+               }
+               check.versionErrorf(posn, "go1.18", "function instantiation")
        }
 
        // targs and xlist are the type arguments and corresponding type expressions, or nil.
index f220efb24056e90018200dcc811f45b367827332..854ce7e406b9fa4ecc5b7fa1ac1e42fdfed70f82 100644 (file)
@@ -26,7 +26,13 @@ func (check *Checker) funcInst(tsig *Signature, pos token.Pos, x *operand, ix *t
        assert(tsig != nil || ix != nil)
 
        if !check.allowVersion(check.pkg, pos, 1, 18) {
-               check.softErrorf(inNode(ix.Orig, ix.Lbrack), UnsupportedFeature, "function instantiation requires go1.18 or later")
+               var posn positioner
+               if ix != nil {
+                       posn = inNode(ix.Orig, ix.Lbrack)
+               } else {
+                       posn = atPos(pos)
+               }
+               check.softErrorf(posn, UnsupportedFeature, "function instantiation requires go1.18 or later")
        }
 
        // targs and xlist are the type arguments and corresponding type expressions, or nil.
diff --git a/src/internal/types/testdata/fixedbugs/issue59639.go b/src/internal/types/testdata/fixedbugs/issue59639.go
new file mode 100644 (file)
index 0000000..c82d5b1
--- /dev/null
@@ -0,0 +1,11 @@
+// -reverseTypeInference -lang=go1.17
+
+// Copyright 2023 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
+
+func f[P /* ERROR "requires go1.18" */ interface{}](P) {}
+
+var v func(int) = f /* ERROR "requires go1.18" */