]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: fix panic in allowVersion when pos is invalid
authorRobert Findley <rfindley@google.com>
Mon, 7 Aug 2023 19:46:34 +0000 (15:46 -0400)
committerRobert Findley <rfindley@google.com>
Mon, 7 Aug 2023 21:16:51 +0000 (21:16 +0000)
CL 515656 updated go/types to use file base as key in the posVers map,
but introduced a panic when the corresponding *token.File is nil.

Check that pos is valid before performing the lookup.

Fixes #61822

Change-Id: I1ac9d48c831a470de8439a50022ba5f59b3e0bed
Reviewed-on: https://go-review.googlesource.com/c/go/+/516738
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Robert Findley <rfindley@google.com>

src/go/types/version.go
src/internal/types/testdata/fixedbugs/issue61822.go [new file with mode: 0644]

index 2f21e98ad8fff546c46f64653695f3349cf30555..0f4d064b7444f2a9fe468ead4522ce0f045ab45d 100644 (file)
@@ -131,9 +131,10 @@ func (check *Checker) allowVersion(pkg *Package, at positioner, v version) bool
                return true
        }
 
-       // If the source file declares its Go version, use that to decide.
-       if check.posVers != nil {
-               fileStart := check.fset.File(at.Pos()).Pos(0)
+       // If the source file declares its Go version and at references a valid
+       // position, use that to decide.
+       if pos := at.Pos(); pos.IsValid() && check.posVers != nil {
+               fileStart := check.fset.File(pos).Pos(0)
                if src, ok := check.posVers[fileStart]; ok && src.major >= 1 {
                        return !src.before(v)
                }
diff --git a/src/internal/types/testdata/fixedbugs/issue61822.go b/src/internal/types/testdata/fixedbugs/issue61822.go
new file mode 100644 (file)
index 0000000..0a91ebb
--- /dev/null
@@ -0,0 +1,19 @@
+// -lang=go1.19
+
+// 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.
+
+//go:build go1.20
+
+package p
+
+type I[P any] interface {
+       ~string | ~int
+       Error() P
+}
+
+func _[P I[string]]() {
+       var x P
+       var _ error = x
+}