Skip to content

Commit 319131f

Browse files
cmd/gc: fix inlining bug for composite literals in if statements.
Fixes #4230. R=golang-dev, rsc CC=golang-dev, remy https://golang.org/cl/6640056
1 parent 77e42e2 commit 319131f

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

src/cmd/gc/fmt.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ exprfmt(Fmt *f, Node *n, int prec)
11611161
case OCOMPLIT:
11621162
if(fmtmode == FErr)
11631163
return fmtstrcpy(f, "composite literal");
1164-
return fmtprint(f, "%N{ %,H }", n->right, n->list);
1164+
return fmtprint(f, "(%N{ %,H })", n->right, n->list);
11651165

11661166
case OPTRLIT:
11671167
if(fmtmode == FExp && n->left->implicit)
@@ -1172,8 +1172,8 @@ exprfmt(Fmt *f, Node *n, int prec)
11721172
if(fmtmode == FExp) { // requires special handling of field names
11731173
if(n->implicit)
11741174
fmtstrcpy(f, "{");
1175-
else
1176-
fmtprint(f, "%T{", n->type);
1175+
else
1176+
fmtprint(f, "(%T{", n->type);
11771177
for(l=n->list; l; l=l->next) {
11781178
// another special case: if n->left is an embedded field of builtin type,
11791179
// it needs to be non-qualified. Can't figure that out in %S, so do it here
@@ -1190,6 +1190,8 @@ exprfmt(Fmt *f, Node *n, int prec)
11901190
else
11911191
fmtstrcpy(f, " ");
11921192
}
1193+
if(!n->implicit)
1194+
return fmtstrcpy(f, "})");
11931195
return fmtstrcpy(f, "}");
11941196
}
11951197
// fallthrough
@@ -1200,7 +1202,7 @@ exprfmt(Fmt *f, Node *n, int prec)
12001202
return fmtprint(f, "%T literal", n->type);
12011203
if(fmtmode == FExp && n->implicit)
12021204
return fmtprint(f, "{ %,H }", n->list);
1203-
return fmtprint(f, "%T{ %,H }", n->type, n->list);
1205+
return fmtprint(f, "(%T{ %,H })", n->type, n->list);
12041206

12051207
case OKEY:
12061208
if(n->left && n->right)

test/fixedbugs/bug465.dir/a.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2012 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
type T struct{ A, B int }
8+
9+
type A []int
10+
11+
type M map[int]int
12+
13+
func F1() int {
14+
if (T{1, 2}) == (T{3, 4}) {
15+
return 1
16+
}
17+
return 0
18+
}
19+
20+
func F2() int {
21+
if (M{1: 2}) == nil {
22+
return 1
23+
}
24+
return 0
25+
}
26+
27+
func F3() int {
28+
if nil == (A{}) {
29+
return 1
30+
}
31+
return 0
32+
}
33+
34+
func F4() int {
35+
if a := (A{}); a == nil {
36+
return 1
37+
}
38+
return 0
39+
}
40+
41+
func F5() int {
42+
for k, v := range (M{1: 2}) {
43+
return v - k
44+
}
45+
return 0
46+
}
47+
48+
func F6() int {
49+
switch a := (T{1, 1}); a == (T{1, 2}) {
50+
default:
51+
return 1
52+
}
53+
return 0
54+
}
55+
56+
func F7() int {
57+
for m := (M{}); len(m) < (T{1, 2}).A; m[1] = (A{1})[0] {
58+
return 1
59+
}
60+
return 0
61+
}

test/fixedbugs/bug465.dir/b.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import "./a"
8+
9+
func main() {
10+
for _, f := range []func() int{
11+
a.F1, a.F2, a.F3, a.F4,
12+
a.F5, a.F6, a.F7} {
13+
if f() > 1 {
14+
panic("f() > 1")
15+
}
16+
}
17+
}

test/fixedbugs/bug465.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rundir
2+
3+
// Copyright 2012 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Issue 4230: inlining bug for composite literal in
8+
// if, for, switch statements.
9+
10+
package ignored

0 commit comments

Comments
 (0)