]> Cypherpunks repositories - gostls13.git/commitdiff
gc: top-level closure bug
authorRuss Cox <rsc@golang.org>
Wed, 27 Jul 2011 23:31:11 +0000 (19:31 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 27 Jul 2011 23:31:11 +0000 (19:31 -0400)
Fixes #2055.

R=ken2
CC=golang-dev
https://golang.org/cl/4816059

src/cmd/gc/closure.c
src/cmd/gc/lex.c
test/fixedbugs/bug355.go [new file with mode: 0644]

index 7e7b405260cf48307b8a74eb803037f70d4c6f0a..1261eefb7d64adba70156c1e3c2a5eae0cf81abe 100644 (file)
@@ -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);
index 6845a8ecd655b686bd2cc496072ae5395f51753e..24a244e40fec680c72354bdc98e029cfb40eda80 100644 (file)
@@ -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 (file)
index 0000000..a9cf016
--- /dev/null
@@ -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")
+       }
+}