]> Cypherpunks repositories - gostls13.git/commitdiff
gc: fieldnames in structliterals in exported inlines should not be qualified if they...
authorLuuk van Dijk <lvd@golang.org>
Wed, 18 Jan 2012 16:51:28 +0000 (17:51 +0100)
committerLuuk van Dijk <lvd@golang.org>
Wed, 18 Jan 2012 16:51:28 +0000 (17:51 +0100)
Trust me.
Fixes #2687.

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

src/cmd/gc/fmt.c
test/fixedbugs/bug396.dir/one.go [new file with mode: 0644]
test/fixedbugs/bug396.dir/two.go [new file with mode: 0644]
test/fixedbugs/bug396.go [new file with mode: 0644]

index 15466844bebaef525e18647a6dbb1af9b0c7d7d2..6f2041c1c5684b0ab6936276b97288e3e24a973f 100644 (file)
@@ -1062,6 +1062,7 @@ exprfmt(Fmt *f, Node *n, int prec)
 {
        int nprec;
        NodeList *l;
+       Type *t;
 
        while(n && n->implicit)
                n = n->left;
@@ -1160,11 +1161,22 @@ exprfmt(Fmt *f, Node *n, int prec)
        case OSTRUCTLIT:
                if (fmtmode == FExp) {   // requires special handling of field names
                        fmtprint(f, "%T{", n->type);
-                       for(l=n->list; l; l=l->next)
+                       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
+                               if(l->n->left->type->embedded) {
+                                       t = l->n->left->type->type;
+                                       if(t->sym == S)
+                                               t = t->type;
+                                       fmtprint(f, " %T:%N", t, l->n->right);
+                               } else
+                                       fmtprint(f, " %hhS:%N", l->n->left->sym, l->n->right);
+
                                if(l->next)
-                                       fmtprint(f, " %hhS:%N,", l->n->left->sym, l->n->right);
+                                       fmtstrcpy(f, ",");
                                else
-                                       fmtprint(f, " %hhS:%N ", l->n->left->sym, l->n->right);
+                                       fmtstrcpy(f, " ");
+                       }
                        return fmtstrcpy(f, "}");
                }
                // fallthrough
diff --git a/test/fixedbugs/bug396.dir/one.go b/test/fixedbugs/bug396.dir/one.go
new file mode 100644 (file)
index 0000000..7902a07
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2012 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 one
+
+type T struct { int }
+
+func New(i int) T { return T{i} }
diff --git a/test/fixedbugs/bug396.dir/two.go b/test/fixedbugs/bug396.dir/two.go
new file mode 100644 (file)
index 0000000..9b32508
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2012 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.
+
+// Use the functions in one.go so that the inlined
+// forms get type-checked.
+
+package two
+
+import "./one"
+
+func use() {
+       _ = one.New(1)
+}
\ No newline at end of file
diff --git a/test/fixedbugs/bug396.go b/test/fixedbugs/bug396.go
new file mode 100644 (file)
index 0000000..50af600
--- /dev/null
@@ -0,0 +1,7 @@
+// $G $D/$F.dir/one.go && $G $D/$F.dir/two.go
+
+// Copyright 2011 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 ignored