]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: revert "no 'declared but not used' errors for invalid var decls"
authorRob Findley <rfindley@google.com>
Wed, 17 Feb 2021 01:01:32 +0000 (20:01 -0500)
committerRobert Findley <rfindley@google.com>
Wed, 17 Feb 2021 01:29:54 +0000 (01:29 +0000)
This reverts commit CL 289712 (afd67f3). It breaks x/tools tests, and
those tests highlight that perhaps I didn't think through the
repercussions of this change as much as I should have.

Fixes #44316

Change-Id: I5db39b4e2a3714131aa22423abfe0f34a0376192
Reviewed-on: https://go-review.googlesource.com/c/go/+/292751
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/go/types/assignments.go
src/go/types/decl.go
src/go/types/testdata/vardecl.src

index d6f18c9bee1e93f3ba37e5eb4e2c8304b0c9c58e..616564b567d7315d4723c5ed3f7ddb46b7a35a8f 100644 (file)
@@ -120,7 +120,6 @@ func (check *Checker) initVar(lhs *Var, x *operand, context string) Type {
                if lhs.typ == nil {
                        lhs.typ = Typ[Invalid]
                }
-               lhs.used = true
                return nil
        }
 
index b861cde496490095c71cc9b08d1e6152b37ca0f8..6462edbd75e3e63eac8f25653eb1e3fdaea4b2ad 100644 (file)
@@ -504,20 +504,6 @@ func (check *Checker) constDecl(obj *Const, typ, init ast.Expr, inherited bool)
 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)
index 6e2d1b5bd5e3b5c65454b7987bb4db9d4be3d915..54f5ef1e10d0983f55f65ea32f421e2b4e53ee63 100644 (file)
@@ -158,18 +158,6 @@ func _() {
        }
 }
 
-
-// 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
@@ -215,4 +203,4 @@ func _() {
        _, _, _ = x, y, z
 }
 
-// TODO(gri) consolidate other var decl checks in this file
+// TODO(gri) consolidate other var decl checks in this file
\ No newline at end of file