Patchwork [gccgo] Permit omitting type in composite literal with key

login
register
mail settings
Submitter Ian Taylor
Date Nov. 10, 2010, 10:57 p.m.
Message ID <mcr8w10vlba.fsf@google.com>
Download mbox | patch
Permalink /patch/70714/
State New
Headers show

Comments

Ian Taylor - Nov. 10, 2010, 10:57 p.m.
Go recently changed to permit omitting the type in a composite literal,
and I fixed gccgo to support that.  However, I missed the case where the
composite literal uses a key, as in

type S struct {
	i int
}
var M = map[int]S{ 0 : { 0 }, 1 : { 1 }}

Previously this had to be

var M = map[int]S{ 0 : S{ 0 }, 1 : S{ 1 }}

This patch permits omitting the type when using a key.  Committed to
gccgo branch.

Ian

Patch

diff -r 4cabf36880e8 go/parse.cc
--- a/go/parse.cc	Wed Nov 10 14:43:00 2010 -0800
+++ b/go/parse.cc	Wed Nov 10 14:54:23 2010 -0800
@@ -2395,7 +2395,15 @@ 
 
 	  vals->push_back(val);
 
-	  val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+	  if (!token->is_op(OPERATOR_LCURLY))
+	    val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+	  else
+	    {
+	      // This must be a composite literal inside another
+	      // composite literal, with the type omitted for the
+	      // inner one.
+	      val = this->composite_lit(type, depth + 1, token->location());
+	    }
 
 	  token = this->peek_token();
 	}