]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: permit type elision from composite literal map keys
authorRobert Griesemer <gri@golang.org>
Fri, 9 Jan 2015 21:19:19 +0000 (13:19 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 20 Mar 2015 22:13:21 +0000 (22:13 +0000)
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>
src/go/parser/parser.go
src/go/parser/short_test.go

index f2df9a76d1b06eb8c8dc6aede3ac9eca68fbe933..0095d7facf3f2f5e6821fdad1ec2dfb314cd7c89 100644 (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
                }
index 14a14d5a5928b1b02d7c57b91ec2959655a60b3e..970ef2d3fae5679c30e59bf21afbce1cf71d3b4c 100644 (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) {