diff options
author | Rob Findley <rfindley@google.com> | 2021-04-28 10:35:53 -0400 |
---|---|---|
committer | Robert Findley <rfindley@google.com> | 2021-04-28 20:22:15 +0000 |
commit | 6082c05d8b4ab59e74204a3749629c8e6240b7b0 (patch) | |
tree | b1dc598d3147816558867f576c33e7c0dfb94fb2 /src/cmd/compile/internal/types2/assignments.go | |
parent | 414af503d7ec20cdfd6df23d5dee733401644ba3 (diff) | |
download | go-6082c05d8b4ab59e74204a3749629c8e6240b7b0.tar.gz go-6082c05d8b4ab59e74204a3749629c8e6240b7b0.zip |
go/types: better errors for invalid short var decls
This is a port of CL 312170 to go/types, adjusted to use go/ast and to
add error codes. go/parser already emits errors for non-identifiers on
the LHS of a short var decl, so a TODO is added to reconsider this
redundancy.
A new error code is added for repeated identifiers in short var decls.
This is a bit specific, but I considered it to be a unique kind of
error.
The x/tools tests for this port turned up a bug: the new logic failed to
call recordDef for blank identifiers. Patchset #2 contains the fix for
this bug, both in go/types and cmd/compile/internal/types2.
Change-Id: Ibdc40b8b4ad0e0696111d431682e1f1056fd5eeb
Reviewed-on: https://go-review.googlesource.com/c/go/+/314629
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/types2/assignments.go')
-rw-r--r-- | src/cmd/compile/internal/types2/assignments.go | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/src/cmd/compile/internal/types2/assignments.go b/src/cmd/compile/internal/types2/assignments.go index ec9fdbba62..583118c8b2 100644 --- a/src/cmd/compile/internal/types2/assignments.go +++ b/src/cmd/compile/internal/types2/assignments.go @@ -344,16 +344,14 @@ func (check *Checker) shortVarDecl(pos syntax.Pos, lhs, rhs []syntax.Expr) { } name := ident.Value - if name == "_" { - continue - } - - if seen[name] { - check.errorf(lhs, "%s repeated on left side of :=", lhs) - hasErr = true - continue + if name != "_" { + if seen[name] { + check.errorf(lhs, "%s repeated on left side of :=", lhs) + hasErr = true + continue + } + seen[name] = true } - seen[name] = true // Use the correct obj if the ident is redeclared. The // variable's scope starts after the declaration; so we @@ -374,7 +372,9 @@ func (check *Checker) shortVarDecl(pos syntax.Pos, lhs, rhs []syntax.Expr) { // declare new variable obj := NewVar(ident.Pos(), check.pkg, name, nil) lhsVars[i] = obj - newVars = append(newVars, obj) + if name != "_" { + newVars = append(newVars, obj) + } check.recordDef(ident, obj) } |