Skip to content

Commit 6be5558

Browse files
[naga] allow trailing commas in template lists (#7142)
Co-authored-by: Erich Gubler <[email protected]>
1 parent 2c42a18 commit 6be5558

7 files changed

+420
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ By @brodycj in [#6924](https://github.com/gfx-rs/wgpu/pull/6924).
181181
- Allow abstract expressions to be used in WGSL function return statements. By @jamienicol in [#7035](https://github.com/gfx-rs/wgpu/pull/7035).
182182
- Error if structs have two fields with the same name. By @SparkyPotato in [#7088](https://github.com/gfx-rs/wgpu/pull/7088).
183183
- Forward '--keep-coordinate-space' flag to GLSL backend in naga-cli. By @cloone8 in [#7206](https://github.com/gfx-rs/wgpu/pull/7206).
184+
- Allow template lists to have a trailing comma. By @KentSlaney in [#7142](https://github.com/gfx-rs/wgpu/pull/7142).
184185
185186
#### General
186187

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ impl<'a> Lexer<'a> {
352352
}
353353
}
354354

355+
pub(in crate::front::wgsl) fn end_of_generic_arguments(&mut self) -> bool {
356+
self.skip(Token::Separator(',')) && self.peek().0 != Token::Paren('>')
357+
}
358+
355359
/// If the next token matches it is skipped and true is returned
356360
pub(in crate::front::wgsl) fn skip(&mut self, what: Token<'_>) -> bool {
357361
let (peeked_token, rest) = self.peek_token_and_rest();

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,9 @@ impl Parser {
576576
(Token::Paren('<'), ast::ConstructorType::PartialArray) => {
577577
lexer.expect_generic_paren('<')?;
578578
let base = self.type_decl(lexer, ctx)?;
579-
let size = if lexer.skip(Token::Separator(',')) {
579+
let size = if lexer.end_of_generic_arguments() {
580580
let expr = self.const_generic_expression(lexer, ctx)?;
581+
lexer.skip(Token::Separator(','));
581582
ast::ArraySize::Constant(expr)
582583
} else {
583584
ast::ArraySize::Dynamic
@@ -1244,6 +1245,7 @@ impl Parser {
12441245
let start = lexer.start_byte_offset();
12451246
let ty = self.type_decl(lexer, ctx)?;
12461247
let span = lexer.span_from(start);
1248+
lexer.skip(Token::Separator(','));
12471249
lexer.expect_generic_paren('>')?;
12481250
Ok((ty, span))
12491251
}
@@ -1436,8 +1438,10 @@ impl Parser {
14361438
lexer.expect(Token::Separator(','))?;
14371439
let base = self.type_decl(lexer, ctx)?;
14381440
if let crate::AddressSpace::Storage { ref mut access } = space {
1439-
*access = if lexer.skip(Token::Separator(',')) {
1440-
lexer.next_storage_access()?
1441+
*access = if lexer.end_of_generic_arguments() {
1442+
let result = lexer.next_storage_access()?;
1443+
lexer.skip(Token::Separator(','));
1444+
result
14411445
} else {
14421446
crate::StorageAccess::LOAD
14431447
};
@@ -1448,8 +1452,9 @@ impl Parser {
14481452
"array" => {
14491453
lexer.expect_generic_paren('<')?;
14501454
let base = self.type_decl(lexer, ctx)?;
1451-
let size = if lexer.skip(Token::Separator(',')) {
1455+
let size = if lexer.end_of_generic_arguments() {
14521456
let size = self.const_generic_expression(lexer, ctx)?;
1457+
lexer.skip(Token::Separator(','));
14531458
ast::ArraySize::Constant(size)
14541459
} else {
14551460
ast::ArraySize::Dynamic
@@ -1461,8 +1466,9 @@ impl Parser {
14611466
"binding_array" => {
14621467
lexer.expect_generic_paren('<')?;
14631468
let base = self.type_decl(lexer, ctx)?;
1464-
let size = if lexer.skip(Token::Separator(',')) {
1469+
let size = if lexer.end_of_generic_arguments() {
14651470
let size = self.unary_expression(lexer, ctx)?;
1471+
lexer.skip(Token::Separator(','));
14661472
ast::ArraySize::Constant(size)
14671473
} else {
14681474
ast::ArraySize::Dynamic
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
targets = "IR"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var<workgroup> sized_comma: array<u32, 1,>;
2+
var<workgroup> sized_no_comma: array<u32, 1>;
3+
4+
@group(0) @binding(0)
5+
var<storage, read_write> unsized_comma: array<u32,>;
6+
7+
@group(0) @binding(1)
8+
var<storage, read_write> unsized_no_comma: array<u32>;
9+
10+
@compute @workgroup_size(1)
11+
fn main() {
12+
sized_comma[0] = unsized_comma[0];
13+
sized_no_comma[0] = unsized_no_comma[0];
14+
unsized_no_comma[0] = sized_comma[0] + sized_no_comma[0];
15+
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
(
2+
types: [
3+
(
4+
name: None,
5+
inner: Scalar((
6+
kind: Uint,
7+
width: 4,
8+
)),
9+
),
10+
(
11+
name: None,
12+
inner: Array(
13+
base: 0,
14+
size: Constant(1),
15+
stride: 4,
16+
),
17+
),
18+
(
19+
name: None,
20+
inner: Array(
21+
base: 0,
22+
size: Dynamic,
23+
stride: 4,
24+
),
25+
),
26+
],
27+
special_types: (
28+
ray_desc: None,
29+
ray_intersection: None,
30+
predeclared_types: {},
31+
),
32+
constants: [],
33+
overrides: [],
34+
global_variables: [
35+
(
36+
name: Some("sized_comma"),
37+
space: WorkGroup,
38+
binding: None,
39+
ty: 1,
40+
init: None,
41+
),
42+
(
43+
name: Some("sized_no_comma"),
44+
space: WorkGroup,
45+
binding: None,
46+
ty: 1,
47+
init: None,
48+
),
49+
(
50+
name: Some("unsized_comma"),
51+
space: Storage(
52+
access: ("LOAD | STORE"),
53+
),
54+
binding: Some((
55+
group: 0,
56+
binding: 0,
57+
)),
58+
ty: 2,
59+
init: None,
60+
),
61+
(
62+
name: Some("unsized_no_comma"),
63+
space: Storage(
64+
access: ("LOAD | STORE"),
65+
),
66+
binding: Some((
67+
group: 0,
68+
binding: 1,
69+
)),
70+
ty: 2,
71+
init: None,
72+
),
73+
],
74+
global_expressions: [],
75+
functions: [],
76+
entry_points: [
77+
(
78+
name: "main",
79+
stage: Compute,
80+
early_depth_test: None,
81+
workgroup_size: (1, 1, 1),
82+
workgroup_size_overrides: None,
83+
function: (
84+
name: Some("main"),
85+
arguments: [],
86+
result: None,
87+
local_variables: [],
88+
expressions: [
89+
GlobalVariable(0),
90+
AccessIndex(
91+
base: 0,
92+
index: 0,
93+
),
94+
GlobalVariable(2),
95+
AccessIndex(
96+
base: 2,
97+
index: 0,
98+
),
99+
Load(
100+
pointer: 3,
101+
),
102+
GlobalVariable(1),
103+
AccessIndex(
104+
base: 5,
105+
index: 0,
106+
),
107+
GlobalVariable(3),
108+
AccessIndex(
109+
base: 7,
110+
index: 0,
111+
),
112+
Load(
113+
pointer: 8,
114+
),
115+
GlobalVariable(3),
116+
AccessIndex(
117+
base: 10,
118+
index: 0,
119+
),
120+
GlobalVariable(0),
121+
AccessIndex(
122+
base: 12,
123+
index: 0,
124+
),
125+
Load(
126+
pointer: 13,
127+
),
128+
GlobalVariable(1),
129+
AccessIndex(
130+
base: 15,
131+
index: 0,
132+
),
133+
Load(
134+
pointer: 16,
135+
),
136+
Binary(
137+
op: Add,
138+
left: 14,
139+
right: 17,
140+
),
141+
],
142+
named_expressions: {},
143+
body: [
144+
Emit((
145+
start: 1,
146+
end: 2,
147+
)),
148+
Emit((
149+
start: 3,
150+
end: 5,
151+
)),
152+
Store(
153+
pointer: 1,
154+
value: 4,
155+
),
156+
Emit((
157+
start: 6,
158+
end: 7,
159+
)),
160+
Emit((
161+
start: 8,
162+
end: 10,
163+
)),
164+
Store(
165+
pointer: 6,
166+
value: 9,
167+
),
168+
Emit((
169+
start: 11,
170+
end: 12,
171+
)),
172+
Emit((
173+
start: 13,
174+
end: 15,
175+
)),
176+
Emit((
177+
start: 16,
178+
end: 19,
179+
)),
180+
Store(
181+
pointer: 11,
182+
value: 18,
183+
),
184+
Return(
185+
value: None,
186+
),
187+
],
188+
diagnostic_filter_leaf: None,
189+
),
190+
),
191+
],
192+
diagnostic_filters: [],
193+
diagnostic_filter_leaf: None,
194+
)

0 commit comments

Comments
 (0)