From 816fa347ad7afa6053fc7ea49d521239d1cdb899 Mon Sep 17 00:00:00 2001 From: Igor Shaposhnik Date: Sun, 13 Mar 2022 00:48:10 +0300 Subject: [PATCH] [wgsl-in] Uses commas to separate struct members instead of semicolons --- src/front/wgsl/mod.rs | 18 +++++++---- src/front/wgsl/tests.rs | 14 ++++----- tests/in/access.wgsl | 12 ++++---- tests/in/boids.wgsl | 20 ++++++------ tests/in/bounds-check-restrict.wgsl | 8 ++--- tests/in/bounds-check-zero-atomic.wgsl | 6 ++-- tests/in/bounds-check-zero.wgsl | 8 ++--- tests/in/collatz.wgsl | 2 +- tests/in/extra.wgsl | 8 ++--- tests/in/globals.wgsl | 4 +-- tests/in/interface.wgsl | 14 ++++----- tests/in/interpolate.wgsl | 16 +++++----- tests/in/operators.wgsl | 4 +-- tests/in/pointers.wgsl | 2 +- tests/in/policy-mix.wgsl | 4 +-- tests/in/push-constants.wgsl | 4 +-- tests/in/quad.wgsl | 4 +-- tests/in/shadow.wgsl | 20 ++++++------ tests/in/skybox.wgsl | 8 ++--- tests/wgsl-errors.rs | 42 +++++++++++++------------- 20 files changed, 112 insertions(+), 106 deletions(-) diff --git a/src/front/wgsl/mod.rs b/src/front/wgsl/mod.rs index 2271a915d2..3f2348b98b 100644 --- a/src/front/wgsl/mod.rs +++ b/src/front/wgsl/mod.rs @@ -2811,7 +2811,14 @@ impl Parser { let mut members = Vec::new(); lexer.expect(Token::Paren('{'))?; - loop { + let mut ready = true; + while !lexer.skip(Token::Paren('}')) { + if !ready { + return Err(Error::Unexpected( + lexer.next(), + ExpectedToken::Token(Token::Separator(',')), + )); + } let (mut size, mut align) = (None, None); self.push_scope(Scope::Attribute, lexer); let mut bind_parser = BindingParser::default(); @@ -2838,10 +2845,6 @@ impl Parser { let bind_span = self.pop_scope(lexer); let (name, span) = match lexer.next() { (Token::Word(word), span) => (word, span), - (Token::Paren('}'), _) => { - let span = Layouter::round_up(alignment, offset); - return Ok((members, span)); - } other => return Err(Error::Unexpected(other, ExpectedToken::FieldName)), }; if crate::keywords::wgsl::RESERVED.contains(&name) { @@ -2849,7 +2852,7 @@ impl Parser { } lexer.expect(Token::Separator(':'))?; let (ty, _access) = self.parse_type_decl(lexer, None, type_arena, const_arena)?; - lexer.expect(Token::Separator(';'))?; + ready = lexer.skip(Token::Separator(',')); self.layouter.update(type_arena, const_arena).unwrap(); @@ -2868,6 +2871,9 @@ impl Parser { offset: range.start, }); } + + let span = Layouter::round_up(alignment, offset); + Ok((members, span)) } fn parse_type_decl_impl<'a>( diff --git a/src/front/wgsl/tests.rs b/src/front/wgsl/tests.rs index a439eff4b9..906311248f 100644 --- a/src/front/wgsl/tests.rs +++ b/src/front/wgsl/tests.rs @@ -157,11 +157,11 @@ fn parse_type_cast() { fn parse_struct() { parse_str( " - struct Foo { x: i32; }; + struct Foo { x: i32 }; struct Bar { - @size(16) x: vec2; - @align(16) y: f32; - @size(32) @align(8) z: vec3; + @size(16) x: vec2, + @align(16) y: f32, + @size(32) @align(8) z: vec3, }; struct Empty {}; var s: Foo; @@ -415,8 +415,8 @@ fn parse_struct_instantiation() { parse_str( " struct Foo { - a: f32; - b: vec3; + a: f32, + b: vec3, }; @stage(fragment) @@ -433,7 +433,7 @@ fn parse_array_length() { parse_str( " struct Foo { - data: array; + data: array }; // this is used as both input and output for convenience @group(0) @binding(0) diff --git a/tests/in/access.wgsl b/tests/in/access.wgsl index d68a74ca0e..390a018b11 100644 --- a/tests/in/access.wgsl +++ b/tests/in/access.wgsl @@ -1,15 +1,15 @@ // This snapshot tests accessing various containers, dereferencing pointers. struct AlignedWrapper { - @align(8) value: i32; + @align(8) value: i32 }; struct Bar { - matrix: mat4x4; - matrix_array: array, 2>; - atom: atomic; - arr: array, 2>; - data: array; + matrix: mat4x4, + matrix_array: array, 2>, + atom: atomic, + arr: array, 2>, + data: array, }; @group(0) @binding(0) diff --git a/tests/in/boids.wgsl b/tests/in/boids.wgsl index c3106f1c64..2655f2783a 100644 --- a/tests/in/boids.wgsl +++ b/tests/in/boids.wgsl @@ -1,22 +1,22 @@ let NUM_PARTICLES: u32 = 1500u; struct Particle { - pos : vec2; - vel : vec2; + pos : vec2, + vel : vec2, }; struct SimParams { - deltaT : f32; - rule1Distance : f32; - rule2Distance : f32; - rule3Distance : f32; - rule1Scale : f32; - rule2Scale : f32; - rule3Scale : f32; + deltaT : f32, + rule1Distance : f32, + rule2Distance : f32, + rule3Distance : f32, + rule1Scale : f32, + rule2Scale : f32, + rule3Scale : f32, }; struct Particles { - particles : array; + particles : array }; @group(0) @binding(0) var params : SimParams; diff --git a/tests/in/bounds-check-restrict.wgsl b/tests/in/bounds-check-restrict.wgsl index fefcdec0cd..d88be39fb4 100644 --- a/tests/in/bounds-check-restrict.wgsl +++ b/tests/in/bounds-check-restrict.wgsl @@ -1,10 +1,10 @@ // Tests for `naga::back::BoundsCheckPolicy::Restrict`. struct Globals { - a: array; - v: vec4; - m: mat3x4; - d: array; + a: array, + v: vec4, + m: mat3x4, + d: array, }; @group(0) @binding(0) var globals: Globals; diff --git a/tests/in/bounds-check-zero-atomic.wgsl b/tests/in/bounds-check-zero-atomic.wgsl index ad4012dbf1..c64cc8bd0a 100644 --- a/tests/in/bounds-check-zero-atomic.wgsl +++ b/tests/in/bounds-check-zero-atomic.wgsl @@ -5,9 +5,9 @@ // be combined. struct Globals { - a: atomic; - b: array, 10>; - c: array>; + a: atomic, + b: array, 10>, + c: array>, }; @group(0) @binding(0) var globals: Globals; diff --git a/tests/in/bounds-check-zero.wgsl b/tests/in/bounds-check-zero.wgsl index e1a05ac927..1dbfd9cb5e 100644 --- a/tests/in/bounds-check-zero.wgsl +++ b/tests/in/bounds-check-zero.wgsl @@ -1,10 +1,10 @@ // Tests for `naga::back::BoundsCheckPolicy::ReadZeroSkipWrite`. struct Globals { - a: array; - v: vec4; - m: mat3x4; - d: array; + a: array, + v: vec4, + m: mat3x4, + d: array, }; @group(0) @binding(0) var globals: Globals; diff --git a/tests/in/collatz.wgsl b/tests/in/collatz.wgsl index 7e2b135434..15a9d46056 100644 --- a/tests/in/collatz.wgsl +++ b/tests/in/collatz.wgsl @@ -1,5 +1,5 @@ struct PrimeIndices { - data: array; + data: array }; // this is used as both input and output for convenience @group(0) @binding(0) diff --git a/tests/in/extra.wgsl b/tests/in/extra.wgsl index e8a858cc19..6dcecd5535 100644 --- a/tests/in/extra.wgsl +++ b/tests/in/extra.wgsl @@ -1,12 +1,12 @@ struct PushConstants { - index: u32; - double: vec2; + index: u32, + double: vec2, }; var pc: PushConstants; struct FragmentIn { - @location(0) color: vec4; - @builtin(primitive_index) primitive_index: u32; + @location(0) color: vec4, + @builtin(primitive_index) primitive_index: u32, }; @stage(fragment) diff --git a/tests/in/globals.wgsl b/tests/in/globals.wgsl index 7e5b725016..ed8c40a70d 100644 --- a/tests/in/globals.wgsl +++ b/tests/in/globals.wgsl @@ -6,9 +6,9 @@ var wg : array; var at: atomic; struct Foo { - v3: vec3; + v3: vec3, // test packed vec3 - v1: f32; + v1: f32, }; @group(0) @binding(1) var alignment: Foo; diff --git a/tests/in/interface.wgsl b/tests/in/interface.wgsl index 7375295521..d2cf84023c 100644 --- a/tests/in/interface.wgsl +++ b/tests/in/interface.wgsl @@ -1,8 +1,8 @@ // Testing various parts of the pipeline interface: locations, built-ins, and entry points struct VertexOutput { - @builtin(position) position: vec4; - @location(1) varying: f32; + @builtin(position) position: vec4, + @location(1) varying: f32, }; @stage(vertex) @@ -16,9 +16,9 @@ fn vertex( } struct FragmentOutput { - @builtin(frag_depth) depth: f32; - @builtin(sample_mask) sample_mask: u32; - @location(0) color: f32; + @builtin(frag_depth) depth: f32, + @builtin(sample_mask) sample_mask: u32, + @location(0) color: f32, }; @stage(fragment) @@ -47,11 +47,11 @@ fn compute( } struct Input1 { - @builtin(vertex_index) index: u32; + @builtin(vertex_index) index: u32, }; struct Input2 { - @builtin(instance_index) index: u32; + @builtin(instance_index) index: u32, }; @stage(vertex) diff --git a/tests/in/interpolate.wgsl b/tests/in/interpolate.wgsl index a3dea61e50..1cb7239458 100644 --- a/tests/in/interpolate.wgsl +++ b/tests/in/interpolate.wgsl @@ -1,14 +1,14 @@ //TODO: merge with "interface"? struct FragmentInput { - @builtin(position) position: vec4; - @location(0) @interpolate(flat) flat : u32; - @location(1) @interpolate(linear) linear : f32; - @location(2) @interpolate(linear, centroid) linear_centroid : vec2; - @location(3) @interpolate(linear, sample) linear_sample : vec3; - @location(4) @interpolate(perspective) perspective : vec4; - @location(5) @interpolate(perspective, centroid) perspective_centroid : f32; - @location(6) @interpolate(perspective, sample) perspective_sample : f32; + @builtin(position) position: vec4, + @location(0) @interpolate(flat) flat : u32, + @location(1) @interpolate(linear) linear : f32, + @location(2) @interpolate(linear, centroid) linear_centroid : vec2, + @location(3) @interpolate(linear, sample) linear_sample : vec3, + @location(4) @interpolate(perspective) perspective : vec4, + @location(5) @interpolate(perspective, centroid) perspective_centroid : f32, + @location(6) @interpolate(perspective, sample) perspective_sample : f32, }; @stage(vertex) diff --git a/tests/in/operators.wgsl b/tests/in/operators.wgsl index 9d472294f1..23dbf734c2 100644 --- a/tests/in/operators.wgsl +++ b/tests/in/operators.wgsl @@ -39,8 +39,8 @@ fn bool_cast(x: vec3) -> vec3 { } struct Foo { - a: vec4; - b: i32; + a: vec4, + b: i32, }; fn constructors() -> f32 { diff --git a/tests/in/pointers.wgsl b/tests/in/pointers.wgsl index 6eddb77953..cf07d5dbf1 100644 --- a/tests/in/pointers.wgsl +++ b/tests/in/pointers.wgsl @@ -5,7 +5,7 @@ fn f() { } struct DynamicArray { - arr: array; + arr: array }; @group(0) @binding(0) diff --git a/tests/in/policy-mix.wgsl b/tests/in/policy-mix.wgsl index fe05a3232a..a29556a5b3 100644 --- a/tests/in/policy-mix.wgsl +++ b/tests/in/policy-mix.wgsl @@ -3,12 +3,12 @@ // Storage and Uniform storage classes struct InStorage { - a: array, 10>; + a: array, 10> }; @group(0) @binding(0) var in_storage: InStorage; struct InUniform { - a: array, 20>; + a: array, 20> }; @group(0) @binding(1) var in_uniform: InUniform; diff --git a/tests/in/push-constants.wgsl b/tests/in/push-constants.wgsl index d5b4cf99dd..4a55ed20a8 100644 --- a/tests/in/push-constants.wgsl +++ b/tests/in/push-constants.wgsl @@ -1,10 +1,10 @@ struct PushConstants { - multiplier: f32; + multiplier: f32 }; var pc: PushConstants; struct FragmentIn { - @location(0) color: vec4; + @location(0) color: vec4 }; @stage(fragment) diff --git a/tests/in/quad.wgsl b/tests/in/quad.wgsl index 847de8630f..37b543397b 100644 --- a/tests/in/quad.wgsl +++ b/tests/in/quad.wgsl @@ -2,8 +2,8 @@ let c_scale: f32 = 1.2; struct VertexOutput { - @location(0) uv : vec2; - @builtin(position) position : vec4; + @location(0) uv : vec2, + @builtin(position) position : vec4, }; @stage(vertex) diff --git a/tests/in/shadow.wgsl b/tests/in/shadow.wgsl index 0b64b2f8d0..37e17877c9 100644 --- a/tests/in/shadow.wgsl +++ b/tests/in/shadow.wgsl @@ -1,6 +1,6 @@ struct Globals { - view_proj: mat4x4; - num_lights: vec4; + view_proj: mat4x4, + num_lights: vec4, }; @group(0) @@ -8,8 +8,8 @@ struct Globals { var u_globals: Globals; struct Entity { - world: mat4x4; - color: vec4; + world: mat4x4, + color: vec4, }; @group(1) @@ -24,9 +24,9 @@ fn vs_bake(@location(0) position: vec4) -> @builtin(position) vec4 { */ struct VertexOutput { - @builtin(position) proj_position: vec4; - @location(0) world_normal: vec3; - @location(1) world_position: vec4; + @builtin(position) proj_position: vec4, + @location(0) world_normal: vec3, + @location(1) world_position: vec4, }; @stage(vertex) @@ -46,9 +46,9 @@ fn vs_main( // fragment shader struct Light { - proj: mat4x4; - pos: vec4; - color: vec4; + proj: mat4x4, + pos: vec4, + color: vec4, }; @group(0) diff --git a/tests/in/skybox.wgsl b/tests/in/skybox.wgsl index afbc38718e..06e5a07aa0 100644 --- a/tests/in/skybox.wgsl +++ b/tests/in/skybox.wgsl @@ -1,11 +1,11 @@ struct VertexOutput { - @builtin(position) position: vec4; - @location(0) uv: vec3; + @builtin(position) position: vec4, + @location(0) uv: vec3, }; struct Data { - proj_inv: mat4x4; - view: mat4x4; + proj_inv: mat4x4, + view: mat4x4, }; @group(0) @binding(0) var r_data: Data; diff --git a/tests/wgsl-errors.rs b/tests/wgsl-errors.rs index a5a0a5fc11..bde6da336b 100644 --- a/tests/wgsl-errors.rs +++ b/tests/wgsl-errors.rs @@ -376,13 +376,13 @@ fn struct_member_zero_size() { check( r#" struct Bar { - @size(0) data: array; + @size(0) data: array }; "#, r#"error: struct member size or alignment must not be 0 ┌─ wgsl:3:23 │ -3 │ @size(0) data: array; +3 │ @size(0) data: array │ ^ struct member size or alignment must not be 0 "#, @@ -394,13 +394,13 @@ fn struct_member_zero_align() { check( r#" struct Bar { - @align(0) data: array; + @align(0) data: array }; "#, r#"error: struct member size or alignment must not be 0 ┌─ wgsl:3:24 │ -3 │ @align(0) data: array; +3 │ @align(0) data: array │ ^ struct member size or alignment must not be 0 "#, @@ -542,7 +542,7 @@ fn postfix_pointers() { check( r#" - struct S { m: i32; }; + struct S { m: i32 }; fn main() { var s: S = S(42); let ps = &s; @@ -655,12 +655,12 @@ fn reserved_keyword() { // struct member check( r#" - struct Foo { sampler: f32; }; + struct Foo { sampler: f32 }; "#, r###"error: name `sampler` is a reserved keyword ┌─ wgsl:2:26 │ -2 │ struct Foo { sampler: f32; }; +2 │ struct Foo { sampler: f32 }; │ ^^^^^^^ definition of `sampler` "###, @@ -843,8 +843,8 @@ fn invalid_arrays() { #[test] fn invalid_structs() { check_validation_error! { - "struct Bad { data: sampler; };", - "struct Bad { data: texture_2d; };": + "struct Bad { data: sampler };", + "struct Bad { data: texture_2d };": Err(naga::valid::ValidationError::Type { error: naga::valid::TypeError::InvalidData(_), .. @@ -852,7 +852,7 @@ fn invalid_structs() { } check_validation_error! { - "struct Bad { data: array; other: f32; };": + "struct Bad { data: array, other: f32, };": Err(naga::valid::ValidationError::Type { error: naga::valid::TypeError::InvalidDynamicArray(_, _), .. @@ -865,7 +865,7 @@ fn invalid_functions() { check_validation_error! { "fn unacceptable_unsized(arg: array) { }", " - struct Unsized { data: array; }; + struct Unsized { data: array }; fn unacceptable_unsized(arg: Unsized) { } ": Err(naga::valid::ValidationError::Function { @@ -883,7 +883,7 @@ fn invalid_functions() { check_validation_error! { "fn unacceptable_unsized(arg: ptr>) { }", " - struct Unsized { data: array; }; + struct Unsized { data: array }; fn unacceptable_unsized(arg: ptr) { } ": Err(naga::valid::ValidationError::Type { @@ -998,8 +998,8 @@ fn missing_bindings() { check_validation_error! { " struct VertexIn { - @location(0) pos: vec4; - uv: vec2; + @location(0) pos: vec4, + uv: vec2 }; @stage(vertex) @@ -1088,7 +1088,7 @@ fn valid_access() { fn invalid_local_vars() { check_validation_error! { " - struct Unsized { data: array; }; + struct Unsized { data: array }; fn local_ptr_dynamic_array(okay: ptr) { var not_okay: ptr> = &(*okay).data; } @@ -1144,12 +1144,12 @@ fn invalid_runtime_sized_arrays() { check_validation_error! { " struct Unsized { - arr: array; + arr: array }; struct Outer { - legit: i32; - unsized: Unsized; + legit: i32, + unsized: Unsized }; @group(0) @binding(0) var outer: Outer; @@ -1187,7 +1187,7 @@ fn select() { } ", " - struct S { member: i32; }; + struct S { member: i32 }; fn select_structs(which: bool) -> S { var x: S = S(1); var y: S = S(2); @@ -1257,7 +1257,7 @@ fn wrong_access_mode() { check_validation_error! { " struct Globals { - i: i32; + i: i32 }; @group(0) @binding(0) @@ -1269,7 +1269,7 @@ fn wrong_access_mode() { ", " struct Globals { - i: i32; + i: i32 }; @group(0) @binding(0)