Skip to content

Commit 2dbb9e1

Browse files
authored
[spec/statement] Improve goto docs (#4314)
Make 2 examples runnable. Add `writeln` calls to show `switch` execution path. `goto` cannot skip *within its scope*. Fixes #4310. Add *Best Practice*.
1 parent 1a68865 commit 2dbb9e1

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

spec/statement.dd

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,13 +1796,17 @@ $(GNAME GotoStatement):
17961796

17971797
$(P `goto` transfers to the statement labeled with $(I Identifier).)
17981798

1799+
$(SPEC_RUNNABLE_EXAMPLE_RUN
17991800
---
1800-
if (foo)
1801+
int x = 1;
1802+
if (x)
18011803
goto L1;
1804+
18021805
x = 3;
18031806
L1:
1804-
x++;
1807+
assert(x == 1);
18051808
---
1809+
)
18061810

18071811
$(P The second form, $(CODE goto default;), transfers to the innermost $(GLINK
18081812
DefaultStatement) of an enclosing $(GLINK SwitchStatement).)
@@ -1816,26 +1820,38 @@ DefaultStatement) of an enclosing $(GLINK SwitchStatement).)
18161820
$(GLINK SwitchStatement)
18171821
with a matching *Expression*.)
18181822

1823+
$(SPEC_RUNNABLE_EXAMPLE_RUN
18191824
---
1825+
int x = 5;
1826+
18201827
switch (x)
18211828
{
18221829
case 3:
1830+
writeln("case 3");
18231831
goto case;
18241832
case 4:
1833+
writeln("case 4");
18251834
goto default;
18261835
case 5:
1836+
writeln("case 5");
18271837
goto case 4;
18281838
default:
1829-
x = 4;
1839+
writeln("default");
18301840
break;
18311841
}
18321842
---
1843+
)
18331844

18341845
$(P Any intervening finally clauses are executed, along with releasing any
18351846
intervening synchronization mutexes.)
18361847

18371848
$(P It is illegal for a $(I GotoStatement) to be used to skip
1838-
initializations.)
1849+
initializations within its containing scope.)
1850+
1851+
$(BEST_PRACTICE Prefer using a higher-level control-flow construct
1852+
or a labelled $(GLINK BreakStatement)/$(GLINK ContinueStatement)
1853+
rather than the $(GRAMMAR_INLINE *`goto` Identifier;*) form.)
1854+
18391855

18401856
$(H2 $(LEGACY_LNAME2 WithStatement, with-statement, With Statement))
18411857

0 commit comments

Comments
 (0)