Skip to content

Commit 2ff5173

Browse files
committed
[naga wgsl-in] Allow trailing comma in switch cases
1 parent 1c8d769 commit 2ff5173

File tree

8 files changed

+445
-304
lines changed

8 files changed

+445
-304
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ This allows using precompiled shaders without manually checking which backend's
125125
#### Naga
126126

127127
- [wgsl-in] Allow a trailing comma in `@blend_src(…)` attributes. By @ErichDonGubler in [#8137](https://github.com/gfx-rs/wgpu/pull/8137).
128+
- [wgsl-in] Allow a trailing comma in the list of `case` values inside a `switch`. By @reima in [#8165](https://github.com/gfx-rs/wgpu/pull/8165).
128129

129130
## v26.0.4 (2025-08-07)
130131

naga/src/front/wgsl/parse/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,11 +2287,17 @@ impl Parser {
22872287
let value = loop {
22882288
let value = this.switch_value(lexer, ctx)?;
22892289
if lexer.skip(Token::Separator(',')) {
2290-
if lexer.skip(Token::Separator(':')) {
2290+
// list of values ends with ':' or a compound statement
2291+
// indicated by an attribute or '{'
2292+
if matches!(
2293+
lexer.peek().0,
2294+
Token::Separator(':')
2295+
| Token::Attribute
2296+
| Token::Paren('{')
2297+
) {
22912298
break value;
22922299
}
22932300
} else {
2294-
lexer.skip(Token::Separator(':'));
22952301
break value;
22962302
}
22972303
cases.push(ast::SwitchCase {
@@ -2301,6 +2307,8 @@ impl Parser {
23012307
});
23022308
};
23032309

2310+
lexer.skip(Token::Separator(':'));
2311+
23042312
let body = this.block(lexer, ctx, brace_nesting_level)?.0;
23052313

23062314
cases.push(ast::SwitchCase {

naga/tests/in/wgsl/control-flow.wgsl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ fn control_flow() {
5858
pos = 3;
5959
}
6060
}
61+
62+
// trailing commas
63+
switch pos {
64+
case 1, {
65+
pos = 0;
66+
}
67+
case 2,: {
68+
pos = 1;
69+
}
70+
case 3, 4, {
71+
pos = 2;
72+
}
73+
case 5, 6,: {
74+
pos = 3;
75+
}
76+
default {
77+
pos = 4;
78+
}
79+
}
6180
}
6281

6382
fn switch_default_break(i: i32) {

naga/tests/out/glsl/wgsl-control-flow.main.Compute.glsl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,44 @@ void control_flow() {
5858
}
5959
case 2: {
6060
pos = 1;
61-
return;
61+
break;
6262
}
6363
case 3: {
6464
pos = 2;
65+
break;
66+
}
67+
case 4: {
68+
break;
69+
}
70+
default: {
71+
pos = 3;
72+
break;
73+
}
74+
}
75+
int _e15 = pos;
76+
switch(_e15) {
77+
case 1: {
78+
pos = 0;
6579
return;
6680
}
81+
case 2: {
82+
pos = 1;
83+
return;
84+
}
85+
case 3:
6786
case 4: {
87+
pos = 2;
6888
return;
6989
}
70-
default: {
90+
case 5:
91+
case 6: {
7192
pos = 3;
7293
return;
7394
}
95+
default: {
96+
pos = 4;
97+
return;
98+
}
7499
}
75100
}
76101

naga/tests/out/hlsl/wgsl-control-flow.hlsl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,44 @@ void control_flow()
4949
}
5050
case 2: {
5151
pos = int(1);
52-
return;
52+
break;
5353
}
5454
case 3: {
5555
pos = int(2);
56+
break;
57+
}
58+
case 4: {
59+
break;
60+
}
61+
default: {
62+
pos = int(3);
63+
break;
64+
}
65+
}
66+
int _e15 = pos;
67+
switch(_e15) {
68+
case 1: {
69+
pos = int(0);
5670
return;
5771
}
72+
case 2: {
73+
pos = int(1);
74+
return;
75+
}
76+
case 3:
5877
case 4: {
78+
pos = int(2);
5979
return;
6080
}
61-
default: {
81+
case 5:
82+
case 6: {
6283
pos = int(3);
6384
return;
6485
}
86+
default: {
87+
pos = int(4);
88+
return;
89+
}
6590
}
6691
}
6792

naga/tests/out/msl/wgsl-control-flow.msl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,44 @@ void control_flow(
5858
}
5959
case 2: {
6060
pos = 1;
61-
return;
61+
break;
6262
}
6363
case 3: {
6464
pos = 2;
65+
break;
66+
}
67+
case 4: {
68+
break;
69+
}
70+
default: {
71+
pos = 3;
72+
break;
73+
}
74+
}
75+
int _e15 = pos;
76+
switch(_e15) {
77+
case 1: {
78+
pos = 0;
6579
return;
6680
}
81+
case 2: {
82+
pos = 1;
83+
return;
84+
}
85+
case 3:
6786
case 4: {
87+
pos = 2;
6888
return;
6989
}
70-
default: {
90+
case 5:
91+
case 6: {
7192
pos = 3;
7293
return;
7394
}
95+
default: {
96+
pos = 4;
97+
return;
98+
}
7499
}
75100
}
76101

0 commit comments

Comments
 (0)