mirror of
https://github.com/golang/go
synced 2024-11-11 22:50:22 -07:00
[dev.typeparams] cmd/compile/internal/types2: no "declared but not used" errors for invalid var decls
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>
This commit is contained in:
parent
036245862a
commit
ab18125567
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user