Skip to content

Commit d2b3099

Browse files
authored
[spec/statement] Improve no case fall-through docs (#4294)
* [spec/statement] Improve no `case` fall-through docs A case statement can end with `noreturn` expression evaluation. Make example runnable. Make it clearer `goto case;` can't go to `default:`. * Do list assert(0) It's implementation defined whether it's recognized with type noreturn.
1 parent 8d5567a commit d2b3099

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

spec/statement.dd

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,25 +1505,36 @@ $(GNAME LastExp):
15051505
$(H3 $(LNAME2 no-implicit-fallthrough, No Implicit Fall-Through))
15061506

15071507

1508-
$(P A $(GLINK ScopeStatementList) must either be empty, or be ended with
1509-
a $(GLINK ContinueStatement), $(GLINK BreakStatement),
1510-
$(GLINK ReturnStatement), $(GLINK GotoStatement), $(GLINK2 expression, ThrowExpression)
1511-
or `assert(0)` expression unless this is the last case.)
1508+
$(P A $(GLINK ScopeStatementList) must either be empty, the last one in the `switch`,
1509+
or be ended by:)
15121510

1511+
* A $(GLINK ContinueStatement), $(GLINK BreakStatement),
1512+
$(GLINK ReturnStatement), or $(GLINK GotoStatement)
1513+
* Evaluating an $(DDSUBLINK spec/expression, assert-ct, `assert(0)` expression)
1514+
* Evaluating an expression of type $(DDSUBLINK spec/type, noreturn, `noreturn`)
1515+
1516+
$(P Simple forms of the last 2 cases above can be recognized by the compiler, but not all.)
1517+
1518+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
15131519
--------------
1520+
uint i;
1521+
string message;
1522+
15141523
switch (i)
15151524
{
15161525
case 1:
1517-
message ~= "one";
1526+
message = "one";
15181527
// ERROR: implicit fall-through
15191528
case 2:
15201529
// valid: the body is empty
15211530
default:
1522-
message ~= "unknown";
1531+
message = "2 or more";
1532+
// valid: no more case statements
15231533
}
15241534
--------------
1525-
1526-
$(P $(D goto case;) can be used for explicit fall-through:)
1535+
)
1536+
$(P $(D goto case;) can be used for explicit fall-through to the next
1537+
*CaseStatement*:)
15271538

15281539
$(SPEC_RUNNABLE_EXAMPLE_RUN
15291540
--------------
@@ -1533,7 +1544,7 @@ foreach (i; 1..5)
15331544
{
15341545
switch (i)
15351546
{
1536-
default: // valid: ends with 'throw'
1547+
default: // valid: ends with `noreturn` expression evaluation
15371548
throw new Exception("unknown number");
15381549

15391550
case 3: // valid: ends with 'break' (break out of the 'switch' only)
@@ -1557,7 +1568,7 @@ writeln(message);
15571568
--------------
15581569
)
15591570
$(P $(RELATIVE_LINK2 goto-statement, `goto`) also supports jumping to
1560-
a specific case or the default case statement.)
1571+
a specific case or to the default case statement.)
15611572

15621573
$(H3 $(LNAME2 string-switch, String Switch))
15631574

0 commit comments

Comments
 (0)