scope := check.scope
// collect lhs variables
- var newVars []*Var
- var lhsVars = make([]*Var, len(lhs))
+ seen := make(map[string]bool, len(lhs))
+ lhsVars := make([]*Var, len(lhs))
+ newVars := make([]*Var, 0, len(lhs))
+ hasErr := false
for i, lhs := range lhs {
- var obj *Var
- if ident, _ := lhs.(*ast.Ident); ident != nil {
- // Use the correct obj if the ident is redeclared. The
- // variable's scope starts after the declaration; so we
- // must use Scope.Lookup here and call Scope.Insert
- // (via check.declare) later.
- name := ident.Name
- if alt := scope.Lookup(name); alt != nil {
- // redeclared object must be a variable
- if alt, _ := alt.(*Var); alt != nil {
- obj = alt
- } else {
- check.errorf(lhs, _UnassignableOperand, "cannot assign to %s", lhs)
- }
- check.recordUse(ident, alt)
+ ident, _ := lhs.(*ast.Ident)
+ if ident == nil {
+ check.useLHS(lhs)
+ // TODO(rFindley) this is redundant with a parser error. Consider omitting?
+ check.errorf(lhs, _BadDecl, "non-name %s on left side of :=", lhs)
+ hasErr = true
+ continue
+ }
+
+ name := ident.Name
+ if name != "_" {
+ if seen[name] {
+ check.errorf(lhs, _RepeatedDecl, "%s repeated on left side of :=", lhs)
+ hasErr = true
+ continue
+ }
+ seen[name] = true
+ }
+
+ // Use the correct obj if the ident is redeclared. The
+ // variable's scope starts after the declaration; so we
+ // must use Scope.Lookup here and call Scope.Insert
+ // (via check.declare) later.
+ if alt := scope.Lookup(name); alt != nil {
+ check.recordUse(ident, alt)
+ // redeclared object must be a variable
+ if obj, _ := alt.(*Var); obj != nil {
+ lhsVars[i] = obj
} else {
- // declare new variable, possibly a blank (_) variable
- obj = NewVar(ident.Pos(), check.pkg, name, nil)
- if name != "_" {
- newVars = append(newVars, obj)
- }
- check.recordDef(ident, obj)
+ check.errorf(lhs, _UnassignableOperand, "cannot assign to %s", lhs)
+ hasErr = true
}
- } else {
- check.useLHS(lhs)
- check.invalidAST(lhs, "cannot declare %s", lhs)
+ continue
+ }
+
+ // declare new variable
+ obj := NewVar(ident.Pos(), check.pkg, name, nil)
+ lhsVars[i] = obj
+ if name != "_" {
+ newVars = append(newVars, obj)
}
+ check.recordDef(ident, obj)
+ }
+
+ // create dummy variables where the lhs is invalid
+ for i, obj := range lhsVars {
if obj == nil {
- obj = NewVar(lhs.Pos(), check.pkg, "_", nil) // dummy variable
+ lhsVars[i] = NewVar(lhs[i].Pos(), check.pkg, "_", nil)
}
- lhsVars[i] = obj
}
check.initVars(lhsVars, rhs, token.NoPos)
// process function literals in rhs expressions before scope changes
check.processDelayed(top)
- // declare new variables
- if len(newVars) > 0 {
- // spec: "The scope of a constant or variable identifier declared inside
- // a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl
- // for short variable declarations) and ends at the end of the innermost
- // containing block."
- scopePos := rhs[len(rhs)-1].End()
- for _, obj := range newVars {
- check.declare(scope, nil, obj, scopePos) // recordObject already called
- }
- } else {
+ if len(newVars) == 0 && !hasErr {
check.softErrorf(pos, _NoNewVar, "no new variables on left side of :=")
+ return
+ }
+
+ // declare new variables
+ // spec: "The scope of a constant or variable identifier declared inside
+ // a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl
+ // for short variable declarations) and ends at the end of the innermost
+ // containing block."
+ scopePos := rhs[len(rhs)-1].End()
+ for _, obj := range newVars {
+ check.declare(scope, nil, obj, scopePos) // id = nil: recordDef already called
}
}
--- /dev/null
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func _() {
+ a, b, b /* ERROR b repeated on left side of := */ := 1, 2, 3
+ _ = a
+ _ = b
+}
+
+func _() {
+ a, _, _ := 1, 2, 3 // multiple _'s ok
+ _ = a
+}
+
+func _() {
+ var b int
+ a, b, b /* ERROR b repeated on left side of := */ := 1, 2, 3
+ _ = a
+ _ = b
+}
+
+func _() {
+ var a []int
+ a /* ERROR expected identifier */ /* ERROR non-name .* on left side of := */ [0], b := 1, 2
+ _ = a
+ _ = b
+}
+
+func _() {
+ var a int
+ a, a /* ERROR a repeated on left side of := */ := 1, 2
+ _ = a
+}
+
+func _() {
+ var a, b int
+ a, b := /* ERROR no new variables on left side of := */ 1, 2
+ _ = a
+ _ = b
+}