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;
if(n->initorder == InitPending) {
if(n->class == PFUNC)
return;
-
+
// if there have already been errors printed,
// those errors probably confused us and
// there might not be a loop. let the user
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) {
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;
}
--- /dev/null
+// 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() {}