Skip to content

Commit 46a6fd0

Browse files
randall77dmitshur
authored andcommitted
[release-branch.go1.15] cmd/compile: make go:notinheap error message friendlier for cgo
Update #40954 Change-Id: Ifaab7349631ccb12fc892882bbdf7f0ebf3d845f Reviewed-on: https://go-review.googlesource.com/c/go/+/251158 Run-TryBot: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Keith Randall <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/255338 Reviewed-by: Austin Clements <[email protected]>
1 parent 9698372 commit 46a6fd0

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ func (e *Escape) newLoc(n *Node, transient bool) *EscLocation {
10301030
Fatalf("e.curfn isn't set")
10311031
}
10321032
if n != nil && n.Type != nil && n.Type.NotInHeap() {
1033-
yyerrorl(n.Pos, "%v is go:notinheap; stack allocation disallowed", n.Type)
1033+
yyerrorl(n.Pos, "%v is incomplete (or unallocatable); stack allocation disallowed", n.Type)
10341034
}
10351035

10361036
n = canonicalNode(n)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,14 +696,14 @@ func convertop(srcConstant bool, src, dst *types.Type, why *string) Op {
696696
// (a) Disallow (*T) to (*U) where T is go:notinheap but U isn't.
697697
if src.IsPtr() && dst.IsPtr() && dst.Elem().NotInHeap() && !src.Elem().NotInHeap() {
698698
if why != nil {
699-
*why = fmt.Sprintf(":\n\t%v is go:notinheap, but %v is not", dst.Elem(), src.Elem())
699+
*why = fmt.Sprintf(":\n\t%v is incomplete (or unallocatable), but %v is not", dst.Elem(), src.Elem())
700700
}
701701
return OXXX
702702
}
703703
// (b) Disallow string to []T where T is go:notinheap.
704704
if src.IsString() && dst.IsSlice() && dst.Elem().NotInHeap() && (dst.Elem().Etype == types.Bytetype.Etype || dst.Elem().Etype == types.Runetype.Etype) {
705705
if why != nil {
706-
*why = fmt.Sprintf(":\n\t%v is go:notinheap", dst.Elem())
706+
*why = fmt.Sprintf(":\n\t%v is incomplete (or unallocatable)", dst.Elem())
707707
}
708708
return OXXX
709709
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,10 @@ func typecheck1(n *Node, top int) (res *Node) {
471471
return n
472472
}
473473
if l.Type.NotInHeap() {
474-
yyerror("go:notinheap map key not allowed")
474+
yyerror("incomplete (or unallocatable) map key not allowed")
475475
}
476476
if r.Type.NotInHeap() {
477-
yyerror("go:notinheap map value not allowed")
477+
yyerror("incomplete (or unallocatable) map value not allowed")
478478
}
479479

480480
setTypeNode(n, types.NewMap(l.Type, r.Type))
@@ -491,7 +491,7 @@ func typecheck1(n *Node, top int) (res *Node) {
491491
return n
492492
}
493493
if l.Type.NotInHeap() {
494-
yyerror("chan of go:notinheap type not allowed")
494+
yyerror("chan of incomplete (or unallocatable) type not allowed")
495495
}
496496

497497
setTypeNode(n, types.NewChan(l.Type, n.TChanDir()))

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ opswitch:
640640
// x = append(...)
641641
r := n.Right
642642
if r.Type.Elem().NotInHeap() {
643-
yyerror("%v is go:notinheap; heap allocation disallowed", r.Type.Elem())
643+
yyerror("%v can't be allocated in Go; it is incomplete (or unallocatable)", r.Type.Elem())
644644
}
645645
switch {
646646
case isAppendOfMake(r):
@@ -1152,7 +1152,7 @@ opswitch:
11521152

11531153
case ONEW:
11541154
if n.Type.Elem().NotInHeap() {
1155-
yyerror("%v is go:notinheap; heap allocation disallowed", n.Type.Elem())
1155+
yyerror("%v can't be allocated in Go; it is incomplete (or unallocatable)", n.Type.Elem())
11561156
}
11571157
if n.Esc == EscNone {
11581158
if n.Type.Elem().Width >= maxImplicitStackVarSize {
@@ -1323,7 +1323,7 @@ opswitch:
13231323
}
13241324
t := n.Type
13251325
if t.Elem().NotInHeap() {
1326-
yyerror("%v is go:notinheap; heap allocation disallowed", t.Elem())
1326+
yyerror("%v can't be allocated in Go; it is incomplete (or unallocatable)", t.Elem())
13271327
}
13281328
if n.Esc == EscNone {
13291329
if !isSmallMakeSlice(n) {
@@ -1400,7 +1400,7 @@ opswitch:
14001400

14011401
t := n.Type
14021402
if t.Elem().NotInHeap() {
1403-
yyerror("%v is go:notinheap; heap allocation disallowed", t.Elem())
1403+
yyerror("%v can't be allocated in Go; it is incomplete (or unallocatable)", t.Elem())
14041404
}
14051405

14061406
length := conv(n.Left, types.Types[TINT])

test/notinheap.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ type embed3 struct { // ERROR "must be go:notinheap"
2323
x [1]nih
2424
}
2525

26-
type embed4 map[nih]int // ERROR "go:notinheap map key not allowed"
26+
type embed4 map[nih]int // ERROR "incomplete \(or unallocatable\) map key not allowed"
2727

28-
type embed5 map[int]nih // ERROR "go:notinheap map value not allowed"
28+
type embed5 map[int]nih // ERROR "incomplete \(or unallocatable\) map value not allowed"
2929

30-
type emebd6 chan nih // ERROR "chan of go:notinheap type not allowed"
30+
type emebd6 chan nih // ERROR "chan of incomplete \(or unallocatable\) type not allowed"
3131

3232
type okay1 *nih
3333

@@ -64,8 +64,8 @@ var sink interface{}
6464

6565
func i() {
6666
sink = new(t1) // no error
67-
sink = (*t2)(new(t1)) // ERROR "cannot convert(.|\n)*t2 is go:notinheap"
68-
sink = (*t2)(new(struct{ x int })) // ERROR "cannot convert(.|\n)*t2 is go:notinheap"
69-
sink = []t3("foo") // ERROR "cannot convert(.|\n)*t3 is go:notinheap"
70-
sink = []t4("bar") // ERROR "cannot convert(.|\n)*t4 is go:notinheap"
67+
sink = (*t2)(new(t1)) // ERROR "cannot convert(.|\n)*t2 is incomplete \(or unallocatable\)"
68+
sink = (*t2)(new(struct{ x int })) // ERROR "cannot convert(.|\n)*t2 is incomplete \(or unallocatable\)"
69+
sink = []t3("foo") // ERROR "cannot convert(.|\n)*t3 is incomplete \(or unallocatable\)"
70+
sink = []t4("bar") // ERROR "cannot convert(.|\n)*t4 is incomplete \(or unallocatable\)"
7171
}

test/notinheap2.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var x nih
2020
// Stack variables are not okay.
2121

2222
func f() {
23-
var y nih // ERROR "nih is go:notinheap; stack allocation disallowed"
23+
var y nih // ERROR "nih is incomplete \(or unallocatable\); stack allocation disallowed"
2424
x = y
2525
}
2626

@@ -34,13 +34,13 @@ var w []nih
3434
var n int
3535

3636
func g() {
37-
y = new(nih) // ERROR "heap allocation disallowed"
38-
y2 = new(struct{ x nih }) // ERROR "heap allocation disallowed"
39-
y3 = new([1]nih) // ERROR "heap allocation disallowed"
40-
z = make([]nih, 1) // ERROR "heap allocation disallowed"
41-
z = append(z, x) // ERROR "heap allocation disallowed"
37+
y = new(nih) // ERROR "can't be allocated in Go"
38+
y2 = new(struct{ x nih }) // ERROR "can't be allocated in Go"
39+
y3 = new([1]nih) // ERROR "can't be allocated in Go"
40+
z = make([]nih, 1) // ERROR "can't be allocated in Go"
41+
z = append(z, x) // ERROR "can't be allocated in Go"
4242
// Test for special case of OMAKESLICECOPY
43-
x := make([]nih, n) // ERROR "heap allocation disallowed"
43+
x := make([]nih, n) // ERROR "can't be allocated in Go"
4444
copy(x, z)
4545
z = x
4646
}

0 commit comments

Comments
 (0)