}
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"
+}
// (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 */ {
}
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"
+}
// (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 */ {
// 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()
// 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()
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
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
+}
+
// 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])
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)
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]{} }
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
var foo bar
_ = &foo{} // ERROR "is not a type|expected .;."
} // GCCGO_ERROR "expected declaration"
-