Commit 58e19aa4 authored by Russ Cox's avatar Russ Cox

go: require { } around else block

R=gri, ken, r
CC=golang-dev
https://golang.org/cl/4721044
parent 3f53475c
...@@ -3762,7 +3762,7 @@ present, the "else" branch is executed. ...@@ -3762,7 +3762,7 @@ present, the "else" branch is executed.
</p> </p>
<pre class="ebnf"> <pre class="ebnf">
IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" Statement ] . IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
</pre> </pre>
<pre> <pre>
......
...@@ -1462,6 +1462,9 @@ non_dcl_stmt: ...@@ -1462,6 +1462,9 @@ non_dcl_stmt:
} }
| if_stmt LELSE stmt | if_stmt LELSE stmt
{ {
if($3->op != OIF && $3->op != OBLOCK)
yyerror("missing { } after else");
popdcl(); popdcl();
$$ = $1; $$ = $1;
$$->nelse = list1($3); $$->nelse = list1($3);
......
...@@ -53,25 +53,28 @@ func main() { ...@@ -53,25 +53,28 @@ func main() {
count = 0 count = 0
if true { if true {
count = count + 1 count = count + 1
} else } else {
count = count - 1 count = count - 1
}
assertequal(count, 1, "if else true") assertequal(count, 1, "if else true")
count = 0 count = 0
if false { if false {
count = count + 1 count = count + 1
} else } else {
count = count - 1 count = count - 1
}
assertequal(count, -1, "if else false") assertequal(count, -1, "if else false")
count = 0 count = 0
if t:=1; false { if t := 1; false {
count = count + 1 count = count + 1
_ = t _ = t
t := 7 t := 7
_ = t _ = t
} else } else {
count = count - t count = count - t
}
assertequal(count, -1, "if else false var") assertequal(count, -1, "if else false var")
count = 0 count = 0
...@@ -80,8 +83,9 @@ func main() { ...@@ -80,8 +83,9 @@ func main() {
count = count + 1 count = count + 1
t := 7 t := 7
_ = t _ = t
} else } else {
count = count - t count = count - t
}
_ = t _ = t
assertequal(count, -1, "if else false var outside") assertequal(count, -1, "if else false var outside")
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment