]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: mention go.mod file when using undeclared any
authorRobert Griesemer <gri@golang.org>
Tue, 24 May 2022 00:26:24 +0000 (17:26 -0700)
committerRobert Griesemer <gri@google.com>
Tue, 24 May 2022 16:05:16 +0000 (16:05 +0000)
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 <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/testdata/fixedbugs/issue46090.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue47818.go
src/cmd/compile/internal/types2/typexpr.go
src/go/types/errors.go
src/go/types/testdata/fixedbugs/issue46090.go
src/go/types/testdata/fixedbugs/issue47818.go
src/go/types/typexpr.go

index 0fb92a36571100625183be779b7e527cb427df6e..07f0101acb1ff9a55b9e179a07aa52942e852206 100644 (file)
@@ -8,4 +8,4 @@
 
 package p
 
-type _ comparable // ERROR undeclared
+type _ comparable // ERROR predeclared comparable
index 58a62092b7ccaa23c6d7c15df9d1f80ea16fbe22..5aa3b82a8d8e447d0cd31448a0f11d5e1f1ec8d6 100644 (file)
 
 package p
 
-type T[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ] struct{}
+type T[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR predeclared any requires go1\.18 or later */] struct{}
 
 // for init (and main, but we're not in package main) we should only get one error
-func init[P /* ERROR func init must have no type parameters */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ]()   {}
-func main[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ]() {}
+func init[P /* ERROR func init must have no type parameters */ any /* ERROR predeclared any requires go1\.18 or later */]() {
+}
+func main[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR predeclared any requires go1\.18 or later */]() {
+}
 
-func f[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ](x P) {
+func f[P /* ERROR type parameter requires go1\.18 or later */ any /* ERROR predeclared any requires go1\.18 or later */](x P) {
        var _ T[ /* ERROR type instantiation requires go1\.18 or later */ int]
        var _ (T[ /* ERROR type instantiation requires go1\.18 or later */ int])
        _ = 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) {
 }
 
 type C1 interface {
-       comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
+       comparable // ERROR predeclared comparable requires go1\.18 or later
 }
 
 type C2 interface {
-       comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
+       comparable // ERROR predeclared comparable requires go1\.18 or later
        int        // ERROR embedding non-interface type int requires go1\.18 or later
        ~ /* ERROR embedding interface element ~int requires go1\.18 or later */ int
        int /* ERROR embedding interface element int\|~string requires go1\.18 or later */ | ~string
@@ -47,12 +49,12 @@ type _ interface {
 }
 
 type (
-       _ comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
+       _ comparable // ERROR predeclared comparable requires go1\.18 or later
        // errors for these were reported with their declaration
        _ C1
        _ C2
 
-       _ = comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
+       _ = comparable // ERROR predeclared comparable requires go1\.18 or later
        // errors for these were reported with their declaration
        _ = C1
        _ = C2
index 8b9976da7988af917390028a55beeaf0d7d5c355..1610f8ff8f585f1523a2702740181ebacd86e8df 100644 (file)
@@ -46,7 +46,7 @@ func (check *Checker) ident(x *operand, e *syntax.Name, def *Named, wantType boo
                return
        case universeAny, universeComparable:
                if !check.allowVersion(check.pkg, 1, 18) {
-                       check.errorf(e, "undeclared name: %s (requires version go1.18 or later)", e.Value)
+                       check.versionErrorf(e, "go1.18", "predeclared %s", e.Value)
                        return // avoid follow-on errors
                }
        }
index 0dc0bc8799102910b8fe188621d33f9d0541ab86..f3cb249f5e1387189138a4fe145732f6e55f4873 100644 (file)
@@ -277,6 +277,17 @@ func (check *Checker) softErrorf(at positioner, code errorCode, format string, a
        check.report(err)
 }
 
+func (check *Checker) versionErrorf(at positioner, code errorCode, goVersion string, format string, args ...interface{}) {
+       msg := check.sprintf(format, args...)
+       var err *error_
+       if compilerErrorMessages {
+               err = newErrorf(at, code, "%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
+       } else {
+               err = newErrorf(at, code, "%s requires %s or later", msg, goVersion)
+       }
+       check.report(err)
+}
+
 func (check *Checker) invalidAST(at positioner, format string, args ...any) {
        check.errorf(at, 0, "invalid AST: "+format, args...)
 }
index 0fb92a36571100625183be779b7e527cb427df6e..07f0101acb1ff9a55b9e179a07aa52942e852206 100644 (file)
@@ -8,4 +8,4 @@
 
 package p
 
-type _ comparable // ERROR undeclared
+type _ comparable // ERROR predeclared comparable
index dbd532ac123981c71a4df3c0fa399671633e4700..2db095c243ecbacfad8e7badaa87acc4c521dffd 100644 (file)
 
 package p
 
-type T[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ] struct{}
+type T[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR predeclared any requires go1\.18 or later */ ] struct{}
 
 // for init (and main, but we're not in package main) we should only get one error
-func init[P /* ERROR func init must have no type parameters */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ]()   {}
-func main[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ]() {}
+func init[P /* ERROR func init must have no type parameters */ any /* ERROR predeclared any requires go1\.18 or later */ ]()   {}
+func main[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR predeclared any requires go1\.18 or later */ ]() {}
 
-func f[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ](x P) {
+func f[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR predeclared any requires go1\.18 or later */ ](x P) {
        var _ T[ /* ERROR type instantiation requires go1\.18 or later */ int]
        var _ (T[ /* ERROR type instantiation requires go1\.18 or later */ int])
        _ = 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) {
 }
 
 type C1 interface {
-       comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
+       comparable // ERROR predeclared comparable requires go1\.18 or later
 }
 
 type C2 interface {
-       comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
+       comparable // ERROR predeclared comparable requires go1\.18 or later
        int        // ERROR embedding non-interface type int requires go1\.18 or later
        ~ /* ERROR embedding interface element ~int requires go1\.18 or later */ int
        int /* ERROR embedding interface element int\|~string requires go1\.18 or later */ | ~string
@@ -47,12 +47,12 @@ type _ interface {
 }
 
 type (
-       _ comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
+       _ comparable // ERROR predeclared comparable requires go1\.18 or later
        // errors for these were reported with their declaration
        _ C1
        _ C2
 
-       _ = comparable // ERROR undeclared name: comparable \(requires version go1\.18 or later\)
+       _ = comparable // ERROR predeclared comparable requires go1\.18 or later
        // errors for these were reported with their declaration
        _ = C1
        _ = C2
index b704372dcf6d8835435be4ca5496a87dde0bba96..d5fe9a5cc6dca7d5fa8947769db4ff6f2e19d2c7 100644 (file)
@@ -43,7 +43,7 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, wantType bool)
                return
        case universeAny, universeComparable:
                if !check.allowVersion(check.pkg, 1, 18) {
-                       check.errorf(e, _UndeclaredName, "undeclared name: %s (requires version go1.18 or later)", e.Name)
+                       check.versionErrorf(e, _UndeclaredName, "go1.18", "predeclared %s", e.Name)
                        return // avoid follow-on errors
                }
        }