Skip to content

Commit 2c95e3a

Browse files
committed
cmd/compile: use clearer error message for stuct literal
This CL changes "T literal.M" error message to "T{...}.M". It's clearer expression and focusing user on actual issue. Updates #38745 Change-Id: I84b455a86742f37e0bde5bf390aa02984eecc3c9 Reviewed-on: https://go-review.googlesource.com/c/go/+/253677 Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent d7384f3 commit 2c95e3a

27 files changed

+379
-353
lines changed

src/cmd/compile/internal/gc/fmt.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ func (n *Node) exprfmt(s fmt.State, prec int, mode fmtMode) {
14071407
return
14081408
}
14091409
if n.Right != nil {
1410-
mode.Fprintf(s, "%v literal", n.Right)
1410+
mode.Fprintf(s, "%v{%s}", n.Right, ellipsisIf(n.List.Len() != 0))
14111411
return
14121412
}
14131413

@@ -1421,7 +1421,7 @@ func (n *Node) exprfmt(s fmt.State, prec int, mode fmtMode) {
14211421

14221422
case OSTRUCTLIT, OARRAYLIT, OSLICELIT, OMAPLIT:
14231423
if mode == FErr {
1424-
mode.Fprintf(s, "%v literal", n.Type)
1424+
mode.Fprintf(s, "%v{%s}", n.Type, ellipsisIf(n.List.Len() != 0))
14251425
return
14261426
}
14271427
mode.Fprintf(s, "(%v{ %.v })", n.Type, n.List)
@@ -1934,3 +1934,10 @@ func indent(s fmt.State) {
19341934
fmt.Fprint(s, ". ")
19351935
}
19361936
}
1937+
1938+
func ellipsisIf(b bool) string {
1939+
if b {
1940+
return "..."
1941+
}
1942+
return ""
1943+
}

test/alias2.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ var _ A0 = T0{}
4646
var _ T0 = A0{}
4747

4848
// But aliases and original types cannot be used with new types based on them.
49-
var _ N0 = T0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
50-
var _ N0 = A0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
49+
var _ N0 = T0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|incompatible type"
50+
var _ N0 = A0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|incompatible type"
5151

5252
var _ A5 = Value{}
5353

@@ -82,10 +82,10 @@ func _() {
8282
var _ A0 = T0{}
8383
var _ T0 = A0{}
8484

85-
var _ N0 = T0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
86-
var _ N0 = A0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
85+
var _ N0 = T0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|incompatible type"
86+
var _ N0 = A0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|incompatible type"
8787

88-
var _ A5 = Value{} // ERROR "cannot use reflect\.Value literal \(type reflect.Value\) as type A5 in assignment|incompatible type"
88+
var _ A5 = Value{} // ERROR "cannot use reflect\.Value{} \(type reflect.Value\) as type A5 in assignment|incompatible type"
8989
}
9090

9191
// Invalid type alias declarations.

test/ddd1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
_ = sum(1.0, 2.0)
2020
_ = sum(1.5) // ERROR "integer"
2121
_ = sum("hello") // ERROR ".hello. .type untyped string. as type int|incompatible"
22-
_ = sum([]int{1}) // ERROR "\[\]int literal.*as type int|incompatible"
22+
_ = sum([]int{1}) // ERROR "\[\]int{...}.*as type int|incompatible"
2323
)
2424

2525
func sum3(int, int, int) int { return 0 }

test/escape2.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ type Bar struct {
118118
}
119119

120120
func NewBar() *Bar {
121-
return &Bar{42, nil} // ERROR "&Bar literal escapes to heap$"
121+
return &Bar{42, nil} // ERROR "&Bar{...} escapes to heap$"
122122
}
123123

124124
func NewBarp(x *int) *Bar { // ERROR "leaking param: x$"
125-
return &Bar{42, x} // ERROR "&Bar literal escapes to heap$"
125+
return &Bar{42, x} // ERROR "&Bar{...} escapes to heap$"
126126
}
127127

128128
func NewBarp2(x *int) *Bar { // ERROR "x does not escape$"
129-
return &Bar{*x, nil} // ERROR "&Bar literal escapes to heap$"
129+
return &Bar{*x, nil} // ERROR "&Bar{...} escapes to heap$"
130130
}
131131

