From da845b2e7c0bb53aa54ac1e31f766ea3a04b0616 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Tue, 19 Aug 2025 17:49:40 +0100 Subject: [PATCH 1/2] [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:`. --- spec/statement.dd | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/spec/statement.dd b/spec/statement.dd index 20514c6343..14e5ae757a 100644 --- a/spec/statement.dd +++ b/spec/statement.dd @@ -1465,25 +1465,33 @@ $(GNAME LastExp): $(H3 $(LNAME2 no-implicit-fallthrough, No Implicit Fall-Through)) - $(P A $(GLINK ScopeStatementList) must either be empty, or be ended with - a $(GLINK ContinueStatement), $(GLINK BreakStatement), - $(GLINK ReturnStatement), $(GLINK GotoStatement), $(GLINK2 expression, ThrowExpression) - or `assert(0)` expression unless this is the last case.) + $(P A $(GLINK ScopeStatementList) must either be empty, the last one in the `switch`, + or be ended by:) + * A $(GLINK ContinueStatement), $(GLINK BreakStatement), + $(GLINK ReturnStatement), or $(GLINK GotoStatement) + * Evaluating an expression of type $(DDSUBLINK spec/type, noreturn, `noreturn`) + +$(SPEC_RUNNABLE_EXAMPLE_FAIL -------------- +uint i; +string message; + switch (i) { case 1: - message ~= "one"; + message = "one"; // ERROR: implicit fall-through case 2: // valid: the body is empty default: - message ~= "unknown"; + message = "2 or more"; + // valid: no more case statements } -------------- - - $(P $(D goto case;) can be used for explicit fall-through:) +) + $(P $(D goto case;) can be used for explicit fall-through to the next + *CaseStatement*:) $(SPEC_RUNNABLE_EXAMPLE_RUN -------------- @@ -1493,7 +1501,7 @@ foreach (i; 1..5) { switch (i) { - default: // valid: ends with 'throw' + default: // valid: ends with `noreturn` expression evaluation throw new Exception("unknown number"); case 3: // valid: ends with 'break' (break out of the 'switch' only) @@ -1517,7 +1525,7 @@ writeln(message); -------------- ) $(P $(RELATIVE_LINK2 goto-statement, `goto`) also supports jumping to - a specific case or the default case statement.) + a specific case or to the default case statement.) $(H3 $(LNAME2 string-switch, String Switch)) From c5e19aebeea5d5b4884d489a1e11dc0ce490dc9e Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Wed, 20 Aug 2025 11:14:54 +0100 Subject: [PATCH 2/2] Do list assert(0) It's implementation defined whether it's recognized with type noreturn. --- spec/statement.dd | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/statement.dd b/spec/statement.dd index 14e5ae757a..72af6bfc8f 100644 --- a/spec/statement.dd +++ b/spec/statement.dd @@ -1470,8 +1470,11 @@ $(H3 $(LNAME2 no-implicit-fallthrough, No Implicit Fall-Through)) * A $(GLINK ContinueStatement), $(GLINK BreakStatement), $(GLINK ReturnStatement), or $(GLINK GotoStatement) + * Evaluating an $(DDSUBLINK spec/expression, assert-ct, `assert(0)` expression) * Evaluating an expression of type $(DDSUBLINK spec/type, noreturn, `noreturn`) + $(P Simple forms of the last 2 cases above can be recognized by the compiler, but not all.) + $(SPEC_RUNNABLE_EXAMPLE_FAIL -------------- uint i;