Skip to content

Commit 92644ff

Browse files
thepuddsdmitshur
authored andcommitted
[release-branch.go1.18] cmd/compile/internal/types2: more consistently print "check go.mod" if language version < 1.18
If you attempt to instantiate a generic type or func and run 'go build' with a language version < 1.18 in the 'go' directive inside the go.mod file, cmd/compile emits a friendly message that includes the suggestion to 'check go.mod': type instantiation requires go1.18 or later (-lang was set to go1.17; check go.mod) However, if the code instead only declares a generic type or func without instantiating, cmd/compile currently emits a less friendly message: type parameters require go1.18 or later With this CL, the error in that situation becomes: type parameter requires go1.18 or later (-lang was set to go1.17; check go.mod) Within cmd/compile/internal/types2, it already calls check.versionErrorf in a dozen or so places, including three existing calls to check.versionErrorf within typeset.go (e.g., for embedding a constraint interface). This CL adds two more calls to check.versionErrorf, replacing calls to check.softErrorf. Both check.versionErrorf and check.softErrorf call check.err(at, <string>, true) after massaging the string message. Fixes #51531 Change-Id: If54e179f5952b97701d1dfde4abb08101de07811 GitHub-Last-Rev: b0b7c13 GitHub-Pull-Request: #51536 Reviewed-on: https://go-review.googlesource.com/c/go/+/390578 Reviewed-by: Robert Griesemer <[email protected]> Trust: Robert Findley <[email protected]> (cherry picked from commit d3070a7) Reviewed-on: https://go-review.googlesource.com/c/go/+/390959 Trust: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 673d52b commit 92644ff

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func (check *Checker) collectObjects() {
413413

414414
case *syntax.TypeDecl:
415415
if len(s.TParamList) != 0 && !check.allowVersion(pkg, 1, 18) {
416-
check.softErrorf(s.TParamList[0], "type parameters require go1.18 or later")
416+
check.versionErrorf(s.TParamList[0], "go1.18", "type parameter")
417417
}
418418
obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Value, nil)
419419
check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, tdecl: s})
@@ -458,7 +458,7 @@ func (check *Checker) collectObjects() {
458458
check.recordDef(s.Name, obj)
459459
}
460460
if len(s.TParamList) != 0 && !check.allowVersion(pkg, 1, 18) && !hasTParamError {
461-
check.softErrorf(s.TParamList[0], "type parameters require go1.18 or later")
461+
check.versionErrorf(s.TParamList[0], "go1.18", "type parameter")
462462
}
463463
info := &declInfo{file: fileScope, fdecl: s}
464464
// Methods are not package-level objects but we still track them in the

src/cmd/compile/internal/types2/testdata/fixedbugs/issue47818.go2

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
package go1_17
1010

11-
type T[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ] struct{}
11+
type T[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ] struct{}
1212

1313
// for init (and main, but we're not in package main) we should only get one error
1414
func init[P /* ERROR func init must have no type parameters */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ]() {}
15-
func main[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ]() {}
15+
func main[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ]() {}
1616

17-
func f[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ](x P) {
17+
func f[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ](x P) {
1818
var _ T[ /* ERROR type instantiation requires go1\.18 or later */ int]
1919
var _ (T[ /* ERROR type instantiation requires go1\.18 or later */ int])
2020
_ = T[ /* ERROR type instantiation requires go1\.18 or later */ int]{}

test/fixedbugs/issue51531.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// errorcheck -G=3 -lang=go1.17
2+
3+
// Copyright 2021 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+
type empty interface{}
10+
11+
type Foo[T empty] int // ERROR "type parameter requires go1\.18 or later \(-lang was set to go1\.17; check go.mod\)"
12+
13+
func Bar[T empty]() {} // ERROR "type parameter requires go1\.18 or later \(-lang was set to go1\.17; check go.mod\)"

0 commit comments

Comments
 (0)