]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: only mark variables as used if they are
authorRobert Griesemer <gri@golang.org>
Tue, 18 Apr 2023 23:43:11 +0000 (16:43 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 19 Apr 2023 14:07:00 +0000 (14:07 +0000)
Marking variables in erroneous variable declarations as used is
convenient for tests but doesn't necessarily hide follow-on errors
in real code: either the variable is not supposed to be declared in
the first place and then we should get an error if it is not used,
or it is there because it is intended to be used, and the we expect
an error it if is not used.

This brings types2 closer to go/types.

Change-Id: If7ee1298fc770f7ad0cefe7e968533fd50ec2343
Reviewed-on: https://go-review.googlesource.com/c/go/+/486175
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/types2/decl.go
src/internal/types/testdata/fixedbugs/issue59371.go
test/fixedbugs/issue30085.go
test/fixedbugs/issue30087.go
test/rename1.go

index f7c6a8e5734a6d3e15e64f43793b18d656581318..dd39c42037f949f0bb0116d810ddd5720ae33c62 100644 (file)
@@ -416,20 +416,6 @@ func (check *Checker) constDecl(obj *Const, typ, init syntax.Expr, inherited boo
 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 d60810a6f0969f79abb9164a40820a05cffbee2e..d5b4db6a858325aae24c645b0db6312af5d650b6 100644 (file)
@@ -12,9 +12,6 @@ func _() {
 
 func _() {
        var ok = undef /* ERROR "undefined: undef" */
-       x, ok := m[0] // must not crash
-       _ = x
-       // The next line is only needed for go/types, not types2.
-       // TODO(gri) find cause and fix
-       _ = ok
+       x, ok := m[0]  // must not crash
+       _, _ = x, ok
 }
index 512355a443a69c352557a6eafd971d317fa5de0e..f8595ab90c4bac78aa91cc6d505ec278160d37a8 100644 (file)
@@ -9,5 +9,5 @@ package main
 func main() {
        var c, d = 1, 2, 3 // ERROR "assignment mismatch: 2 variables but 3 values|wrong number of initializations|extra init expr"
        var e, f, g = 1, 2 // ERROR "assignment mismatch: 3 variables but 2 values|wrong number of initializations|missing init expr"
-       _, _, _, _ = c, d, e, f
+       _, _, _, _, _ = c, d, e, f, g
 }
index 3e4b0324a3765404a0dfd9daf10020829dee669d..c55c47b06486bfc00e16701dd6b92604cf044ae4 100644 (file)
@@ -11,5 +11,5 @@ func main() {
        _ = 1, 2        // ERROR "assignment mismatch: 1 variable but 2 values|number of variables does not match|cannot assign"
        c, d := 1       // ERROR "assignment mismatch: 2 variables but 1 value|wrong number of initializations|cannot initialize"
        e, f := 1, 2, 3 // ERROR "assignment mismatch: 2 variables but 3 values|wrong number of initializations|cannot initialize"
-       _, _, _, _ = c, d, e, f
+       _, _, _, _, _, _ = a, b, c, d, e, f
 }
index 058db4494a06b56a75bc516ad1519f70c8966bd7..56824e99eca75510a81b233d5cd4a4016fd5f02c 100644 (file)
@@ -15,7 +15,7 @@ func main() {
        const (
                a = 1 + iota // ERROR "invalid operation|incompatible types|cannot convert"
        )
-
+       _, _ = n, y
 }
 
 const (