]> Cypherpunks repositories - gostls13.git/commitdiff
go: require { } around else block
authorRuss Cox <rsc@golang.org>
Thu, 14 Jul 2011 21:15:52 +0000 (17:15 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 14 Jul 2011 21:15:52 +0000 (17:15 -0400)
R=gri, ken, r
CC=golang-dev
https://golang.org/cl/4721044

doc/go_spec.html
src/cmd/gc/go.y
test/if.go

index 489ad4db3654c5c4e072add4ac4c6599a23061ca..986523871843b7f33be4e15bfeb73cc5cfa43d7a 100644 (file)
@@ -3762,7 +3762,7 @@ present, the "else" branch is executed.
 </p>
 
 <pre class="ebnf">
-IfStmt    = "if" [ SimpleStmt ";" ] Expression Block [ "else" Statement ] .
+IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
 </pre>
 
 <pre>
index 01a4e822fbe598b1596ab6082fe6ad8a575ff3c3..d3e363d370791339cad06a0566b07f2b7d108e8d 100644 (file)
@@ -1462,6 +1462,9 @@ non_dcl_stmt:
        }
 |      if_stmt LELSE stmt
        {
+               if($3->op != OIF && $3->op != OBLOCK)
+                       yyerror("missing { } after else");
+
                popdcl();
                $$ = $1;
                $$->nelse = list1($3);
index c1bb69d277f6594687a09016406239ef8a98543b..18a6715d7eb979bba97baf24585ea895d01d438c 100644 (file)
@@ -53,25 +53,28 @@ func main() {
        count = 0
        if true {
                count = count + 1
-       } else
+       } else {
                count = count - 1
+       }
        assertequal(count, 1, "if else true")
 
        count = 0
        if false {
                count = count + 1
-       } else
+       } else {
                count = count - 1
+       }
        assertequal(count, -1, "if else false")
 
        count = 0
-       if t:=1; false {
+       if t := 1; false {
                count = count + 1
                _ = t
                t := 7
                _ = t
-       } else
+       } else {
                count = count - t
+       }
        assertequal(count, -1, "if else false var")
 
        count = 0
@@ -80,8 +83,9 @@ func main() {
                count = count + 1
                t := 7
                _ = t
-       } else
+       } else {
                count = count - t
+       }
        _ = t
        assertequal(count, -1, "if else false var outside")
 }