Skip to content

Commit eb3ac1f

Browse files
griesemerRobert Griesemer
authored and
Robert Griesemer
committed
cmd/compile/internal/types2: mention go.mod file when using undeclared any
Use the existing versionErrorf mechanism to report use of undeclared any and comparable. Also, port versionErrorf mechanism to go/types and use it in this case as well. Adjust tests as needed. For #52880. Change-Id: I6a646f16a849692ee0cb57e362d5f3d77e2c25f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/407896 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 62e1302 commit eb3ac1f

File tree

7 files changed

+33
-20
lines changed

7 files changed

+33
-20
lines changed

src/cmd/compile/internal/types2/testdata/fixedbugs/issue46090.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
package p
1010

11-
type _ comparable // ERROR undeclared
11+
type _ comparable // ERROR predeclared comparable

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
package p
1212

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

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

19-
func f[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ](x P) {
21+
func f[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR predeclared any requires go1\.18 or later */](x P) {
2022
var _ T[ /* ERROR type instantiation requires go1\.18 or later */ int]
2123
var _ (T[ /* ERROR type instantiation requires go1\.18 or later */ int])
2224
_ = T[ /* ERROR type instantiation requires go1\.18 or later */ int]{}
@@ -30,11 +32,11 @@ func (T[ /* ERROR type instantiation requires go1\.18 or later */ P]) g(x int) {
3032
}
3133

3234
type C1 interface {
33-
comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
35+
comparable // ERROR predeclared comparable requires go1\.18 or later
3436
}
3537

3638
type C2 interface {
37-
comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
39+
comparable // ERROR predeclared comparable requires go1\.18 or later
3840
int // ERROR embedding non-interface type int requires go1\.18 or later
3941
~ /* ERROR embedding interface element ~int requires go1\.18 or later */ int
4042
int /* ERROR embedding interface element int\|~string requires go1\.18 or later */ | ~string
@@ -47,12 +49,12 @@ type _ interface {
4749
}
4850

4951
type (
50-
_ comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
52+
_ comparable // ERROR predeclared comparable requires go1\.18 or later
5153
// errors for these were reported with their declaration
5254
_ C1
5355
_ C2
5456

55-
_ = comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
57+
_ = comparable // ERROR predeclared comparable requires go1\.18 or later
5658
// errors for these were reported with their declaration
5759
_ = C1
5860
_ = C2

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (check *Checker) ident(x *operand, e *syntax.Name, def *Named, wantType boo
4646
return
4747
case universeAny, universeComparable:
4848
if !check.allowVersion(check.pkg, 1, 18) {
49-
check.errorf(e, "undeclared name: %s (requires version go1.18 or later)", e.Value)
49+
check.versionErrorf(e, "go1.18", "predeclared %s", e.Value)
5050
return // avoid follow-on errors
5151
}
5252
}

src/go/types/errors.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,17 @@ func (check *Checker) softErrorf(at positioner, code errorCode, format string, a
277277
check.report(err)
278278
}
279279

280+
func (check *Checker) versionErrorf(at positioner, code errorCode, goVersion string, format string, args ...interface{}) {
281+
msg := check.sprintf(format, args...)
282+
var err *error_
283+
if compilerErrorMessages {
284+
err = newErrorf(at, code, "%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
285+
} else {
286+
err = newErrorf(at, code, "%s requires %s or later", msg, goVersion)
287+
}
288+
check.report(err)
289+
}
290+
280291
func (check *Checker) invalidAST(at positioner, format string, args ...any) {
281292
check.errorf(at, 0, "invalid AST: "+format, args...)
282293
}

src/go/types/testdata/fixedbugs/issue46090.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
package p
1010

11-
type _ comparable // ERROR undeclared
11+
type _ comparable // ERROR predeclared comparable

src/go/types/testdata/fixedbugs/issue47818.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
package p
1212

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

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

19-
func f[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ](x P) {
19+
func f[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR predeclared any requires go1\.18 or later */ ](x P) {
2020
var _ T[ /* ERROR type instantiation requires go1\.18 or later */ int]
2121
var _ (T[ /* ERROR type instantiation requires go1\.18 or later */ int])
2222
_ = T[ /* ERROR type instantiation requires go1\.18 or later */ int]{}
@@ -30,11 +30,11 @@ func (T[ /* ERROR type instantiation requires go1\.18 or later */ P]) g(x int) {
3030
}
3131

3232
type C1 interface {
33-
comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
33+
comparable // ERROR predeclared comparable requires go1\.18 or later
3434
}
3535

3636
type C2 interface {
37-
comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
37+
comparable // ERROR predeclared comparable requires go1\.18 or later
3838
int // ERROR embedding non-interface type int requires go1\.18 or later
3939
~ /* ERROR embedding interface element ~int requires go1\.18 or later */ int
4040
int /* ERROR embedding interface element int\|~string requires go1\.18 or later */ | ~string
@@ -47,12 +47,12 @@ type _ interface {
4747
}
4848

4949
type (
50-
_ comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
50+
_ comparable // ERROR predeclared comparable requires go1\.18 or later
5151
// errors for these were reported with their declaration
5252
_ C1
5353
_ C2
5454

55-
_ = comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
55+
_ = comparable // ERROR predeclared comparable requires go1\.18 or later
5656
// errors for these were reported with their declaration
5757
_ = C1
5858
_ = C2

src/go/types/typexpr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, wantType bool)
4343
return
4444
case universeAny, universeComparable:
4545
if !check.allowVersion(check.pkg, 1, 18) {
46-
check.errorf(e, _UndeclaredName, "undeclared name: %s (requires version go1.18 or later)", e.Name)
46+
check.versionErrorf(e, _UndeclaredName, "go1.18", "predeclared %s", e.Name)
4747
return // avoid follow-on errors
4848
}
4949
}

0 commit comments

Comments
 (0)