Skip to content

Commit 2da95e0

Browse files
griesemerRobert Griesemer
authored and
Robert Griesemer
committed
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 <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent b8d8c9e commit 2da95e0

File tree

21 files changed

+59
-63
lines changed

21 files changed

+59
-63
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestIssue5770(t *testing.T) {
2828
f := mustParse(t, `package p; type S struct{T}`)
2929
var conf Config
3030
_, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, nil) // do not crash
31-
want := "undeclared name: T"
31+
const want = "undefined: T"
3232
if err == nil || !strings.Contains(err.Error(), want) {
3333
t.Errorf("got: %v; want: %s", err, want)
3434
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ func (check *Checker) ident(x *operand, e *syntax.Name, def *Named, wantType boo
3737
check.error(e, _InvalidBlank, "cannot use _ as value or type")
3838
}
3939
} else {
40-
if check.conf.CompilerErrorMessages {
41-
check.errorf(e, _UndeclaredName, "undefined: %s", e.Value)
42-
} else {
43-
check.errorf(e, _UndeclaredName, "undeclared name: %s", e.Value)
44-
}
40+
check.errorf(e, _UndeclaredName, "undefined: %s", e.Value)
4541
}
4642
return
4743
case universeAny, universeComparable:
@@ -482,7 +478,7 @@ func (check *Checker) arrayLength(e syntax.Expr) int64 {
482478
if name, _ := e.(*syntax.Name); name != nil {
483479
obj := check.lookup(name.Value)
484480
if obj == nil {
485-
check.errorf(name, _InvalidArrayLen, "undeclared name %s for array length", name.Value)
481+
check.errorf(name, _InvalidArrayLen, "undefined %s for array length", name.Value)
486482
return -1
487483
}
488484
if _, ok := obj.(*Const); !ok {

src/go/types/issues_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestIssue5770(t *testing.T) {
3131
f := mustParse(t, `package p; type S struct{T}`)
3232
conf := Config{Importer: importer.Default()}
3333
_, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, nil) // do not crash
34-
want := "undeclared name: T"
34+
const want = "undefined: T"
3535
if err == nil || !strings.Contains(err.Error(), want) {
3636
t.Errorf("got: %v; want: %s", err, want)
3737
}

src/go/types/typexpr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, wantType bool)
3838
check.error(e, _InvalidBlank, "cannot use _ as value or type")
3939
}
4040
} else {
41-
check.errorf(e, _UndeclaredName, "undeclared name: %s", e.Name)
41+
check.errorf(e, _UndeclaredName, "undefined: %s", e.Name)
4242
}
4343
return
4444
case universeAny, universeComparable:
@@ -469,7 +469,7 @@ func (check *Checker) arrayLength(e ast.Expr) int64 {
469469
if name, _ := e.(*ast.Ident); name != nil {
470470
obj := check.lookup(name.Name)
471471
if obj == nil {
472-
check.errorf(name, _InvalidArrayLen, "undeclared name %s for array length", name.Name)
472+
check.errorf(name, _InvalidArrayLen, "undefined %s for array length", name.Name)
473473
return -1
474474
}
475475
if _, ok := obj.(*Const); !ok {

src/internal/types/testdata/check/constdecl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func _() {
8787
// Caused panic because the constant value was not set up (gri - 7/8/2014).
8888
func _() {
8989
const (
90-
x string = missing /* ERROR "undeclared name" */
90+
x string = missing /* ERROR "undefined" */
9191
y = x + ""
9292
)
9393
}
@@ -97,11 +97,11 @@ const A /* ERROR initialization cycle */ = unsafe.Sizeof(func() { _ = A })
9797

9898
func _() {
9999
// The function literal below must not see a.
100-
const a = unsafe.Sizeof(func() { _ = a /* ERROR "undeclared name" */ })
100+
const a = unsafe.Sizeof(func() { _ = a /* ERROR "undefined" */ })
101101
const b = unsafe.Sizeof(func() { _ = a })
102102

103103
// The function literal below must not see x, y, or z.
104-
const x, y, z = 0, 1, unsafe.Sizeof(func() { _ = x /* ERROR "undeclared name" */ + y /* ERROR "undeclared name" */ + z /* ERROR "undeclared name" */ })
104+
const x, y, z = 0, 1, unsafe.Sizeof(func() { _ = x /* ERROR "undefined" */ + y /* ERROR "undefined" */ + z /* ERROR "undefined" */ })
105105
}
106106

107107
// Test cases for errors in inherited constant initialization expressions.

src/internal/types/testdata/check/cycles0.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func _() {
8686
t1 /* ERROR invalid recursive type */ t1
8787
t2 *t2
8888

89-
t3 t4 /* ERROR undeclared */
90-
t4 t5 /* ERROR undeclared */
89+
t3 t4 /* ERROR undefined */
90+
t4 t5 /* ERROR undefined */
9191
t5 t3
9292

9393
// arrays

src/internal/types/testdata/check/decls0.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import "unsafe"
1313
const pi = 3.1415
1414

1515
type (
16-
N undeclared /* ERROR "undeclared" */
16+
N undefined /* ERROR "undefined" */
1717
B bool
1818
I int32
1919
A [10]P

src/internal/types/testdata/check/decls2/decls2a.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ type T2 struct {
4747
}
4848

4949
// Methods declared without a declared type.
50-
func (undeclared /* ERROR "undeclared" */) m() {}
51-
func (x *undeclared /* ERROR "undeclared" */) m() {}
50+
func (undefined /* ERROR "undefined" */) m() {}
51+
func (x *undefined /* ERROR "undefined" */) m() {}
5252

5353
func (pi /* ERROR "not a type" */) m1() {}
5454
func (x pi /* ERROR "not a type" */) m2() {}

src/internal/types/testdata/check/decls2/decls2b.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ func f_double /* ERROR "redeclared" */ () {}
3838

3939
// Blank methods need to be type-checked.
4040
// Verify by checking that errors are reported.
41-
func (T /* ERROR "undeclared" */ ) _() {}
42-
func (T1) _(undeclared /* ERROR "undeclared" */ ) {}
41+
func (T /* ERROR "undefined" */ ) _() {}
42+
func (T1) _(undefined /* ERROR "undefined" */ ) {}
4343
func (T1) _() int { return "foo" /* ERROR "cannot use .* in return statement" */ }
4444

45-
// Methods with undeclared receiver type can still be checked.
45+
// Methods with undefined receiver type can still be checked.
4646
// Verify by checking that errors are reported.
47-
func (Foo /* ERROR "undeclared" */ ) m() {}
48-
func (Foo /* ERROR "undeclared" */ ) m(undeclared /* ERROR "undeclared" */ ) {}
49-
func (Foo /* ERROR "undeclared" */ ) m() int { return "foo" /* ERROR "cannot use .* in return statement" */ }
47+
func (Foo /* ERROR "undefined" */ ) m() {}
48+
func (Foo /* ERROR "undefined" */ ) m(undefined /* ERROR "undefined" */ ) {}
49+
func (Foo /* ERROR "undefined" */ ) m() int { return "foo" /* ERROR "cannot use .* in return statement" */ }
5050

51-
func (Foo /* ERROR "undeclared" */ ) _() {}
52-
func (Foo /* ERROR "undeclared" */ ) _(undeclared /* ERROR "undeclared" */ ) {}
53-
func (Foo /* ERROR "undeclared" */ ) _() int { return "foo" /* ERROR "cannot use .* in return statement" */ }
51+
func (Foo /* ERROR "undefined" */ ) _() {}
52+
func (Foo /* ERROR "undefined" */ ) _(undefined /* ERROR "undefined" */ ) {}
53+
func (Foo /* ERROR "undefined" */ ) _() int { return "foo" /* ERROR "cannot use .* in return statement" */ }
5454

5555
// Receiver declarations are regular parameter lists;
5656
// receiver types may use parentheses, and the list

src/internal/types/testdata/check/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ func _() {
5353
// Don't report spurious errors as a consequence of earlier errors.
5454
// Add more tests as needed.
5555
func _() {
56-
if err := foo /* ERROR undeclared */ (); err != nil /* no error here */ {}
56+
if err := foo /* ERROR undefined */ (); err != nil /* no error here */ {}
5757
}
5858

5959
// Use unqualified names for package-local objects.
6060
type T struct{}
6161
var _ int = T /* ERROR value of type T */ {} // use T in error message rather then errors.T
6262

6363
// Don't report errors containing "invalid type" (issue #24182).
64-
func _(x *missing /* ERROR undeclared name: missing */ ) {
64+
func _(x *missing /* ERROR undefined: missing */ ) {
6565
x.m() // there shouldn't be an error here referring to *invalid type
6666
}

src/internal/types/testdata/check/expr3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func slice_literals() {
309309
const index1 = 1
310310
_ = S0{index1: 1}
311311
_ = S0{index2: 2}
312-
_ = S0{index3 /* ERROR "undeclared name" */ : 3}
312+
_ = S0{index3 /* ERROR "undefined" */ : 3}
313313

314314
// indices must be integer constants
315315
i := 1
@@ -385,7 +385,7 @@ func map_literals() {
385385
key1 := "foo"
386386
_ = M0{key1: 1}
387387
_ = M0{key2: 2}
388-
_ = M0{key3 /* ERROR "undeclared name" */ : 2}
388+
_ = M0{key3 /* ERROR "undefined" */ : 2}
389389

390390
var value int
391391
_ = M1{true: 1, false: 0}

src/internal/types/testdata/check/issues0.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ func issue8066() {
3131

3232
// Check that a missing identifier doesn't lead to a spurious error cascade.
3333
func issue8799a() {
34-
x, ok := missing /* ERROR undeclared */ ()
34+
x, ok := missing /* ERROR undefined */ ()
3535
_ = !ok
3636
_ = x
3737
}
3838

3939
func issue8799b(x int, ok bool) {
40-
x, ok = missing /* ERROR undeclared */ ()
40+
x, ok = missing /* ERROR undefined */ ()
4141
_ = !ok
4242
_ = x
4343
}
4444

4545
func issue9182() {
46-
type Point C /* ERROR undeclared */ .Point
46+
type Point C /* ERROR undefined */ .Point
4747
// no error for composite literal based on unknown type
4848
_ = Point{x: 1, y: 2}
4949
}
@@ -88,13 +88,13 @@ func issue10979() {
8888
T /* ERROR non-interface type T */
8989
}
9090
type _ interface {
91-
nosuchtype /* ERROR undeclared name: nosuchtype */
91+
nosuchtype /* ERROR undefined: nosuchtype */
9292
}
9393
type _ interface {
9494
fmt.Nosuchtype /* ERROR Nosuchtype not declared by package fmt */
9595
}
9696
type _ interface {
97-
nosuchpkg /* ERROR undeclared name: nosuchpkg */ .Nosuchtype
97+
nosuchpkg /* ERROR undefined: nosuchpkg */ .Nosuchtype
9898
}
9999
type I interface {
100100
I.m /* ERROR no field or method m */
@@ -207,11 +207,11 @@ func issue15755() {
207207
// Test that we don't get "declared but not used"
208208
// errors in the context of invalid/C objects.
209209
func issue20358() {
210-
var F C /* ERROR "undeclared" */ .F
211-
var A C /* ERROR "undeclared" */ .A
212-
var S C /* ERROR "undeclared" */ .S
213-
type T C /* ERROR "undeclared" */ .T
214-
type P C /* ERROR "undeclared" */ .P
210+
var F C /* ERROR "undefined" */ .F
211+
var A C /* ERROR "undefined" */ .A
212+
var S C /* ERROR "undefined" */ .S
213+
type T C /* ERROR "undefined" */ .T
214+
type P C /* ERROR "undefined" */ .P
215215

216216
// these variables must be "used" even though
217217
// the LHS expressions/types below in which
@@ -240,7 +240,7 @@ func issue24026() {
240240
// b and c must not be visible inside function literal
241241
a := 0
242242
a, b, c := func() (int, int, int) {
243-
return a, b /* ERROR undeclared */ , c /* ERROR undeclared */
243+
return a, b /* ERROR undefined */ , c /* ERROR undefined */
244244
}()
245245
_, _ = b, c
246246
}
@@ -313,7 +313,7 @@ type allocator struct {
313313

314314
// Test that we don't crash when type-checking composite literals
315315
// containing errors in the type.
316-
var issue27346 = [][n /* ERROR undeclared */ ]int{
316+
var issue27346 = [][n /* ERROR undefined */ ]int{
317317
0: {},
318318
}
319319

src/internal/types/testdata/check/stmt0.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func assignments1() {
6565
var u64 uint64
6666
u64 += 1<<u64
6767

68-
undeclared /* ERROR "undeclared" */ = 991
68+
undefined /* ERROR "undefined" */ = 991
6969

7070
// test cases for issue 5800
7171
var (

src/internal/types/testdata/check/vardecl.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ func _() {
158158
// Invalid variable declarations must not lead to "declared but not used errors".
159159
// TODO(gri) enable these tests once go/types follows types2 logic for declared but not used variables
160160
// func _() {
161-
// var a x // DISABLED_ERROR undeclared name: x
162-
// var b = x // DISABLED_ERROR undeclared name: x
163-
// var c int = x // DISABLED_ERROR undeclared name: x
161+
// var a x // DISABLED_ERROR undefined: x
162+
// var b = x // DISABLED_ERROR undefined: x
163+
// var c int = x // DISABLED_ERROR undefined: x
164164
// var d, e, f x /* DISABLED_ERROR x */ /* DISABLED_ERROR x */ /* DISABLED_ERROR x */
165165
// var g, h, i = x, x, x /* DISABLED_ERROR x */ /* DISABLED_ERROR x */ /* DISABLED_ERROR x */
166166
// var j, k, l float32 = x, x, x /* DISABLED_ERROR x */ /* DISABLED_ERROR x */ /* DISABLED_ERROR x */
@@ -204,11 +204,11 @@ var A /* ERROR initialization cycle */ = func() int { return A }()
204204

205205
func _() {
206206
// The function literal below must not see a.
207-
var a = func() int { return a /* ERROR "undeclared name" */ }()
207+
var a = func() int { return a /* ERROR "undefined" */ }()
208208
var _ = func() int { return a }()
209209

210210
// The function literal below must not see x, y, or z.
211-
var x, y, z = 0, 1, func() int { return x /* ERROR "undeclared name" */ + y /* ERROR "undeclared name" */ + z /* ERROR "undeclared name" */ }()
211+
var x, y, z = 0, 1, func() int { return x /* ERROR "undefined" */ + y /* ERROR "undefined" */ + z /* ERROR "undefined" */ }()
212212
_, _, _ = x, y, z
213213
}
214214

src/internal/types/testdata/fixedbugs/issue39634.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
package p
1010

1111
// crash 1
12-
type nt1[_ any]interface{g /* ERROR undeclared name */ }
13-
type ph1[e nt1[e],g(d /* ERROR undeclared name */ )]s /* ERROR undeclared name */
14-
func(*ph1[e,e /* ERROR redeclared */ ])h(d /* ERROR undeclared name */ )
12+
type nt1[_ any]interface{g /* ERROR undefined */ }
13+
type ph1[e nt1[e],g(d /* ERROR undefined */ )]s /* ERROR undefined */
14+
func(*ph1[e,e /* ERROR redeclared */ ])h(d /* ERROR undefined */ )
1515

1616
// crash 2
1717
// Disabled: empty []'s are now syntax errors. This example leads to too many follow-on errors.
@@ -39,11 +39,11 @@ type foo9[A any] interface { foo9 /* ERROR invalid recursive type */ [A] }
3939
func _() { var _ = new(foo9[int]) }
4040

4141
// crash 12
42-
var u /* ERROR cycle */ , i [func /* ERROR used as value */ /* ERROR used as value */ (u, c /* ERROR undeclared */ /* ERROR undeclared */ ) {}(0, len /* ERROR must be called */ /* ERROR must be called */ )]c /* ERROR undeclared */ /* ERROR undeclared */
42+
var u /* ERROR cycle */ , i [func /* ERROR used as value */ /* ERROR used as value */ (u, c /* ERROR undefined */ /* ERROR undefined */ ) {}(0, len /* ERROR must be called */ /* ERROR must be called */ )]c /* ERROR undefined */ /* ERROR undefined */
4343

4444
// crash 15
4545
func y15() { var a /* ERROR declared but not used */ interface{ p() } = G15[string]{} }
46-
type G15[X any] s /* ERROR undeclared name */
46+
type G15[X any] s /* ERROR undefined */
4747
func (G15 /* ERROR generic type .* without instantiation */ ) p()
4848

4949
// crash 16
@@ -62,7 +62,7 @@ func F17[T Z17](T) {}
6262
type o18[T any] []func(_ o18[[]_ /* ERROR cannot use _ */ ])
6363

6464
// crash 19
65-
type Z19 [][[]Z19{}[0][0]]c19 /* ERROR undeclared */
65+
type Z19 [][[]Z19{}[0][0]]c19 /* ERROR undefined */
6666

6767
// crash 20
6868
type Z20 /* ERROR invalid recursive type */ interface{ Z20 }

src/internal/types/testdata/fixedbugs/issue43527.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const L = 10
88

99
type (
1010
_ [L]struct{}
11-
_ [A /* ERROR undeclared name A for array length */ ]struct{}
11+
_ [A /* ERROR undefined A for array length */ ]struct{}
1212
_ [B /* ERROR invalid array length B */ ]struct{}
1313
_[A any] struct{}
1414

src/internal/types/testdata/fixedbugs/issue45635.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package main
66

77
func main() {
8-
some /* ERROR "undeclared name" */ [int, int]()
8+
some /* ERROR "undefined" */ [int, int]()
99
}
1010

1111
type N[T any] struct{}

src/internal/types/testdata/fixedbugs/issue49005.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ type T1 interface{ M() }
88

99
func F1() T1
1010

11-
var _ = F1().(*X1 /* ERROR undeclared name: X1 */)
11+
var _ = F1().(*X1 /* ERROR undefined: X1 */)
1212

1313
func _() {
1414
switch F1().(type) {
15-
case *X1 /* ERROR undeclared name: X1 */ :
15+
case *X1 /* ERROR undefined: X1 */ :
1616
}
1717
}
1818

src/internal/types/testdata/fixedbugs/issue49482.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type _[P *struct{}] struct{}
99
type _[P *int,] int
1010
type _[P (*int),] int
1111

12-
const P = 2 // declare P to avoid noisy 'undeclared name' errors below.
12+
const P = 2 // declare P to avoid noisy 'undefined' errors below.
1313

1414
// The following parse as invalid array types due to parsing ambiguitiues.
1515
type _ [P *int /* ERROR "int \(type\) is not an expression" */ ]int

src/internal/types/testdata/fixedbugs/issue50929.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func MMD[Rc RC /* ERROR got 1 arguments */ [RG], RG any, G any]() M /* ERROR got
5252

5353
var empty Rc
5454
switch any(empty).(type) {
55-
case BC /* ERROR undeclared name: BC */ :
55+
case BC /* ERROR undefined: BC */ :
5656

5757
case RSC[G]:
5858
nFn = NSG /* ERROR cannot use NSG\[G\] */ [G]

src/internal/types/testdata/fixedbugs/issue54405.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
package p
99

1010
var x struct {
11-
f *NotAType /* ERROR undeclared name */
11+
f *NotAType /* ERROR undefined */
1212
}
1313
var _ = x.f == nil // no error expected here
1414

15-
var y *NotAType /* ERROR undeclared name */
15+
var y *NotAType /* ERROR undefined */
1616
var _ = y == nil // no error expected here

0 commit comments

Comments
 (0)