]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: trailing semis are ok after valid fallthrough
authorRobert Griesemer <gri@golang.org>
Tue, 19 Apr 2016 21:18:59 +0000 (14:18 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 19 Apr 2016 21:44:44 +0000 (21:44 +0000)
Fixes #15376.

Change-Id: I9ece80f26b83be129671c961120c157da2ac0079
Reviewed-on: https://go-review.googlesource.com/22270
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/types/stmt.go
src/go/types/testdata/stmt0.src

index c6691851fb28902080f43e281ae9da66a715dac3..e301f711590ff4b916fce51d8652aa829c7864c9 100644 (file)
@@ -83,9 +83,19 @@ func (check *Checker) simpleStmt(s ast.Stmt) {
        }
 }
 
+func trimTrailingEmptyStmts(list []ast.Stmt) []ast.Stmt {
+       for i := len(list); i > 0; i-- {
+               if _, ok := list[i-1].(*ast.EmptyStmt); !ok {
+                       return list[:i]
+               }
+       }
+       return nil
+}
+
 func (check *Checker) stmtList(ctxt stmtContext, list []ast.Stmt) {
        ok := ctxt&fallthroughOk != 0
        inner := ctxt &^ fallthroughOk
+       list = trimTrailingEmptyStmts(list) // trailing empty statements are "invisible" to fallthrough analysis
        for i, s := range list {
                inner := inner
                if ok && i+1 == len(list) {
index e0d714dfb6f11078a6f97258d5c0823960fdf550..ac32ed7ba92f3297dbd8350b56b24d51b79a9c00 100644 (file)
@@ -531,16 +531,18 @@ func switches1() {
        case 1:
                fallthrough
        case 2:
-       default:
-               fallthrough
+               fallthrough; ; ; // trailing empty statements are ok
        case 3:
+       default:
+               fallthrough; ;
+       case 4:
                fallthrough /* ERROR "fallthrough statement out of place" */
        }
 
        var y interface{}
        switch y.(type) {
        case int:
-               fallthrough /* ERROR "fallthrough statement out of place" */
+               fallthrough /* ERROR "fallthrough statement out of place" */ ; ; ;
        default:
        }
 
@@ -554,7 +556,7 @@ func switches1() {
        switch x {
        case 0:
                goto L1
-               L1: fallthrough
+               L1: fallthrough; ;
        case 1:
                goto L2
                goto L3
@@ -576,9 +578,16 @@ func switches1() {
 
        switch x {
        case 0:
+               fallthrough; ;
+       case 1:
                {
                        fallthrough /* ERROR "fallthrough statement out of place" */
                }
+       case 2:
+               fallthrough
+       case 3:
+               fallthrough /* ERROR "fallthrough statement out of place" */
+               { /* empty block is not an empty statement */ }; ;
        default:
        }
 }