]> Cypherpunks repositories - gostls13.git/commitdiff
gc: don't print implicit type on struct literal in export
authorLuuk van Dijk <lvd@golang.org>
Mon, 6 Feb 2012 11:19:59 +0000 (12:19 +0100)
committerLuuk van Dijk <lvd@golang.org>
Mon, 6 Feb 2012 11:19:59 +0000 (12:19 +0100)
As pointed out in the discussion around 2678.

R=rsc
CC=golang-dev
https://golang.org/cl/5534077

src/cmd/gc/fmt.c
src/cmd/gc/go.h
src/cmd/gc/go.y
src/cmd/gc/typecheck.c
src/cmd/gc/y.tab.c
test/fixedbugs/bug392.dir/one.go
test/fixedbugs/bug392.dir/two.go

index 31b0a623f2149ce326618e0a440a9c8b6ec7f662..35d33bce87f516e71f6751fcdd5eecb8744faf39 100644 (file)
@@ -1150,11 +1150,16 @@ exprfmt(Fmt *f, Node *n, int prec)
                return fmtprint(f, "%N{ %,H }", n->right, n->list);
 
        case OPTRLIT:
+               if (fmtmode == FExp && n->left->right->implicit == Implicit) 
+                       return fmtprint(f, "%N", n->left);
                return fmtprint(f, "&%N", n->left);
 
        case OSTRUCTLIT:
                if (fmtmode == FExp) {   // requires special handling of field names
-                       fmtprint(f, "%T{", n->type);
+                       if(n->right->implicit == Implicit)
+                               fmtstrcpy(f, "{");
+                       else 
+                               fmtprint(f, "%T{", n->type);
                        for(l=n->list; l; l=l->next) {
                                // another special case: if n->left is an embedded field of builtin type,
                                // it needs to be non-qualified.  Can't figure that out in %S, so do it here
@@ -1411,7 +1416,7 @@ nodedump(Fmt *fp, Node *n)
                fmtprint(fp, "%O-%O%J", n->op, n->etype, n);
                break;
        case OTYPE:
-               fmtprint(fp, "%O %S type=%T", n->op, n->sym, n->type);
+               fmtprint(fp, "%O %S%J type=%T", n->op, n->sym, n, n->type);
                if(recur && n->type == T && n->ntype) {
                        indent(fp);
                        fmtprint(fp, "%O-ntype%N", n->op, n->ntype);
index 7dc8e57e5093e2aa079188e8e9bf5ec0efaffb1f..4b47dddc115d780aeb8678dcdab660157bcc9f81 100644 (file)
@@ -217,6 +217,13 @@ enum
        EscNever,
 };
 
+enum
+{
+       Explicit,
+       Implicit,  // don't print in output
+       ImplPtr,   // OIND added by &T{ ... } literal
+};
+
 struct Node
 {
        // Tree structure.
@@ -252,7 +259,7 @@ struct      Node
        uchar   used;
        uchar   isddd;
        uchar   readonly;
-       uchar   implicit;       // don't show in printout
+       uchar   implicit;       // Explicit, Implicit, ImplPtr. 
        uchar   addrtaken;      // address taken, even if not moved to heap
        uchar   dupok;  // duplicate definitions ok (for func)
 
index de0735425022f43cd69614689a8524bcca0e5db0..c44aabf398c482ddadd96d7899ab91717800d5ce 100644 (file)
@@ -808,7 +808,7 @@ uexpr:
                        // Special case for &T{...}: turn into (*T){...}.
                        $$ = $2;
                        $$->right = nod(OIND, $$->right, N);
-                       $$->right->implicit = 1;
+                       $$->right->implicit = ImplPtr;
                } else {
                        $$ = nod(OADDR, $2, N);
                }
index 2e8c3b1e255288214a21c366972d091c0ba2a99b..4f2aea49928d388795f9ee2ac40c33e5829e2013 100644 (file)
@@ -2047,10 +2047,9 @@ typecheckcomplit(Node **np)
        n->type = t;
        
        if(isptr[t->etype]) {
-               // For better or worse, we don't allow pointers as
-               // the composite literal type, except when using
-               // the &T syntax, which sets implicit.
-               if(!n->right->implicit) {
+               // For better or worse, we don't allow pointers as the composite literal type,
+               // except when using the &T syntax, which sets implicit to ImplPtr.
+               if(n->right->implicit == Explicit) {
                        yyerror("invalid pointer type %T for composite literal (use &%T instead)", t, t->type);
                        goto error;
                }
index 80e05bbc3d74565c1bf32c23079d02d98a8eda55..9bf1019e9d722f58ca4293538841ffd5c9f182d4 100644 (file)
@@ -3232,7 +3232,7 @@ yyreduce:
                        // Special case for &T{...}: turn into (*T){...}.
                        (yyval.node) = (yyvsp[(2) - (2)].node);
                        (yyval.node)->right = nod(OIND, (yyval.node)->right, N);
-                       (yyval.node)->right->implicit = 1;
+                       (yyval.node)->right->implicit = ImplPtr;
                } else {
                        (yyval.node) = nod(OADDR, (yyvsp[(2) - (2)].node), N);
                }
index a7017255e5a1aa198236fcc2514c7c1e0849bbff..69fe08946285eee1517914a05366849b01f1e64d 100644 (file)
@@ -20,3 +20,22 @@ func F3() (ret []int) { return append(ret, 1) }
 // Call of inlined method with blank receiver.
 func (_ *T) M() int { return 1 }
 func (t *T) MM() int { return t.M() }
+
+
+// One more like issue 2678
+type S struct { x, y int }
+type U []S
+
+func F4(S int) U { return U{{S,S}} }
+
+func F5() []*S {
+       return []*S{ {1,2}, { 3, 4} }
+}
+
+func F6(S int) *U {
+       return &U{{S,S}}
+}
+
+
+
+
index b0ce26d39abadef65dfcadbb00e5e4990747c070..3485519f97c6be8ec526f894d8bcc6a713d66c5c 100644 (file)
@@ -13,6 +13,7 @@ func use() {
        one.F1(nil)
        one.F2(nil)
        one.F3()
+       one.F4(1)
 
        var t *one.T
        t.M()