Skip to content

Commit

Permalink
cmd/gc: fix escape analysis for slice of array
Browse files Browse the repository at this point in the history
Fixes golang#7931.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/100390044
  • Loading branch information
rsc committed May 12, 2014
1 parent 7e8bc47 commit f078711
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/cmd/gc/esc.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,8 @@ escassign(EscState *e, Node *dst, Node *src)
case ODOTTYPE:
case ODOTTYPE2:
case OSLICE:
case OSLICEARR:
case OSLICE3:
case OSLICEARR:
case OSLICE3ARR:
// Conversions, field access, slice all preserve the input value.
escassign(e, dst, src->left);
Expand Down Expand Up @@ -1155,6 +1155,10 @@ escwalk(EscState *e, int level, Node *dst, Node *src)
break;

case ODOT:
case OSLICE:
case OSLICEARR:
case OSLICE3:
case OSLICE3ARR:
escwalk(e, level, dst, src->left);
break;

Expand All @@ -1164,7 +1168,6 @@ escwalk(EscState *e, int level, Node *dst, Node *src)
break;
}
// fall through
case OSLICE:
case ODOTPTR:
case OINDEXMAP:
case OIND:
Expand Down
32 changes: 32 additions & 0 deletions test/escape2.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,3 +1411,35 @@ func foo150(x ...byte) { // ERROR "leaking param: x"
func bar150() {
foo150(1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
}

// issue 7931: bad handling of slice of array

var save151 *int

func foo151(x *int) { // ERROR "leaking param: x"
save151 = x
}

func bar151() {
var a [64]int // ERROR "moved to heap: a"
a[4] = 101
foo151(&(&a)[4:8][0]) // ERROR "&\(&a\)\[4:8\]\[0\] escapes to heap" "&a escapes to heap"
}

func bar151b() {
var a [10]int // ERROR "moved to heap: a"
b := a[:] // ERROR "a escapes to heap"
foo151(&b[4:8][0]) // ERROR "&b\[4:8\]\[0\] escapes to heap"
}

func bar151c() {
var a [64]int // ERROR "moved to heap: a"
a[4] = 101
foo151(&(&a)[4:8:8][0]) // ERROR "&\(&a\)\[4:8:8\]\[0\] escapes to heap" "&a escapes to heap"
}

func bar151d() {
var a [10]int // ERROR "moved to heap: a"
b := a[:] // ERROR "a escapes to heap"
foo151(&b[4:8:8][0]) // ERROR "&b\[4:8:8\]\[0\] escapes to heap"
}

0 comments on commit f078711

Please sign in to comment.