]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: fix (pathological) corner case
authorRobert Griesemer <gri@golang.org>
Mon, 8 Sep 2014 21:54:00 +0000 (14:54 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 8 Sep 2014 21:54:00 +0000 (14:54 -0700)
Inside a control clause (if ... {}), composite
literals starting with a type name must be parenthesized.
A composite literal used in the array length expression
of an array composite literal is already parenthesized.
Not a valid program, but syntactically is should
be accepted.

LGTM=adonovan
R=adonovan
CC=golang-codereviews
https://golang.org/cl/142760043

src/go/parser/parser.go
src/go/parser/short_test.go

index 9c62076f25e7504a84cb1a1351c93bd12c24177e..4a005d8ffa3892a88c597ad7de13903d5620a5ba 100644 (file)
@@ -641,6 +641,7 @@ func (p *parser) parseArrayType() ast.Expr {
        }
 
        lbrack := p.expect(token.LBRACK)
+       p.exprLev++
        var len ast.Expr
        // always permit ellipsis for more fault-tolerant parsing
        if p.tok == token.ELLIPSIS {
@@ -649,6 +650,7 @@ func (p *parser) parseArrayType() ast.Expr {
        } else if p.tok != token.RBRACK {
                len = p.parseRhs()
        }
+       p.exprLev--
        p.expect(token.RBRACK)
        elt := p.parseType()
 
index f861086ddbe48de970930eb2cc5d67167700cc9c..05e44de28a72dbee1f93dcfe3a730c663ff5df48 100644 (file)
@@ -39,6 +39,7 @@ var valids = []string{
        `package p; func ((*T),) m() {}`,
        `package p; func (*(T),) m() {}`,
        `package p; func _(x []int) { for range x {} }`,
+       `package p; func _() { if [T{}.n]int{} {} }`,
 }
 
 func TestValid(t *testing.T) {