Commit 492ac4b8 authored by Robert Griesemer's avatar Robert Griesemer

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/2621Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 98182c86
...@@ -1259,7 +1259,7 @@ func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr { ...@@ -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} 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 { if p.trace {
defer un(trace(p, "Element")) defer un(trace(p, "Element"))
} }
...@@ -1287,16 +1287,30 @@ func (p *parser) parseElement(keyOk bool) ast.Expr { ...@@ -1287,16 +1287,30 @@ func (p *parser) parseElement(keyOk bool) ast.Expr {
x := p.checkExpr(p.parseExpr(keyOk)) x := p.checkExpr(p.parseExpr(keyOk))
if keyOk { if keyOk {
if p.tok == token.COLON { if p.tok == token.COLON {
colon := p.pos
p.next()
// Try to resolve the key but don't collect it // Try to resolve the key but don't collect it
// as unresolved identifier if it fails so that // as unresolved identifier if it fails so that
// we don't get (possibly false) errors about // we don't get (possibly false) errors about
// undeclared names. // undeclared names.
p.tryResolve(x, false) p.tryResolve(x, false)
return &ast.KeyValueExpr{Key: x, Colon: colon, Value: p.parseElement(false)} } else {
// not a key
p.resolve(x)
}
}
return x
}
func (p *parser) parseElement() ast.Expr {
if p.trace {
defer un(trace(p, "Element"))
} }
p.resolve(x) // not a key
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 return x
...@@ -1308,7 +1322,7 @@ func (p *parser) parseElementList() (list []ast.Expr) { ...@@ -1308,7 +1322,7 @@ func (p *parser) parseElementList() (list []ast.Expr) {
} }
for p.tok != token.RBRACE && p.tok != token.EOF { 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") { if !p.atComma("composite literal") {
break break
} }
......
...@@ -44,6 +44,8 @@ var valids = []string{ ...@@ -44,6 +44,8 @@ var valids = []string{
`package p; func _(x interface{f()}) { interface{f()}(x).f() }`, `package p; func _(x interface{f()}) { interface{f()}(x).f() }`,
`package p; func _(x chan int) { chan int(x) <- 0 }`, `package p; func _(x chan int) { chan int(x) <- 0 }`,
`package p; const (x = 0; y; z)`, // issue 9639 `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) { func TestValid(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment