1
0
mirror of https://github.com/golang/go synced 2024-11-19 06:34:42 -07:00

go/parser: permit type elision from composite literal map keys

Per pending https://go-review.googlesource.com/2591 .

Change-Id: I1ce9d1c629e9fc43dbd862b3433aa5840f46656c
Reviewed-on: https://go-review.googlesource.com/2621
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Robert Griesemer 2015-01-09 13:19:19 -08:00
parent 98182c86b1
commit 492ac4b866
2 changed files with 22 additions and 6 deletions

View File

@ -1259,7 +1259,7 @@ func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
return &ast.CallExpr{Fun: fun, Lparen: lparen, Args: list, Ellipsis: ellipsis, Rparen: rparen}
}
func (p *parser) parseElement(keyOk bool) ast.Expr {
func (p *parser) parseValue(keyOk bool) ast.Expr {
if p.trace {
defer un(trace(p, "Element"))
}
@ -1287,16 +1287,30 @@ func (p *parser) parseElement(keyOk bool) ast.Expr {
x := p.checkExpr(p.parseExpr(keyOk))
if keyOk {
if p.tok == token.COLON {
colon := p.pos
p.next()
// Try to resolve the key but don't collect it
// as unresolved identifier if it fails so that
// we don't get (possibly false) errors about
// undeclared names.
p.tryResolve(x, false)
return &ast.KeyValueExpr{Key: x, Colon: colon, Value: p.parseElement(false)}
} else {
// not a key
p.resolve(x)
}
p.resolve(x) // not a key
}
return x
}
func (p *parser) parseElement() ast.Expr {
if p.trace {
defer un(trace(p, "Element"))
}
x := p.parseValue(true)
if p.tok == token.COLON {
colon := p.pos
p.next()
x = &ast.KeyValueExpr{Key: x, Colon: colon, Value: p.parseValue(false)}
}
return x
@ -1308,7 +1322,7 @@ func (p *parser) parseElementList() (list []ast.Expr) {
}
for p.tok != token.RBRACE && p.tok != token.EOF {
list = append(list, p.parseElement(true))
list = append(list, p.parseElement())
if !p.atComma("composite literal") {
break
}

View File

@ -44,6 +44,8 @@ var valids = []string{
`package p; func _(x interface{f()}) { interface{f()}(x).f() }`,
`package p; func _(x chan int) { chan int(x) <- 0 }`,
`package p; const (x = 0; y; z)`, // issue 9639
`package p; var _ = map[P]int{P{}:0, {}:1}`,
`package p; var _ = map[*P]int{&P{}:0, {}:1}`,
}
func TestValid(t *testing.T) {