Skip to content

Commit

Permalink
go/parser: fix (pathological) corner case
Browse files Browse the repository at this point in the history
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
  • Loading branch information
griesemer committed Sep 8, 2014
1 parent 857d55a commit ec96795
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/go/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()

Expand Down
1 change: 1 addition & 0 deletions src/go/parser/short_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ec96795

Please sign in to comment.