summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2011-07-27 19:31:11 -0400
committerRuss Cox <rsc@golang.org>2011-07-27 19:31:11 -0400
commitf310dca44c6aef5f54b0a7a0cd1f0a0a5cc57376 (patch)
tree8fb33abca5e6b16c4fbd272229db4f79379b66ac
parent1d817a6b164a7736a7b80a52603c0e2b67821a85 (diff)
downloadgo-f310dca44c6aef5f54b0a7a0cd1f0a0a5cc57376.tar.gz
gc: top-level closure bug
Fixes issue 2055. R=ken2 CC=golang-dev http://codereview.appspot.com/4816059
-rw-r--r--src/cmd/gc/closure.c5
-rw-r--r--src/cmd/gc/lex.c5
-rw-r--r--test/fixedbugs/bug355.go18
3 files changed, 26 insertions, 2 deletions
diff --git a/src/cmd/gc/closure.c b/src/cmd/gc/closure.c
index 7e7b40526..1261eefb7 100644
--- a/src/cmd/gc/closure.c
+++ b/src/cmd/gc/closure.c
@@ -84,6 +84,11 @@ typecheckclosure(Node *func, int top)
oldfn = curfn;
typecheck(&func->ntype, Etype);
func->type = func->ntype->type;
+ if(curfn == nil) {
+ xtop = list(xtop, func);
+ return;
+ }
+
if(func->type != T) {
curfn = func;
typechecklist(func->nbody, Etop);
diff --git a/src/cmd/gc/lex.c b/src/cmd/gc/lex.c
index 6845a8ecd..24a244e40 100644
--- a/src/cmd/gc/lex.c
+++ b/src/cmd/gc/lex.c
@@ -255,7 +255,7 @@ main(int argc, char *argv[])
resumecheckwidth();
for(l=xtop; l; l=l->next)
- if(l->n->op == ODCLFUNC) {
+ if(l->n->op == ODCLFUNC || l->n->op == OCLOSURE) {
curfn = l->n;
saveerrors();
typechecklist(l->n->nbody, Etop);
@@ -274,8 +274,9 @@ main(int argc, char *argv[])
while(closures) {
l = closures;
closures = nil;
- for(; l; l=l->next)
+ for(; l; l=l->next) {
funccompile(l->n, 1);
+ }
}
for(l=externdcl; l; l=l->next)
diff --git a/test/fixedbugs/bug355.go b/test/fixedbugs/bug355.go
new file mode 100644
index 000000000..a9cf0161b
--- /dev/null
+++ b/test/fixedbugs/bug355.go
@@ -0,0 +1,18 @@
+// $G $D/$F.go && $L $F.$A && ./$A.out
+
+// Copyright 2011 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.
+
+package main
+
+var f = func() int {
+ type S int
+ return 42
+}
+
+func main() {
+ if f() != 42 {
+ panic("BUG: bug355")
+ }
+}