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 <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
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)
}
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:
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 {
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)
}
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:
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 {
// 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 + ""
)
}
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.
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
const pi = 3.1415
type (
- N undeclared /* ERROR "undeclared" */
+ N undefined /* ERROR "undefined" */
B bool
I int32
A [10]P
}
// 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() {}
// 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
// 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.
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
}
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
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}
// 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}
}
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 */
// 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
// 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
}
// 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: {},
}
var u64 uint64
u64 += 1<<u64
- undeclared /* ERROR "undeclared" */ = 991
+ undefined /* ERROR "undefined" */ = 991
// test cases for issue 5800
var (
// Invalid variable declarations must not lead to "declared but not used errors".
// TODO(gri) enable these tests once go/types follows types2 logic for declared but not used variables
// func _() {
-// var a x // DISABLED_ERROR undeclared name: x
-// var b = x // DISABLED_ERROR undeclared name: x
-// var c int = x // DISABLED_ERROR undeclared name: x
+// var a x // DISABLED_ERROR undefined: x
+// var b = x // DISABLED_ERROR undefined: x
+// var c int = x // DISABLED_ERROR undefined: x
// var d, e, f x /* DISABLED_ERROR x */ /* DISABLED_ERROR x */ /* DISABLED_ERROR x */
// var g, h, i = x, x, x /* DISABLED_ERROR x */ /* DISABLED_ERROR x */ /* DISABLED_ERROR x */
// var j, k, l float32 = x, x, x /* DISABLED_ERROR x */ /* DISABLED_ERROR x */ /* DISABLED_ERROR x */
func _() {
// The function literal below must not see a.
- var a = func() int { return a /* ERROR "undeclared name" */ }()
+ var a = func() int { return a /* ERROR "undefined" */ }()
var _ = func() int { return a }()
// The function literal below must not see x, y, or z.
- var x, y, z = 0, 1, func() int { return x /* ERROR "undeclared name" */ + y /* ERROR "undeclared name" */ + z /* ERROR "undeclared name" */ }()
+ var x, y, z = 0, 1, func() int { return x /* ERROR "undefined" */ + y /* ERROR "undefined" */ + z /* ERROR "undefined" */ }()
_, _, _ = x, y, z
}
package p
// crash 1
-type nt1[_ any]interface{g /* ERROR undeclared name */ }
-type ph1[e nt1[e],g(d /* ERROR undeclared name */ )]s /* ERROR undeclared name */
-func(*ph1[e,e /* ERROR redeclared */ ])h(d /* ERROR undeclared name */ )
+type nt1[_ any]interface{g /* ERROR undefined */ }
+type ph1[e nt1[e],g(d /* ERROR undefined */ )]s /* ERROR undefined */
+func(*ph1[e,e /* ERROR redeclared */ ])h(d /* ERROR undefined */ )
// crash 2
// Disabled: empty []'s are now syntax errors. This example leads to too many follow-on errors.
func _() { var _ = new(foo9[int]) }
// crash 12
-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 */
+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 */
// crash 15
func y15() { var a /* ERROR declared but not used */ interface{ p() } = G15[string]{} }
-type G15[X any] s /* ERROR undeclared name */
+type G15[X any] s /* ERROR undefined */
func (G15 /* ERROR generic type .* without instantiation */ ) p()
// crash 16
type o18[T any] []func(_ o18[[]_ /* ERROR cannot use _ */ ])
// crash 19
-type Z19 [][[]Z19{}[0][0]]c19 /* ERROR undeclared */
+type Z19 [][[]Z19{}[0][0]]c19 /* ERROR undefined */
// crash 20
type Z20 /* ERROR invalid recursive type */ interface{ Z20 }
type (
_ [L]struct{}
- _ [A /* ERROR undeclared name A for array length */ ]struct{}
+ _ [A /* ERROR undefined A for array length */ ]struct{}
_ [B /* ERROR invalid array length B */ ]struct{}
_[A any] struct{}
package main
func main() {
- some /* ERROR "undeclared name" */ [int, int]()
+ some /* ERROR "undefined" */ [int, int]()
}
type N[T any] struct{}
func F1() T1
-var _ = F1().(*X1 /* ERROR undeclared name: X1 */)
+var _ = F1().(*X1 /* ERROR undefined: X1 */)
func _() {
switch F1().(type) {
- case *X1 /* ERROR undeclared name: X1 */ :
+ case *X1 /* ERROR undefined: X1 */ :
}
}
type _[P *int,] int
type _[P (*int),] int
-const P = 2 // declare P to avoid noisy 'undeclared name' errors below.
+const P = 2 // declare P to avoid noisy 'undefined' errors below.
// The following parse as invalid array types due to parsing ambiguitiues.
type _ [P *int /* ERROR "int \(type\) is not an expression" */ ]int
var empty Rc
switch any(empty).(type) {
- case BC /* ERROR undeclared name: BC */ :
+ case BC /* ERROR undefined: BC */ :
case RSC[G]:
nFn = NSG /* ERROR cannot use NSG\[G\] */ [G]
package p
var x struct {
- f *NotAType /* ERROR undeclared name */
+ f *NotAType /* ERROR undefined */
}
var _ = x.f == nil // no error expected here
-var y *NotAType /* ERROR undeclared name */
+var y *NotAType /* ERROR undefined */
var _ = y == nil // no error expected here