Skip to content

Commit 7cfa831

Browse files
committed
cmd/gc: fix issue with method wrappers not having escape analysis run on them.
Escape analysis needs the right curfn value on a dclfunc node, otherwise it will not analyze the function. When generating method value wrappers, we forgot to set the curfn correctly. Fixes #5753. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/10383048
1 parent 428ea68 commit 7cfa831

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/cmd/gc/closure.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ typecheckpartialcall(Node *fn, Node *sym)
280280
static Node*
281281
makepartialcall(Node *fn, Type *t0, Node *meth)
282282
{
283-
Node *ptr, *n, *fld, *call, *xtype, *xfunc, *cv;
283+
Node *ptr, *n, *fld, *call, *xtype, *xfunc, *cv, *savecurfn;
284284
Type *rcvrtype, *basetype, *t;
285285
NodeList *body, *l, *callargs, *retargs;
286286
char *p;
@@ -304,13 +304,17 @@ makepartialcall(Node *fn, Type *t0, Node *meth)
304304
if(sym->flags & SymUniq)
305305
return sym->def;
306306
sym->flags |= SymUniq;
307+
308+
savecurfn = curfn;
309+
curfn = N;
307310

308311
xtype = nod(OTFUNC, N, N);
309312
i = 0;
310313
l = nil;
311314
callargs = nil;
312315
ddd = 0;
313316
xfunc = nod(ODCLFUNC, N, N);
317+
curfn = xfunc;
314318
for(t = getinargx(t0)->type; t; t = t->down) {
315319
snprint(namebuf, sizeof namebuf, "a%d", i++);
316320
n = newname(lookup(namebuf));
@@ -385,6 +389,7 @@ makepartialcall(Node *fn, Type *t0, Node *meth)
385389
typecheck(&xfunc, Etop);
386390
sym->def = xfunc;
387391
xtop = list(xtop, xfunc);
392+
curfn = savecurfn;
388393

389394
return xfunc;
390395
}

test/fixedbugs/issue5753.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run
2+
3+
// Copyright 2013 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 5753: bad typecheck info causes escape analysis to
8+
// not run on method thunks.
9+
10+
package main
11+
12+
type Thing struct{}
13+
14+
func (t *Thing) broken(s string) []string {
15+
foo := [1]string{s}
16+
return foo[:]
17+
}
18+
19+
func main() {
20+
t := &Thing{}
21+
22+
f := t.broken
23+
s := f("foo")
24+
_ = f("bar")
25+
if s[0] != "foo" {
26+
panic(`s[0] != "foo"`)
27+
}
28+
29+
}

0 commit comments

Comments
 (0)