Skip to content

Commit 2f0da6d

Browse files
committed
go/types: revert "no 'declared but not used' errors for invalid var decls"
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]>
1 parent 70c37ee commit 2f0da6d

File tree

3 files changed

+1
-28
lines changed

3 files changed

+1
-28
lines changed

src/go/types/assignments.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ func (check *Checker) initVar(lhs *Var, x *operand, context string) Type {
120120
if lhs.typ == nil {
121121
lhs.typ = Typ[Invalid]
122122
}
123-
lhs.used = true
124123
return nil
125124
}
126125

src/go/types/decl.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -504,20 +504,6 @@ func (check *Checker) constDecl(obj *Const, typ, init ast.Expr, inherited bool)
504504
func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init ast.Expr) {
505505
assert(obj.typ == nil)
506506

507-
// If we have undefined variable types due to errors,
508-
// mark variables as used to avoid follow-on errors.
509-
// Matches compiler behavior.
510-
defer func() {
511-
if obj.typ == Typ[Invalid] {
512-
obj.used = true
513-
}
514-
for _, lhs := range lhs {
515-
if lhs.typ == Typ[Invalid] {
516-
lhs.used = true
517-
}
518-
}
519-
}()
520-
521507
// determine type, if any
522508
if typ != nil {
523509
obj.typ = check.typ(typ)

src/go/types/testdata/vardecl.src

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,6 @@ func _() {
158158
}
159159
}
160160

161-
162-
// Invalid variable declarations must not lead to "declared but not used errors".
163-
func _() {
164-
var a x // ERROR undeclared name: x
165-
var b = x // ERROR undeclared name: x
166-
var c int = x // ERROR undeclared name: x
167-
var d, e, f x /* ERROR x */ /* ERROR x */ /* ERROR x */
168-
var g, h, i = x /* ERROR x */, x /* ERROR x */, x /* ERROR x */
169-
var j, k, l float32 = x /* ERROR x */, x /* ERROR x */, x /* ERROR x */
170-
// but no "declared but not used" errors
171-
}
172-
173161
// Invalid (unused) expressions must not lead to spurious "declared but not used errors"
174162
func _() {
175163
var a, b, c int
@@ -215,4 +203,4 @@ func _() {
215203
_, _, _ = x, y, z
216204
}
217205

218-
// TODO(gri) consolidate other var decl checks in this file
206+
// TODO(gri) consolidate other var decl checks in this file

0 commit comments

Comments
 (0)