func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init ast.Expr) {
assert(obj.typ == nil)
+ // If we have undefined variable types due to errors,
+ // mark variables as used to avoid follow-on errors.
+ // Matches compiler behavior.
+ defer func() {
+ if obj.typ == Typ[Invalid] {
+ obj.used = true
+ }
+ for _, lhs := range lhs {
+ if lhs.typ == Typ[Invalid] {
+ lhs.used = true
+ }
+ }
+ }()
+
// determine type, if any
if typ != nil {
obj.typ = check.typ(typ)
}
}
+
+// Invalid variable declarations must not lead to "declared but not used errors".
+func _() {
+ var a x // ERROR undeclared name: x
+ var b = x // ERROR undeclared name: x
+ var c int = x // ERROR undeclared name: x
+ var d, e, f x /* ERROR x */ /* ERROR x */ /* ERROR x */
+ var g, h, i = x /* ERROR x */, x /* ERROR x */, x /* ERROR x */
+ var j, k, l float32 = x /* ERROR x */, x /* ERROR x */, x /* ERROR x */
+ // but no "declared but not used" errors
+}
+
// Invalid (unused) expressions must not lead to spurious "declared but not used errors"
func _() {
var a, b, c int
_, _, _ = x, y, z
}
-// TODO(gri) consolidate other var decl checks in this file
\ No newline at end of file
+// TODO(gri) consolidate other var decl checks in this file