Skip to content

Commit

Permalink
go/types: revert "no 'declared but not used' errors for invalid var d…
Browse files Browse the repository at this point in the history
…ecls"

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 <[email protected]>
Trust: Robert Findley <[email protected]>
Run-TryBot: Robert Findley <[email protected]>
TryBot-Result: Go Bot <[email protected]>
  • Loading branch information
findleyr committed Feb 17, 2021
1 parent 70c37ee commit 2f0da6d
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 28 deletions.
1 change: 0 additions & 1 deletion src/go/types/assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 0 additions & 14 deletions src/go/types/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 1 addition & 13 deletions src/go/types/testdata/vardecl.src
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 2f0da6d

Please sign in to comment.