]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/gc: ignore declarations of types for goto validation
authorDaniel Morsing <daniel.morsing@gmail.com>
Wed, 29 Apr 2015 09:51:42 +0000 (10:51 +0100)
committerDaniel Morsing <daniel.morsing@gmail.com>
Fri, 15 May 2015 16:03:47 +0000 (16:03 +0000)
Fixes #8042.

Change-Id: I75080f24104256065fd73b07a13c5b8e7d6da94c
Reviewed-on: https://go-review.googlesource.com/9442
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/internal/gc/dcl.go
src/cmd/internal/gc/gen.go
src/cmd/internal/gc/go.go
test/goto.go

index 85a33bec3fadb3c2d881a28c4580ead00f2cf6e0..58b69ab8f990bc86784d800e4bb8d33dcbbbbfaf 100644 (file)
@@ -65,6 +65,9 @@ func popdcl() {
                }
                s = Pkglookup(d.Name, d.Pkg)
                lno = int(s.Lastlineno)
+               if s.Def != nil {
+                       d.whyPushed = s.Def.Op
+               }
                dcopy(s, d)
                d.Lastlineno = int32(lno)
                if dflag() {
index cd0e650ca99c6c1cd9e47983ce98941ea7a36628..bba04d41ad701326bfe2d71f868f71f8b79e9bd9 100644 (file)
@@ -159,6 +159,21 @@ func checkgoto(from *Node, to *Node) {
                fs = fs.Link
        }
        if fs != to.Sym {
+               // more declarations at label than at goto.
+               // figure out if they are all types.
+               ts := to.Sym
+               ntt := nt
+               for ; ntt > nf; ntt-- {
+                       if ts.whyPushed != OTYPE {
+                               break
+                       }
+                       ts = ts.Link
+               }
+               // all types, nothing to see here.
+               if ntt == nf {
+                       return
+               }
+
                lno := int(lineno)
                setlineno(from)
 
@@ -168,11 +183,11 @@ func checkgoto(from *Node, to *Node) {
                var block *Sym
 
                var dcl *Sym
-               ts := to.Sym
+               ts = to.Sym
                for ; nt > nf; nt-- {
                        if ts.Pkg == nil {
                                block = ts
-                       } else {
+                       } else if ts.whyPushed != OTYPE {
                                dcl = ts
                        }
                        ts = ts.Link
@@ -181,7 +196,7 @@ func checkgoto(from *Node, to *Node) {
                for ts != fs {
                        if ts.Pkg == nil {
                                block = ts
-                       } else {
+                       } else if ts.whyPushed != OTYPE {
                                dcl = ts
                        }
                        ts = ts.Link
index 31692bdf00ce3a6d964c337d557568c241339965..4800218c95fcc2aa5683b99ef39eedc08d83d961 100644 (file)
@@ -111,6 +111,7 @@ type Sym struct {
        Uniqgen   uint32
        Importdef *Pkg   // where imported definition was found
        Linkname  string // link name
+       whyPushed uint8  // why this symbol pushed onto dclstack. Same as Node.Op. Used by goto validation
 
        // saved and restored by dcopy
        Pkg        *Pkg
index ca477b3d0c3beb77025ac31326b661d7dec8b43f..c626f3d1c110d2905f3d4a48af2d6bc17ea5bbae 100644 (file)
@@ -536,3 +536,19 @@ func _() {
                goto L // ERROR "goto L jumps into block starting at LINE-4|goto jumps into block"
        }
 }
+
+// issue 8042
+func _() {
+       goto L
+       type a int
+       L:
+}
+
+// make sure we only complain about variable declarations.
+func _() {
+       goto L // ERROR "goto L jumps over declaration of x at LINE+2|goto jumps over declaration"
+       type a int
+       x := 1  // GCCGO_ERROR "defined here"
+       _ = x
+L:
+}