]> Cypherpunks repositories - gostls13.git/commitdiff
rewrite &Point{1, 2} as allocation
authorRuss Cox <rsc@golang.org>
Tue, 21 Oct 2008 23:53:54 +0000 (16:53 -0700)
committerRuss Cox <rsc@golang.org>
Tue, 21 Oct 2008 23:53:54 +0000 (16:53 -0700)
R=ken
OCL=17592
CL=17592

src/cmd/gc/go.y
src/cmd/gc/walk.c
test/addr.go [deleted file]
test/complit.go
test/golden.out

index 12fe548c9d3895628e3b8aa74fc7fa7f65882a22..fd0c5b48dc6e4fdc3a44df09828b2e68cefae112 100644 (file)
@@ -754,8 +754,6 @@ uexpr:
        }
 |      '&' uexpr
        {
-               if($2->op == OCONV && !func)
-                       yyerror("& of composite literal at top level");
                $$ = nod(OADDR, $2, N);
        }
 |      '+' uexpr
@@ -1186,13 +1184,11 @@ xfndcl:
        {
                maxarg = 0;
                stksize = 0;
-               func++;
        } fndcl fnbody
        {
                $$ = $3;
                $$->nbody = $4;
                funcbody($$);
-               func--;
        }
 
 fndcl:
index fedf578b0ebef93ffc9c9600b7b6e56a5bca0328..f63b2933441f1deeb64a57719fe57e823b4a3969 100644 (file)
@@ -895,6 +895,36 @@ loop:
        case OADDR:
                if(top != Erv)
                        goto nottop;
+               if(n->left->op == OCONV && iscomposite(n->left->type)) {
+                       // turn &Point{1, 2} into allocation.
+                       // initialize with
+                       //      nvar := new(Point);
+                       //      *nvar = Point{1, 2};
+                       // and replace expression with nvar
+
+                       // TODO(rsc): might do a better job (fewer copies) later
+                       Node *nnew, *nvar, *nas;
+
+                       walktype(n->left, Elv);
+                       if(n->left == N)
+                               goto ret;
+
+                       nvar = nod(0, N, N);
+                       tempname(nvar, ptrto(n->left->type));
+
+                       nnew = nod(ONEW, N, N);
+                       nnew->type = nvar->type;
+                       nnew = newcompat(nnew);
+                       
+                       nas = nod(OAS, nvar, nnew);
+                       addtop = list(addtop, nas);
+                       
+                       nas = nod(OAS, nod(OIND, nvar, N), n->left);
+                       addtop = list(addtop, nas);
+
+                       indir(n, nvar);
+                       goto ret;
+               }
                walktype(n->left, Elv);
                if(n->left == N)
                        goto ret;
diff --git a/test/addr.go b/test/addr.go
deleted file mode 100644 (file)
index c803ee7..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-// errchk $G $D/$F.go
-
-// Copyright 2009 The Go Authors.  All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package main
-
-var a = &[]int{1,2};   // ERROR "composite"
index d0ebfad97c073bac1fe451764388bda41614a781..86985b994a1110609d706d6faa83b25833cd0eaa 100644 (file)
@@ -22,6 +22,11 @@ func eq(a *[]*R) {
        }
 }
 
+type P struct { a, b int };
+func NewP(a, b int) *P {
+       return &P{a, b}
+}
+
 func main() {
        var t T;
        t = T{0, 7.2, "hi", &t};
@@ -57,4 +62,8 @@ func main() {
        if len(m) != 3 { panic("m") }
 
        eq(&[]*R{itor(0), itor(1), itor(2), itor(3), itor(4), itor(5)});
+       
+       p1 := NewP(1, 2);
+       p2 := NewP(1, 2);
+       if p1 == p2 { panic("NewP") }
 }
index d7c87598a38ea17174077ea120cd934c72e4a83e..a7de99bf71ab0a526d0ede1005e9d4bc62b6a2fa 100644 (file)
@@ -129,11 +129,6 @@ found 2, expected 1
 panic on line 74 PC=xxx
 BUG wrong result
 
-=========== bugs/bug097.go
-
-panic on line 76 PC=xxx
-BUG wrong result
-
 =========== bugs/bug098.go
 bugs/bug098.go:10: illegal types for operand: AS
        *M