]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: no "declared but not used" errors for...
authorRobert Griesemer <gri@golang.org>
Wed, 2 Dec 2020 00:02:37 +0000 (16:02 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 2 Dec 2020 05:11:01 +0000 (05:11 +0000)
Matches compiler behavior.

Change-Id: I87ca46fb7269fbac61ffbf8ed48902156b06f6e4
Reviewed-on: https://go-review.googlesource.com/c/go/+/274615
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/assignments.go
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/testdata/vardecl.src

index 3178c38adeb7939e8e95a594ff67fd1c38939698..b367aa76da5ff60045b8f2c5d5b25f2d5e4dd8d0 100644 (file)
@@ -112,6 +112,7 @@ func (check *Checker) initVar(lhs *Var, x *operand, context string) Type {
                if lhs.typ == nil {
                        lhs.typ = Typ[Invalid]
                }
+               lhs.used = true // avoid follow-on "declared but not used" errors
                return nil
        }
 
index c7bfd3fd7bd1e626781334fb5a19f31302c6e52a..bb33e38051942ca6a0a95c64f37eb9aa0f11b25d 100644 (file)
@@ -457,6 +457,20 @@ func (check *Checker) constDecl(obj *Const, typ, init syntax.Expr) {
 func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init syntax.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.varType(typ)
index d8980f2ede41cf16b340f9bdc1fb5b6ee1e45882..9e48cdf847740ea69125c7cbcab59d81ff03ea74 100644 (file)
@@ -155,7 +155,18 @@ func _() {
        }
 }
 
-// Invalid (unused) expressions must not lead to spurious "declared but not used errors"
+// 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, x, x          /* ERROR x */ /* ERROR x */ /* ERROR x */
+       var j, k, l float32 = x, x, x  /* ERROR x */ /* ERROR 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
        var x, y int