132132
func (b *Bar) NoLeak() int { // ERROR "b does not escape$"
@@ -173,7 +173,7 @@ type Bar2 struct {
173173
}
174174

175175
func NewBar2() *Bar2 {
176-
return &Bar2{[12]int{42}, nil} // ERROR "&Bar2 literal escapes to heap$"
176+
return &Bar2{[12]int{42}, nil} // ERROR "&Bar2{...} escapes to heap$"
177177
}
178178

179179
func (b *Bar2) NoLeak() int { // ERROR "b does not escape$"
@@ -539,7 +539,7 @@ func foo72b() [10]*int {
539539

540540
// issue 2145
541541
func foo73() {
542-
s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape$"
542+
s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$"
543543
for _, v := range s {
544544
vv := v
545545
// actually just escapes its scope
@@ -550,7 +550,7 @@ func foo73() {
550550
}
551551

552552
func foo731() {
553-
s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape$"
553+
s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$"
554554
for _, v := range s {
555555
vv := v // ERROR "moved to heap: vv$"
556556
// actually just escapes its scope
@@ -562,7 +562,7 @@ func foo731() {
562562
}
563563

564564
func foo74() {
565-
s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape$"
565+
s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$"
566566
for _, v := range s {
567567
vv := v
568568
// actually just escapes its scope
@@ -574,7 +574,7 @@ func foo74() {
574574
}
575575

576576
func foo74a() {
577-
s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape$"
577+
s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$"
578578
for _, v := range s {
579579
vv := v // ERROR "moved to heap: vv$"
580580
// actually just escapes its scope
@@ -589,7 +589,7 @@ func foo74a() {
589589
// issue 3975
590590
func foo74b() {
591591
var array [3]func()
592-
s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape$"
592+
s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$"
593593
for i, v := range s {
594594
vv := v
595595
// actually just escapes its scope
@@ -601,7 +601,7 @@ func foo74b() {
601601

602602
func foo74c() {
603603
var array [3]func()
604-
s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape$"
604+
s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$"
605605
for i, v := range s {
606606
vv := v // ERROR "moved to heap: vv$"
607607
// actually just escapes its scope
@@ -759,15 +759,15 @@ type LimitedFooer struct {
759759
}
760760

761761
func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r$"
762-
return &LimitedFooer{r, n} // ERROR "&LimitedFooer literal escapes to heap$"
762+
return &LimitedFooer{r, n} // ERROR "&LimitedFooer{...} escapes to heap$"
763763
}
764764

765765
func foo90(x *int) map[*int]*int { // ERROR "leaking param: x$"
766-
return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap$"
766+
return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} escapes to heap$"
767767
}
768768

769769
func foo91(x *int) map[*int]*int { // ERROR "leaking param: x$"
770-
return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap$"
770+
return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} escapes to heap$"
771771
}
772772

773773
func foo92(x *int) [2]*int { // ERROR "leaking param: x to result ~r1 level=0$"
@@ -870,28 +870,28 @@ func foo106(x *int) { // ERROR "leaking param: x$"
870870
}
871871

872872
func foo107(x *int) map[*int]*int { // ERROR "leaking param: x$"
873-
return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap$"
873+
return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} escapes to heap$"
874874
}
875875

876876
func foo108(x *int) map[*int]*int { // ERROR "leaking param: x$"
877-
return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap$"
877+
return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} escapes to heap$"
878878
}
879879

880880
func foo109(x *int) *int { // ERROR "leaking param: x$"
881-
m := map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal does not escape$"
881+
m := map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} does not escape$"
882882
for k, _ := range m {
883883
return k
884884
}
885885
return nil
886886
}
887887

888888
func foo110(x *int) *int { // ERROR "leaking param: x$"
889-
m := map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal does not escape$"
889+
m := map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} does not escape$"
890890
return m[nil]
891891
}
892892

893893
func foo111(x *int) *int { // ERROR "leaking param: x to result ~r1 level=0"
894-
m := []*int{x} // ERROR "\[\]\*int literal does not escape$"
894+
m := []*int{x} // ERROR "\[\]\*int{...} does not escape$"
895895
return m[0]
896896
}
897897

@@ -906,7 +906,7 @@ func foo113(x *int) *int { // ERROR "leaking param: x to result ~r1 level=0$"
906906
}
907907

908908
func foo114(x *int) *int { // ERROR "leaking param: x to result ~r1 level=0$"
909-
m := &Bar{ii: x} // ERROR "&Bar literal does not escape$"
909+
m := &Bar{ii: x} // ERROR "&Bar{...} does not escape$"
910910
return m.ii
911911
}
912912

@@ -1343,8 +1343,8 @@ func foo140() interface{} {
13431343
X string
13441344
T *T
13451345
}
1346-
t := &T{} // ERROR "&T literal escapes to heap$"
1347-
return U{ // ERROR "U literal escapes to heap$"
1346+
t := &T{} // ERROR "&T{} escapes to heap$"
1347+
return U{ // ERROR "U{...} escapes to heap$"
13481348
X: t.X,
13491349
T: t,
13501350
}
@@ -1530,7 +1530,7 @@ type V struct {
15301530
}
15311531

15321532
func NewV(u U) *V { // ERROR "leaking param: u$"
1533-
return &V{u.String()} // ERROR "&V literal escapes to heap$"
1533+
return &V{u.String()} // ERROR "&V{...} escapes to heap$"
15341534
}
15351535

15361536
func foo152() {
@@ -1571,21 +1571,21 @@ type Lit struct {
15711571
func ptrlitNoescape() {
15721572
// Both literal and element do not escape.
15731573
i := 0
1574-
x := &Lit{&i} // ERROR "&Lit literal does not escape$"
1574+
x := &Lit{&i} // ERROR "&Lit{...} does not escape$"
15751575
_ = x
15761576
}
15771577

15781578
func ptrlitNoEscape2() {
15791579
// Literal does not escape, but element does.
15801580
i := 0 // ERROR "moved to heap: i$"
1581-
x := &Lit{&i} // ERROR "&Lit literal does not escape$"
1581+
x := &Lit{&i} // ERROR "&Lit{...} does not escape$"
15821582
sink = *x
15831583
}
15841584

15851585
func ptrlitEscape() {
15861586
// Both literal and element escape.
15871587
i := 0 // ERROR "moved to heap: i$"
1588-
x := &Lit{&i} // ERROR "&Lit literal escapes to heap$"
1588+
x := &Lit{&i} // ERROR "&Lit{...} escapes to heap$"
15891589
sink = x
15901590
}
15911591

@@ -1760,18 +1760,18 @@ func stringtoslicerune2() {
17601760
}
17611761

17621762
func slicerunetostring0() {
1763-
r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape$"
1763+
r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$"
17641764
s := string(r) // ERROR "string\(r\) does not escape$"
17651765
_ = s
17661766
}
17671767

17681768
func slicerunetostring1() string {
1769-
r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape$"
1769+
r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$"
17701770
return string(r) // ERROR "string\(r\) escapes to heap$"
17711771
}
17721772

17731773
func slicerunetostring2() {
1774-
r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape$"
1774+
r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$"
17751775
sink = string(r) // ERROR "string\(r\) escapes to heap$"
17761776
}
17771777

0 commit comments

Comments
 (0)