1
0
mirror of https://github.com/golang/go synced 2024-11-25 09:57:57 -07:00

cmd/gc: do not corrupt init() with initializers of _ in closures.

Fixes #5607.

R=golang-dev, daniel.morsing, r, dsymonds
CC=golang-dev
https://golang.org/cl/9952043
This commit is contained in:
Rémy Oudompheng 2013-06-02 23:54:34 +02:00
parent 85046d3e52
commit 0d0d57ccfe
2 changed files with 43 additions and 3 deletions

View File

@ -50,8 +50,11 @@ init1(Node *n, NodeList **out)
case PFUNC:
break;
default:
if(isblank(n) && n->defn != N && n->defn->initorder == InitNotStarted) {
if(isblank(n) && n->curfn == N && n->defn != N && n->defn->initorder == InitNotStarted) {
// blank names initialization is part of init() but not
// when they are inside a function.
n->defn->initorder = InitDone;
if(debug['%']) dump("nonstatic", n->defn);
*out = list(*out, n->defn);
}
return;
@ -128,7 +131,7 @@ init1(Node *n, NodeList **out)
if(debug['j'])
print("%S\n", n->sym);
if(!staticinit(n, out)) {
if(debug['%']) dump("nonstatic", n->defn);
if(debug['%']) dump("nonstatic", n->defn);
*out = list(*out, n->defn);
}
} else if(0) {
@ -149,6 +152,7 @@ if(debug['%']) dump("nonstatic", n->defn);
n->defn->initorder = InitDone;
for(l=n->defn->rlist; l; l=l->next)
init1(l->n, out);
if(debug['%']) dump("nonstatic", n->defn);
*out = list(*out, n->defn);
break;
}

View File

@ -0,0 +1,36 @@
// run
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Issue 5607: generation of init() function incorrectly
// uses initializers of blank variables inside closures.
package main
var Test = func() {
var mymap = map[string]string{"a": "b"}
var innerTest = func() {
// Used to crash trying to compile this line as
// part of init() (funcdepth mismatch).
var _, x = mymap["a"]
println(x)
}
innerTest()
}
var Test2 = func() {
// The following initializer should not be part of init()
// The compiler used to generate a call to Panic() in init().
var _, x = Panic()
_ = x
}
func Panic() (int, int) {
panic("omg")
return 1, 2
}
func main() {}