From 2da95e0ec80cb7df1f89a7b7f147dde42ad17a19 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 21 Sep 2022 12:57:48 -0700 Subject: [PATCH] go/types, types2: report "undefined: x" instead of "undeclared name: x" This matches the compiler's long-standing behavior. For #55326. Change-Id: I90696a11f0b7d1f4be95a4b9a6f01844df2a2347 Reviewed-on: https://go-review.googlesource.com/c/go/+/432555 Run-TryBot: Robert Findley Reviewed-by: Robert Griesemer Reviewed-by: Robert Findley TryBot-Result: Gopher Robot --- .../compile/internal/types2/issues_test.go | 2 +- src/cmd/compile/internal/types2/typexpr.go | 8 ++----- src/go/types/issues_test.go | 2 +- src/go/types/typexpr.go | 4 ++-- .../types/testdata/check/constdecl.go | 6 ++--- src/internal/types/testdata/check/cycles0.go | 4 ++-- src/internal/types/testdata/check/decls0.go | 2 +- .../types/testdata/check/decls2/decls2a.go | 4 ++-- .../types/testdata/check/decls2/decls2b.go | 18 +++++++------- src/internal/types/testdata/check/errors.go | 4 ++-- src/internal/types/testdata/check/expr3.go | 4 ++-- src/internal/types/testdata/check/issues0.go | 24 +++++++++---------- src/internal/types/testdata/check/stmt0.go | 2 +- src/internal/types/testdata/check/vardecl.go | 10 ++++---- .../types/testdata/fixedbugs/issue39634.go | 12 +++++----- .../types/testdata/fixedbugs/issue43527.go | 2 +- .../types/testdata/fixedbugs/issue45635.go | 2 +- .../types/testdata/fixedbugs/issue49005.go | 4 ++-- .../types/testdata/fixedbugs/issue49482.go | 2 +- .../types/testdata/fixedbugs/issue50929.go | 2 +- .../types/testdata/fixedbugs/issue54405.go | 4 ++-- 21 files changed, 59 insertions(+), 63 deletions(-) diff --git a/src/cmd/compile/internal/types2/issues_test.go b/src/cmd/compile/internal/types2/issues_test.go index b77e8a8fe1..78691b9bf4 100644 --- a/src/cmd/compile/internal/types2/issues_test.go +++ b/src/cmd/compile/internal/types2/issues_test.go @@ -28,7 +28,7 @@ func TestIssue5770(t *testing.T) { f := mustParse(t, `package p; type S struct{T}`) var conf Config _, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, nil) // do not crash - want := "undeclared name: T" + const want = "undefined: T" if err == nil || !strings.Contains(err.Error(), want) { t.Errorf("got: %v; want: %s", err, want) } diff --git a/src/cmd/compile/internal/types2/typexpr.go b/src/cmd/compile/internal/types2/typexpr.go index 6953203788..900a730916 100644 --- a/src/cmd/compile/internal/types2/typexpr.go +++ b/src/cmd/compile/internal/types2/typexpr.go @@ -37,11 +37,7 @@ func (check *Checker) ident(x *operand, e *syntax.Name, def *Named, wantType boo check.error(e, _InvalidBlank, "cannot use _ as value or type") } } else { - if check.conf.CompilerErrorMessages { - check.errorf(e, _UndeclaredName, "undefined: %s", e.Value) - } else { - check.errorf(e, _UndeclaredName, "undeclared name: %s", e.Value) - } + check.errorf(e, _UndeclaredName, "undefined: %s", e.Value) } return case universeAny, universeComparable: @@ -482,7 +478,7 @@ func (check *Checker) arrayLength(e syntax.Expr) int64 { if name, _ := e.(*syntax.Name); name != nil { obj := check.lookup(name.Value) if obj == nil { - check.errorf(name, _InvalidArrayLen, "undeclared name %s for array length", name.Value) + check.errorf(name, _InvalidArrayLen, "undefined %s for array length", name.Value) return -1 } if _, ok := obj.(*Const); !ok { diff --git a/src/go/types/issues_test.go b/src/go/types/issues_test.go index 4f926ed421..b033460770 100644 --- a/src/go/types/issues_test.go +++ b/src/go/types/issues_test.go @@ -31,7 +31,7 @@ func TestIssue5770(t *testing.T) { f := mustParse(t, `package p; type S struct{T}`) conf := Config{Importer: importer.Default()} _, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, nil) // do not crash - want := "undeclared name: T" + const want = "undefined: T" if err == nil || !strings.Contains(err.Error(), want) { t.Errorf("got: %v; want: %s", err, want) } diff --git a/src/go/types/typexpr.go b/src/go/types/typexpr.go index fb34bcc2b2..3323e455d7 100644 --- a/src/go/types/typexpr.go +++ b/src/go/types/typexpr.go @@ -38,7 +38,7 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, wantType bool) check.error(e, _InvalidBlank, "cannot use _ as value or type") } } else { - check.errorf(e, _UndeclaredName, "undeclared name: %s", e.Name) + check.errorf(e, _UndeclaredName, "undefined: %s", e.Name) } return case universeAny, universeComparable: @@ -469,7 +469,7 @@ func (check *Checker) arrayLength(e ast.Expr) int64 { if name, _ := e.(*ast.Ident); name != nil { obj := check.lookup(name.Name) if obj == nil { - check.errorf(name, _InvalidArrayLen, "undeclared name %s for array length", name.Name) + check.errorf(name, _InvalidArrayLen, "undefined %s for array length", name.Name) return -1 } if _, ok := obj.(*Const); !ok { diff --git a/src/internal/types/testdata/check/constdecl.go b/src/internal/types/testdata/check/constdecl.go index bb07a361fa..faa9b9d5cb 100644 --- a/src/internal/types/testdata/check/constdecl.go +++ b/src/internal/types/testdata/check/constdecl.go @@ -87,7 +87,7 @@ func _() { // Caused panic because the constant value was not set up (gri - 7/8/2014). func _() { const ( - x string = missing /* ERROR "undeclared name" */ + x string = missing /* ERROR "undefined" */ y = x + "" ) } @@ -97,11 +97,11 @@ const A /* ERROR initialization cycle */ = unsafe.Sizeof(func() { _ = A }) func _() { // The function literal below must not see a. - const a = unsafe.Sizeof(func() { _ = a /* ERROR "undeclared name" */ }) + const a = unsafe.Sizeof(func() { _ = a /* ERROR "undefined" */ }) const b = unsafe.Sizeof(func() { _ = a }) // The function literal below must not see x, y, or z. - const x, y, z = 0, 1, unsafe.Sizeof(func() { _ = x /* ERROR "undeclared name" */ + y /* ERROR "undeclared name" */ + z /* ERROR "undeclared name" */ }) + const x, y, z = 0, 1, unsafe.Sizeof(func() { _ = x /* ERROR "undefined" */ + y /* ERROR "undefined" */ + z /* ERROR "undefined" */ }) } // Test cases for errors in inherited constant initialization expressions. diff --git a/src/internal/types/testdata/check/cycles0.go b/src/internal/types/testdata/check/cycles0.go index aaf82d49d2..e5368d13c9 100644 --- a/src/internal/types/testdata/check/cycles0.go +++ b/src/internal/types/testdata/check/cycles0.go @@ -86,8 +86,8 @@ func _() { t1 /* ERROR invalid recursive type */ t1 t2 *t2 - t3 t4 /* ERROR undeclared */ - t4 t5 /* ERROR undeclared */ + t3 t4 /* ERROR undefined */ + t4 t5 /* ERROR undefined */ t5 t3 // arrays diff --git a/src/internal/types/testdata/check/decls0.go b/src/internal/types/testdata/check/decls0.go index 9f7a006a20..6002a9e8a7 100644 --- a/src/internal/types/testdata/check/decls0.go +++ b/src/internal/types/testdata/check/decls0.go @@ -13,7 +13,7 @@ import "unsafe" const pi = 3.1415 type ( - N undeclared /* ERROR "undeclared" */ + N undefined /* ERROR "undefined" */ B bool I int32 A [10]P diff --git a/src/internal/types/testdata/check/decls2/decls2a.go b/src/internal/types/testdata/check/decls2/decls2a.go index cccbf29793..731405b20b 100644 --- a/src/internal/types/testdata/check/decls2/decls2a.go +++ b/src/internal/types/testdata/check/decls2/decls2a.go @@ -47,8 +47,8 @@ type T2 struct { } // Methods declared without a declared type. -func (undeclared /* ERROR "undeclared" */) m() {} -func (x *undeclared /* ERROR "undeclared" */) m() {} +func (undefined /* ERROR "undefined" */) m() {} +func (x *undefined /* ERROR "undefined" */) m() {} func (pi /* ERROR "not a type" */) m1() {} func (x pi /* ERROR "not a type" */) m2() {} diff --git a/src/internal/types/testdata/check/decls2/decls2b.go b/src/internal/types/testdata/check/decls2/decls2b.go index 5c55750a10..d4c5861e5e 100644 --- a/src/internal/types/testdata/check/decls2/decls2b.go +++ b/src/internal/types/testdata/check/decls2/decls2b.go @@ -38,19 +38,19 @@ func f_double /* ERROR "redeclared" */ () {} // Blank methods need to be type-checked. // Verify by checking that errors are reported. -func (T /* ERROR "undeclared" */ ) _() {} -func (T1) _(undeclared /* ERROR "undeclared" */ ) {} +func (T /* ERROR "undefined" */ ) _() {} +func (T1) _(undefined /* ERROR "undefined" */ ) {} func (T1) _() int { return "foo" /* ERROR "cannot use .* in return statement" */ } -// Methods with undeclared receiver type can still be checked. +// Methods with undefined receiver type can still be checked. // Verify by checking that errors are reported. -func (Foo /* ERROR "undeclared" */ ) m() {} -func (Foo /* ERROR "undeclared" */ ) m(undeclared /* ERROR "undeclared" */ ) {} -func (Foo /* ERROR "undeclared" */ ) m() int { return "foo" /* ERROR "cannot use .* in return statement" */ } +func (Foo /* ERROR "undefined" */ ) m() {} +func (Foo /* ERROR "undefined" */ ) m(undefined /* ERROR "undefined" */ ) {} +func (Foo /* ERROR "undefined" */ ) m() int { return "foo" /* ERROR "cannot use .* in return statement" */ } -func (Foo /* ERROR "undeclared" */ ) _() {} -func (Foo /* ERROR "undeclared" */ ) _(undeclared /* ERROR "undeclared" */ ) {} -func (Foo /* ERROR "undeclared" */ ) _() int { return "foo" /* ERROR "cannot use .* in return statement" */ } +func (Foo /* ERROR "undefined" */ ) _() {} +func (Foo /* ERROR "undefined" */ ) _(undefined /* ERROR "undefined" */ ) {} +func (Foo /* ERROR "undefined" */ ) _() int { return "foo" /* ERROR "cannot use .* in return statement" */ } // Receiver declarations are regular parameter lists; // receiver types may use parentheses, and the list diff --git a/src/internal/types/testdata/check/errors.go b/src/internal/types/testdata/check/errors.go index 7cdc5fb5ff..b3ab8afcac 100644 --- a/src/internal/types/testdata/check/errors.go +++ b/src/internal/types/testdata/check/errors.go @@ -53,7 +53,7 @@ func _() { // Don't report spurious errors as a consequence of earlier errors. // Add more tests as needed. func _() { - if err := foo /* ERROR undeclared */ (); err != nil /* no error here */ {} + if err := foo /* ERROR undefined */ (); err != nil /* no error here */ {} } // Use unqualified names for package-local objects. @@ -61,6 +61,6 @@ type T struct{} var _ int = T /* ERROR value of type T */ {} // use T in error message rather then errors.T // Don't report errors containing "invalid type" (issue #24182). -func _(x *missing /* ERROR undeclared name: missing */ ) { +func _(x *missing /* ERROR undefined: missing */ ) { x.m() // there shouldn't be an error here referring to *invalid type } diff --git a/src/internal/types/testdata/check/expr3.go b/src/internal/types/testdata/check/expr3.go index ba6c7dd314..abe209201a 100644 --- a/src/internal/types/testdata/check/expr3.go +++ b/src/internal/types/testdata/check/expr3.go @@ -309,7 +309,7 @@ func slice_literals() { const index1 = 1 _ = S0{index1: 1} _ = S0{index2: 2} - _ = S0{index3 /* ERROR "undeclared name" */ : 3} + _ = S0{index3 /* ERROR "undefined" */ : 3} // indices must be integer constants i := 1 @@ -385,7 +385,7 @@ func map_literals() { key1 := "foo" _ = M0{key1: 1} _ = M0{key2: 2} - _ = M0{key3 /* ERROR "undeclared name" */ : 2} + _ = M0{key3 /* ERROR "undefined" */ : 2} var value int _ = M1{true: 1, false: 0} diff --git a/src/internal/types/testdata/check/issues0.go b/src/internal/types/testdata/check/issues0.go index 5d6ed2bea7..8e277a7177 100644 --- a/src/internal/types/testdata/check/issues0.go +++ b/src/internal/types/testdata/check/issues0.go @@ -31,19 +31,19 @@ func issue8066() { // Check that a missing identifier doesn't lead to a spurious error cascade. func issue8799a() { - x, ok := missing /* ERROR undeclared */ () + x, ok := missing /* ERROR undefined */ () _ = !ok _ = x } func issue8799b(x int, ok bool) { - x, ok = missing /* ERROR undeclared */ () + x, ok = missing /* ERROR undefined */ () _ = !ok _ = x } func issue9182() { - type Point C /* ERROR undeclared */ .Point + type Point C /* ERROR undefined */ .Point // no error for composite literal based on unknown type _ = Point{x: 1, y: 2} } @@ -88,13 +88,13 @@ func issue10979() { T /* ERROR non-interface type T */ } type _ interface { - nosuchtype /* ERROR undeclared name: nosuchtype */ + nosuchtype /* ERROR undefined: nosuchtype */ } type _ interface { fmt.Nosuchtype /* ERROR Nosuchtype not declared by package fmt */ } type _ interface { - nosuchpkg /* ERROR undeclared name: nosuchpkg */ .Nosuchtype + nosuchpkg /* ERROR undefined: nosuchpkg */ .Nosuchtype } type I interface { I.m /* ERROR no field or method m */ @@ -207,11 +207,11 @@ func issue15755() { // Test that we don't get "declared but not used" // errors in the context of invalid/C objects. func issue20358() { - var F C /* ERROR "undeclared" */ .F - var A C /* ERROR "undeclared" */ .A - var S C /* ERROR "undeclared" */ .S - type T C /* ERROR "undeclared" */ .T - type P C /* ERROR "undeclared" */ .P + var F C /* ERROR "undefined" */ .F + var A C /* ERROR "undefined" */ .A + var S C /* ERROR "undefined" */ .S + type T C /* ERROR "undefined" */ .T + type P C /* ERROR "undefined" */ .P // these variables must be "used" even though // the LHS expressions/types below in which @@ -240,7 +240,7 @@ func issue24026() { // b and c must not be visible inside function literal a := 0 a, b, c := func() (int, int, int) { - return a, b /* ERROR undeclared */ , c /* ERROR undeclared */ + return a, b /* ERROR undefined */ , c /* ERROR undefined */ }() _, _ = b, c } @@ -313,7 +313,7 @@ type allocator struct { // Test that we don't crash when type-checking composite literals // containing errors in the type. -var issue27346 = [][n /* ERROR undeclared */ ]int{ +var issue27346 = [][n /* ERROR undefined */ ]int{ 0: {}, } diff --git a/src/internal/types/testdata/check/stmt0.go b/src/internal/types/testdata/check/stmt0.go index 14a37c1ed9..799f5e7ebb 100644 --- a/src/internal/types/testdata/check/stmt0.go +++ b/src/internal/types/testdata/check/stmt0.go @@ -65,7 +65,7 @@ func assignments1() { var u64 uint64 u64 += 1<