Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9bbccb8

Browse files
authored
Clarify default mixed in with cases, fix rule about duplicate case values (gpuweb#3316)
- default can appear at most once in a case selector list - Rephrase rule about duplicate case values. They are not always literals. - Restate rules about "case bodies" to include the default-alone clause - Fix the "declaration in a body" rule to mirror the clarifications recently made for the 'loop' statement - Rearrange to pull validation rules above behaviours. - Pull initial "purpose" statement at the top, above grammar rules.
1 parent e57b379 commit 9bbccb8

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

wgsl/index.bs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6673,6 +6673,9 @@ An `if` statement is executed as follows:
66736673

66746674
### Switch Statement ### {#switch-statement}
66756675

6676+
A <dfn noexport dfn-for="statement">switch</dfn> statement transfers control to one of a set of [=case clauses=], or to the [=default clause=],
6677+
depending on the evaluation of a selector expression.
6678+
66766679
<div class='syntax' noexport='true'>
66776680
<dfn for=syntax>switch_statement</dfn> :
66786681

@@ -6681,7 +6684,17 @@ An `if` statement is executed as follows:
66816684
<div class='syntax' noexport='true'>
66826685
<dfn for=syntax>switch_body</dfn> :
66836686

6687+
| [=syntax/case_clause=]
6688+
6689+
| [=syntax/default_alone_clause=]
6690+
</div>
6691+
<div class='syntax' noexport='true'>
6692+
<dfn for=syntax>case_clause</dfn> :
6693+
66846694
| [=syntax/case=] [=syntax/case_selectors=] [=syntax/colon=] ? [=syntax/compound_statement=]
6695+
</div>
6696+
<div class='syntax' noexport='true'>
6697+
<dfn for=syntax>default_alone_clause</dfn> :
66856698

66866699
| [=syntax/default=] [=syntax/colon=] ? [=syntax/compound_statement=]
66876700
</div>
@@ -6699,48 +6712,62 @@ An `if` statement is executed as follows:
66996712
| [=syntax/expression=]
67006713
</div>
67016714

6702-
A <dfn noexport dfn-for="statement">switch</dfn> statement transfers control to one of a set of case clauses, or to the `default` clause,
6703-
depending on the evaluation of a selector expression.
6715+
A <dfn noexport>case clause</dfn> is the [=syntax/case=] token followed by a comma-separated list of [=syntax/case_selector|case selectors=] and a
6716+
body in the form of a [=compound statement=].
67046717

6705-
If the selector value equals a value in a case selector list, then control is transferred to
6706-
the body of that case clause.
6707-
The expressions in the [=syntax/case_selectors=] [=shader-creation error|must=]
6708-
be [=const-expressions=].
6709-
If the selector value does not equal any of the case selector values, then control is
6710-
transferred to the `default` clause.
6718+
A <dfn noexport>default-alone clause</dfn> is the [=syntax/default=] token followed by a body in the form of a [=compound statement=].
67116719

6712-
Each switch statement [=shader-creation error|must=] have exactly one default clause.
6720+
A <dfn noexport>default clause</dfn> is either:
6721+
* a [=case clause=] where [=syntax/default=] appears as one of its selectors, or
6722+
* a [=default-alone clause=].
6723+
6724+
Each switch statement [=shader-creation error|must=] have exactly one [=default clause=].
6725+
6726+
The [=syntax/default=] token [=shader-creation error|must=] not appear more than once in a single [=syntax/case_selector=] list.
67136727

67146728
[=Type rule precondition=]:
67156729
For a single switch statement, the selector expression and all case selector expressions [=shader-creation error|must=] be of the same [=type/concrete=] [=integer scalar=] type.
67166730

6717-
A literal value [=shader-creation error|must not=] appear more than once in the case selectors for a switch statement.
6731+
The expressions in the [=syntax/case_selectors=] [=shader-creation error|must=]
6732+
be [=const-expressions=].
6733+
6734+
Two different case selector expressions in the same switch statement [=shader-creation error|must not=] have the same value.
67186735

6719-
Note: The value of the literal is what matters, not the spelling.
6720-
For example `0` and `0x0000` both denote the zero value.
6736+
If the selector value equals the value of an expression in a [=syntax/case_selector=] list,
6737+
then control is transferred to the body of that [=case clause=].
6738+
If the selector value does not equal any of the case selector values, then control is
6739+
transferred to the body of the [=default clause=].
67216740

6722-
When control reaches the end of a case body, control normally transfers to the first statement
6723-
after the switch statement.
6724-
When a [=declaration=] appears in a case body, its [=identifier=] is [=in scope=] from
6725-
the start of the next statement until the end of the case body.
6741+
When control reaches the end of the body of a clause, control transfers to the first statement after the switch statement.
6742+
6743+
When one of the statements in the body of a clause is a [=declaration=],
6744+
it follows the normal [=scope=] and [=lifetime=] rules of a declaration in a [=compound statement=].
6745+
That is, the body is a sequence of statements, and if one of those is a declaration
6746+
then the scope of that declaration extends from the start of the next statement in the sequence
6747+
until the end of the body.
6748+
The declaration executes when it is reached,
6749+
creating a new instance of the [=variable declaration|variable=] or [=value declaration|value=], and initializes it.
67266750

67276751
<div class='example wgsl function-scope' heading='WGSL Switch'>
67286752
<xmp highlight='rust'>
67296753
var a : i32;
67306754
let x : i32 = generateValue();
67316755
switch x {
6732-
case 0: { // the colon is optional
6756+
case 0: { // The colon is optional
67336757
a = 1;
67346758
}
6735-
default { // the default needn't appear last
6759+
default { // The default need not appear last
67366760
a = 2;
67376761
}
6738-
case 1, 2 { // multiple selector values can be used
6739-
a = 3; // a will be overridden in the next case
6762+
case 1, 2, { // Multiple selector values can be used
6763+
a = 3;
67406764
}
6741-
case 3 {
6765+
case 3, { // The trailing comma is optional
67426766
a = 4;
67436767
}
6768+
case 4 {
6769+
a = 5;
6770+
}
67446771
}
67456772
</xmp>
67466773
</div>
@@ -9012,7 +9039,7 @@ When several edges have to be created we use `X -> {Y, Z}` as a short-hand for `
90129039
Analysis of [[#for-statement|for]] and [[#while-statement|while]] loops follows
90139040
from their respective desugaring translations to [[#loop-statement|loop]] statements.
90149041

9015-
In [[#switch-statement|switch]], a `default` block is treated exactly like a case block with regards to uniformity.
9042+
In [[#switch-statement|switch]], a [=default-alone clause=] block is treated exactly like a [=case clause=] with regards to uniformity.
90169043

90179044
Note: If the set of behaviors (see [[#behaviors]]) for an if, switch, or loop statement is {Next}, this means that we either did not diverge within the statement, or we reconverged, so we pick the node corresponding to control flow at the start of the statement as the node corresponding to control flow at the exit of the statement.
90189045

wgsl/keywords

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ default
488488
discard
489489
else
490490
enable
491-
fallthrough
492491
false
493492
flat
494493
fn
@@ -524,6 +523,7 @@ demote
524523
demote_to_helper
525524
do
526525
enum
526+
fallthrough
527527
handle
528528
null
529529
premerge

0 commit comments

Comments
 (0)