From: Robert Findley Date: Mon, 7 Aug 2023 19:46:34 +0000 (-0400) Subject: go/types: fix panic in allowVersion when pos is invalid X-Git-Tag: go1.22rc1~1374 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=008ab9adb8382a274ba25c005a046b912af94809;p=gostls13.git go/types: fix panic in allowVersion when pos is invalid 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 Reviewed-by: Hyang-Ah Hana Kim Run-TryBot: Robert Findley --- diff --git a/src/go/types/version.go b/src/go/types/version.go index 2f21e98ad8..0f4d064b74 100644 --- a/src/go/types/version.go +++ b/src/go/types/version.go @@ -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 index 0000000000..0a91ebb7b2 --- /dev/null +++ b/src/internal/types/testdata/fixedbugs/issue61822.go @@ -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 +}