From: Alan Donovan Date: Thu, 18 Dec 2025 17:27:11 +0000 (-0500) Subject: go/types,cmd/compile/internal/types2: better diagnostic for type shadowing X-Git-Tag: go1.26rc3~11^2~4 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=532e3203492ebcac67b2f3aa2a52115f49d51997;p=gostls13.git go/types,cmd/compile/internal/types2: better diagnostic for type shadowing This change causes the "x is not a type" diagnostic to describe x's actual kind, helping to reveal when shadowing is at work. (The kind description could improve other errors too.) Fixes #76877 Change-Id: Ia3484998bb384ff570c20b6792cf8461c60aa38c Reviewed-on: https://go-review.googlesource.com/c/go/+/731180 Reviewed-by: Robert Griesemer Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Griesemer --- diff --git a/src/cmd/compile/internal/types2/object.go b/src/cmd/compile/internal/types2/object.go index dd2d415790..5d284ee61b 100644 --- a/src/cmd/compile/internal/types2/object.go +++ b/src/cmd/compile/internal/types2/object.go @@ -671,3 +671,52 @@ func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) { } buf.WriteString(f.name) } + +// objectKind returns a description of the object's kind. +func objectKind(obj Object) string { + switch obj := obj.(type) { + case *PkgName: + return "package name" + case *Const: + return "constant" + case *TypeName: + if obj.IsAlias() { + return "type alias" + } else if _, ok := obj.Type().(*TypeParam); ok { + return "type parameter" + } else { + return "defined type" + } + case *Var: + switch obj.Kind() { + case PackageVar: + return "package-level variable" + case LocalVar: + return "local variable" + case RecvVar: + return "receiver" + case ParamVar: + return "parameter" + case ResultVar: + return "result variable" + case FieldVar: + return "struct field" + } + case *Func: + if obj.Signature().Recv() != nil { + return "method" + } else { + return "function" + } + case *Label: + return "label" + case *Builtin: + return "built-in function" + case *Nil: + return "untyped nil" + } + if debug { + panic(fmt.Sprintf("unknown symbol (%T)", obj)) + } + return "unknown symbol" +} diff --git a/src/cmd/compile/internal/types2/typexpr.go b/src/cmd/compile/internal/types2/typexpr.go index a79b54eacc..7d5932eec1 100644 --- a/src/cmd/compile/internal/types2/typexpr.go +++ b/src/cmd/compile/internal/types2/typexpr.go @@ -50,7 +50,8 @@ func (check *Checker) ident(x *operand, e *syntax.Name, wantType bool) { // (see go.dev/issue/65344). _, gotType := obj.(*TypeName) if !gotType && wantType { - check.errorf(e, NotAType, "%s is not a type", obj.Name()) + check.errorf(e, NotAType, "%s (%s) is not a type", obj.Name(), objectKind(obj)) + // avoid "declared but not used" errors // (don't use Checker.use - we don't want to evaluate too much) if v, _ := obj.(*Var); v != nil && v.pkg == check.pkg /* see Checker.use1 */ { diff --git a/src/go/types/object.go b/src/go/types/object.go index 57158c1595..e12bf7285a 100644 --- a/src/go/types/object.go +++ b/src/go/types/object.go @@ -674,3 +674,52 @@ func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) { } buf.WriteString(f.name) } + +// objectKind returns a description of the object's kind. +func objectKind(obj Object) string { + switch obj := obj.(type) { + case *PkgName: + return "package name" + case *Const: + return "constant" + case *TypeName: + if obj.IsAlias() { + return "type alias" + } else if _, ok := obj.Type().(*TypeParam); ok { + return "type parameter" + } else { + return "defined type" + } + case *Var: + switch obj.Kind() { + case PackageVar: + return "package-level variable" + case LocalVar: + return "local variable" + case RecvVar: + return "receiver" + case ParamVar: + return "parameter" + case ResultVar: + return "result variable" + case FieldVar: + return "struct field" + } + case *Func: + if obj.Signature().Recv() != nil { + return "method" + } else { + return "function" + } + case *Label: + return "label" + case *Builtin: + return "built-in function" + case *Nil: + return "untyped nil" + } + if debug { + panic(fmt.Sprintf("unknown symbol (%T)", obj)) + } + return "unknown symbol" +} diff --git a/src/go/types/typexpr.go b/src/go/types/typexpr.go index 346ff18e9a..549c307487 100644 --- a/src/go/types/typexpr.go +++ b/src/go/types/typexpr.go @@ -49,7 +49,8 @@ func (check *Checker) ident(x *operand, e *ast.Ident, wantType bool) { // (see go.dev/issue/65344). _, gotType := obj.(*TypeName) if !gotType && wantType { - check.errorf(e, NotAType, "%s is not a type", obj.Name()) + check.errorf(e, NotAType, "%s (%s) is not a type", obj.Name(), objectKind(obj)) + // avoid "declared but not used" errors // (don't use Checker.use - we don't want to evaluate too much) if v, _ := obj.(*Var); v != nil && v.pkg == check.pkg /* see Checker.use1 */ { diff --git a/src/internal/types/testdata/check/cycles5.go b/src/internal/types/testdata/check/cycles5.go index de85c03d8a..9605f8ded4 100644 --- a/src/internal/types/testdata/check/cycles5.go +++ b/src/internal/types/testdata/check/cycles5.go @@ -162,7 +162,7 @@ func makeArray() (res T12) { return } // issue #20770 var r = newReader() -func newReader() r // ERROR "r is not a type" +func newReader() r // ERROR "r (package-level variable) is not a type" // variations of the theme of #8699 and #20770 var arr /* ERROR "cycle" */ = f() diff --git a/src/internal/types/testdata/check/cycles5a.go b/src/internal/types/testdata/check/cycles5a.go index e10f554e5c..a8cad50243 100644 --- a/src/internal/types/testdata/check/cycles5a.go +++ b/src/internal/types/testdata/check/cycles5a.go @@ -162,7 +162,7 @@ func makeArray() (res T12) { return } // issue #20770 var r = newReader() -func newReader() r // ERROR "r is not a type" +func newReader() r // ERROR "r (package-level variable) is not a type" // variations of the theme of #8699 and #20770 var arr /* ERROR "cycle" */ = f() diff --git a/src/internal/types/testdata/check/decls1.go b/src/internal/types/testdata/check/decls1.go index 6cdbf27f4c..5e4ba86cb0 100644 --- a/src/internal/types/testdata/check/decls1.go +++ b/src/internal/types/testdata/check/decls1.go @@ -63,7 +63,7 @@ var ( t12 complex64 = -(u + *t11) / *&v t13 int = a /* ERROR "shifted operand" */ << d t14 int = i << j - t15 math /* ERROR "math is not a type" */ + t15 math /* ERROR "math (package name) is not a type" */ t16 math.xxx /* ERROR "undefined" */ t17 math /* ERROR "not a type" */ .Pi t18 float64 = math.Pi * 10.0 @@ -144,3 +144,10 @@ func init /* ERROR "no arguments and no return values" */ (int) {} func init /* ERROR "no arguments and no return values" */ () int { return 0 } func init /* ERROR "no arguments and no return values" */ (int) int { return 0 } func (T) init(int) int { return 0 } + +func _() { + var error error + var _ error /* ERROR "error (local variable) is not a type" */ + _ = error +} + diff --git a/src/internal/types/testdata/check/issues0.go b/src/internal/types/testdata/check/issues0.go index 6117f7a8b9..fb4e1282f9 100644 --- a/src/internal/types/testdata/check/issues0.go +++ b/src/internal/types/testdata/check/issues0.go @@ -104,7 +104,7 @@ func issue10979() { // issue11347 // These should not crash. -var a1, b1, c1 /* ERROR "cycle" */ b1 /* ERROR "b1 is not a type" */ = 0 > 0<<""[""[c1]]>c1 +var a1, b1, c1 /* ERROR "cycle" */ b1 /* ERROR "b1 (package-level variable) is not a type" */ = 0 > 0<<""[""[c1]]>c1 var a2, b2 /* ERROR "cycle" */ = 0 /* ERROR "assignment mismatch" */ /* ERROR "assignment mismatch" */ > 0<<""[b2] var a3, b3 /* ERROR "cycle" */ = int /* ERROR "assignment mismatch" */ /* ERROR "assignment mismatch" */ (1<<""[b3]) diff --git a/src/internal/types/testdata/check/issues1.go b/src/internal/types/testdata/check/issues1.go index 72c6cf7757..482bdb08e1 100644 --- a/src/internal/types/testdata/check/issues1.go +++ b/src/internal/types/testdata/check/issues1.go @@ -246,5 +246,5 @@ var _ = append[context.CancelFunc, []context.CancelFunc, context.CancelFunc](can func g[T any](T) T { panic(0) } var _ = g[int] -var _ = g[nil /* ERROR "is not a type" */ ] +var _ = g[nil /* ERROR "nil (untyped nil) is not a type" */ ] var _ = g(0) diff --git a/src/internal/types/testdata/fixedbugs/issue39634.go b/src/internal/types/testdata/fixedbugs/issue39634.go index 58fc43eea6..5392f903c2 100644 --- a/src/internal/types/testdata/fixedbugs/issue39634.go +++ b/src/internal/types/testdata/fixedbugs/issue39634.go @@ -43,7 +43,7 @@ type foo9[A any] interface { foo9 /* ERROR "invalid recursive type" */ [A] } func _() { var _ = new(foo9[int]) } // crash 12 -var u, i [func /* ERROR "used as value" */ /* ERROR "used as value" */ (u /* ERROR "u is not a type" */ /* ERROR "u is not a type" */ , c /* ERROR "undefined" */ /* ERROR "undefined" */ ) {}(0, len /* ERROR "must be called" */ /* ERROR "must be called" */ )]c /* ERROR "undefined" */ /* ERROR "undefined" */ +var u, i [func /* ERROR "used as value" */ /* ERROR "used as value" */ (u /* ERROR "u (package-level variable) is not a type" */ /* ERROR "u (package-level variable) is not a type" */ , 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 and not used" */ interface{ p() } = G15[string]{} } diff --git a/src/internal/types/testdata/fixedbugs/issue65344.go b/src/internal/types/testdata/fixedbugs/issue65344.go index 9f8337cf2b..4db97c04ff 100644 --- a/src/internal/types/testdata/fixedbugs/issue65344.go +++ b/src/internal/types/testdata/fixedbugs/issue65344.go @@ -4,12 +4,12 @@ package p -type T1 C /* ERROR "C is not a type" */ +type T1 C /* ERROR "C (constant) is not a type" */ // TODO(gri) try to avoid this follow-on error const C = T1(0 /* ERROR "cannot convert 0 (untyped int constant) to type T1" */) -type T2 V /* ERROR "V is not a type" */ +type T2 V /* ERROR "V (package-level variable) is not a type" */ var V T2 diff --git a/test/fixedbugs/issue4610.go b/test/fixedbugs/issue4610.go index d56c6d3e8c..8993f9882b 100644 --- a/test/fixedbugs/issue4610.go +++ b/test/fixedbugs/issue4610.go @@ -14,4 +14,3 @@ func main() { var foo bar _ = &foo{} // ERROR "is not a type|expected .;." } // GCCGO_ERROR "expected declaration" -