]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: leave checking of LHS in short var decls to type checker
authorRobert Griesemer <gri@golang.org>
Mon, 29 Aug 2022 22:45:35 +0000 (15:45 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 1 Sep 2022 23:18:06 +0000 (23:18 +0000)
Instead of checking at parse-time that the LHS of a short variable
declaration contains only identifiers, leave the check to the the
type checker which tests this already.

This removes a duplicate error and matches the behavior of the
syntax package.

For #54511.

Change-Id: I4c68f2bd8a0e015133685f9308beb98e714a83fc
Reviewed-on: https://go-review.googlesource.com/c/go/+/426476
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/syntax/parser.go
src/go/parser/parser.go
src/go/parser/short_test.go
src/go/types/testdata/fixedbugs/issue43087.go

index 8ba72fe7cf841419363c710381c89f8c0e1a2ab9..e2298852b8bd2e5d8dbc8e440f00c0fc9b8297c9 100644 (file)
@@ -2487,8 +2487,6 @@ func (p *parser) commClause() *CommClause {
                //
                // All these (and more) are recognized by simpleStmt and invalid
                // syntax trees are flagged later, during type checking.
-               // TODO(gri) eventually may want to restrict valid syntax trees
-               // here.
 
        case _Default:
                p.next()
index 26ba7b2892239562ce20e0c22f0d2a4540cf5848..cc3c0480949ebc2f2a02f6db25b77dcdc76d1d2e 100644 (file)
@@ -1867,11 +1867,7 @@ func (p *parser) parseSimpleStmt(mode int) (ast.Stmt, bool) {
                } else {
                        y = p.parseList(true)
                }
-               as := &ast.AssignStmt{Lhs: x, TokPos: pos, Tok: tok, Rhs: y}
-               if tok == token.DEFINE {
-                       p.checkAssignStmt(as)
-               }
-               return as, isRange
+               return &ast.AssignStmt{Lhs: x, TokPos: pos, Tok: tok, Rhs: y}, isRange
        }
 
        if len(x) > 1 {
@@ -1918,14 +1914,6 @@ func (p *parser) parseSimpleStmt(mode int) (ast.Stmt, bool) {
        return &ast.ExprStmt{X: x[0]}, false
 }
 
-func (p *parser) checkAssignStmt(as *ast.AssignStmt) {
-       for _, x := range as.Lhs {
-               if _, isIdent := x.(*ast.Ident); !isIdent {
-                       p.errorExpected(x.Pos(), "identifier on left side of :=")
-               }
-       }
-}
-
 func (p *parser) parseCallExpr(callType string) *ast.CallExpr {
        x := p.parseRhs() // could be a conversion: (some type)(x)
        if t := unparen(x); t != x {
@@ -2245,11 +2233,7 @@ func (p *parser) parseCommClause() *ast.CommClause {
                                pos := p.pos
                                p.next()
                                rhs := p.parseRhs()
-                               as := &ast.AssignStmt{Lhs: lhs, TokPos: pos, Tok: tok, Rhs: []ast.Expr{rhs}}
-                               if tok == token.DEFINE {
-                                       p.checkAssignStmt(as)
-                               }
-                               comm = as
+                               comm = &ast.AssignStmt{Lhs: lhs, TokPos: pos, Tok: tok, Rhs: []ast.Expr{rhs}}
                        } else {
                                // lhs must be single receive operation
                                if len(lhs) > 1 {
index ea8b087bae12b5c146607d2cdc66a02bb990cec9..298579ea6c0c20468dc738f5dc8532b50ac21e19 100644 (file)
@@ -143,7 +143,6 @@ var invalids = []string{
        `package p; func f() { switch t /* ERROR "expected switch expression" */ = t.(type), t {} };`,
        `package p; func f() { _ = (<-<- /* ERROR "expected 'chan'" */ chan int)(nil) };`,
        `package p; func f() { _ = (<-chan<-chan<-chan<-chan<-chan<- /* ERROR "expected channel type" */ int)(nil) };`,
-       `package p; func f() { var t []int; t /* ERROR "expected identifier on left side of :=" */ [0] := 0 };`,
        `package p; func f() { if x := g(); x /* ERROR "expected boolean expression" */ = 0 {}};`,
        `package p; func f() { _ = x = /* ERROR "expected '=='" */ 0 {}};`,
        `package p; func f() { _ = 1 == func()int { var x bool; x = x = /* ERROR "expected '=='" */ true; return x }() };`,
index ef37b4aa2919ac9250a80e48725949daf72ee1c2..85d4450139ea5f867e8017a9818f5c4b26edfe0e 100644 (file)
@@ -24,7 +24,7 @@ func _() {
 
 func _() {
        var a []int
-       a /* ERROR expected identifier */ /* ERROR non-name .* on left side of := */ [0], b := 1, 2
+       a /* ERROR non-name .* on left side of := */ [0], b := 1, 2
        _ = a
        _ = b
 }