Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ By @cwfitzgerald in [#8162](https://github.com/gfx-rs/wgpu/pull/8162).
#### naga

- [wgsl-in] Allow a trailing comma in `@blend_src(…)` attributes. By @ErichDonGubler in [#8137](https://github.com/gfx-rs/wgpu/pull/8137).
- [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).

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

Expand Down
15 changes: 12 additions & 3 deletions naga/src/front/wgsl/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2121,7 +2121,7 @@ impl Parser {
let _ = lexer.next();
this.pop_rule_span(lexer);
}
(Token::Paren('{') | Token::Attribute, _) => {
(token, _) if is_start_of_compound_statement(token) => {
let (inner, span) = this.block(lexer, ctx, brace_nesting_level)?;
block.stmts.push(ast::Statement {
kind: ast::StatementKind::Block(inner),
Expand Down Expand Up @@ -2287,11 +2287,14 @@ impl Parser {
let value = loop {
let value = this.switch_value(lexer, ctx)?;
if lexer.skip(Token::Separator(',')) {
if lexer.skip(Token::Separator(':')) {
// list of values ends with ':' or a compound statement
let next_token = lexer.peek().0;
if next_token == Token::Separator(':')
|| is_start_of_compound_statement(next_token)
{
break value;
}
} else {
lexer.skip(Token::Separator(':'));
break value;
}
cases.push(ast::SwitchCase {
Expand All @@ -2301,6 +2304,8 @@ impl Parser {
});
};

lexer.skip(Token::Separator(':'));

let body = this.block(lexer, ctx, brace_nesting_level)?.0;

cases.push(ast::SwitchCase {
Expand Down Expand Up @@ -3244,3 +3249,7 @@ impl Parser {
})
}
}

const fn is_start_of_compound_statement<'a>(token: Token<'a>) -> bool {
matches!(token, Token::Attribute | Token::Paren('{'))
}
19 changes: 19 additions & 0 deletions naga/tests/in/wgsl/control-flow.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ fn control_flow() {
pos = 3;
}
}

// trailing commas
switch pos {
case 1, {
pos = 0;
}
case 2,: {
pos = 1;
}
case 3, 4, {
pos = 2;
}
case 5, 6,: {
pos = 3;
}
default {
pos = 4;
}
}
}

fn switch_default_break(i: i32) {
Expand Down
29 changes: 27 additions & 2 deletions naga/tests/out/glsl/wgsl-control-flow.main.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,44 @@ void control_flow() {
}
case 2: {
pos = 1;
return;
break;
}
case 3: {
pos = 2;
break;
}
case 4: {
break;
}
default: {
pos = 3;
break;
}
}
int _e15 = pos;
switch(_e15) {
case 1: {
pos = 0;
return;
}
case 2: {
pos = 1;
return;
}
case 3:
case 4: {
pos = 2;
return;
}
default: {
case 5:
case 6: {
pos = 3;
return;
}
default: {
pos = 4;
return;
}
}
}

Expand Down
29 changes: 27 additions & 2 deletions naga/tests/out/hlsl/wgsl-control-flow.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,44 @@ void control_flow()
}
case 2: {
pos = int(1);
return;
break;
}
case 3: {
pos = int(2);
break;
}
case 4: {
break;
}
default: {
pos = int(3);
break;
}
}
int _e15 = pos;
switch(_e15) {
case 1: {
pos = int(0);
return;
}
case 2: {
pos = int(1);
return;
}
case 3:
case 4: {
pos = int(2);
return;
}
default: {
case 5:
case 6: {
pos = int(3);
return;
}
default: {
pos = int(4);
return;
}
}
}

Expand Down
29 changes: 27 additions & 2 deletions naga/tests/out/msl/wgsl-control-flow.msl
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,44 @@ void control_flow(
}
case 2: {
pos = 1;
return;
break;
}
case 3: {
pos = 2;
break;
}
case 4: {
break;
}
default: {
pos = 3;
break;
}
}
int _e15 = pos;
switch(_e15) {
case 1: {
pos = 0;
return;
}
case 2: {
pos = 1;
return;
}
case 3:
case 4: {
pos = 2;
return;
}
default: {
case 5:
case 6: {
pos = 3;
return;
}
default: {
pos = 4;
return;
}
}
}

Expand Down
Loading
Loading