2022-02-03 18:27:21 +00:00
|
|
|
/*!
|
|
|
|
Tests for the WGSL front end.
|
|
|
|
*/
|
2021-05-13 17:21:43 +00:00
|
|
|
#![cfg(feature = "wgsl-in")]
|
|
|
|
|
2021-04-14 18:20:48 +00:00
|
|
|
fn check(input: &str, snapshot: &str) {
|
|
|
|
let output = naga::front::wgsl::parse_str(input)
|
|
|
|
.expect_err("expected parser error")
|
2021-05-02 04:49:02 +00:00
|
|
|
.emit_to_string(input);
|
2021-04-14 18:20:48 +00:00
|
|
|
if output != snapshot {
|
2023-10-27 00:12:39 +00:00
|
|
|
for diff in diff::lines(snapshot, &output) {
|
2021-04-14 18:20:48 +00:00
|
|
|
match diff {
|
2023-01-31 00:34:04 +00:00
|
|
|
diff::Result::Left(l) => println!("-{l}"),
|
|
|
|
diff::Result::Both(l, _) => println!(" {l}"),
|
|
|
|
diff::Result::Right(r) => println!("+{r}"),
|
2021-04-14 18:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("Error snapshot failed");
|
|
|
|
}
|
2021-02-13 20:54:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 13:36:48 +00:00
|
|
|
#[test]
|
|
|
|
fn reserved_identifier_prefix() {
|
|
|
|
check(
|
|
|
|
"var __bad;",
|
|
|
|
r###"error: Identifier starts with a reserved prefix: '__bad'
|
|
|
|
┌─ wgsl:1:5
|
|
|
|
│
|
|
|
|
1 │ var __bad;
|
|
|
|
│ ^^^^^ invalid identifier
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-13 20:54:31 +00:00
|
|
|
#[test]
|
|
|
|
fn function_without_identifier() {
|
2021-04-14 18:20:48 +00:00
|
|
|
check(
|
2021-02-13 20:54:31 +00:00
|
|
|
"fn () {}",
|
2021-04-14 23:22:40 +00:00
|
|
|
r###"error: expected identifier, found '('
|
2021-04-14 18:20:48 +00:00
|
|
|
┌─ wgsl:1:4
|
|
|
|
│
|
|
|
|
1 │ fn () {}
|
|
|
|
│ ^ expected identifier
|
2021-02-28 04:22:34 +00:00
|
|
|
|
2021-04-14 18:20:48 +00:00
|
|
|
"###,
|
2021-02-13 20:54:31 +00:00
|
|
|
);
|
|
|
|
}
|
2021-03-04 03:28:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_integer() {
|
2021-04-14 18:20:48 +00:00
|
|
|
check(
|
2021-03-07 05:03:05 +00:00
|
|
|
"fn foo([location(1.)] x: i32) {}",
|
2021-04-14 23:22:40 +00:00
|
|
|
r###"error: expected identifier, found '['
|
2021-04-14 18:20:48 +00:00
|
|
|
┌─ wgsl:1:8
|
|
|
|
│
|
|
|
|
1 │ fn foo([location(1.)] x: i32) {}
|
|
|
|
│ ^ expected identifier
|
2021-03-04 03:28:54 +00:00
|
|
|
|
2021-04-14 18:20:48 +00:00
|
|
|
"###,
|
2021-03-04 03:28:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_float() {
|
2021-04-14 18:20:48 +00:00
|
|
|
check(
|
2023-01-12 17:26:28 +00:00
|
|
|
"const scale: f32 = 1.1.;",
|
|
|
|
r###"error: expected identifier, found ';'
|
|
|
|
┌─ wgsl:1:24
|
2021-04-14 18:20:48 +00:00
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
1 │ const scale: f32 = 1.1.;
|
|
|
|
│ ^ expected identifier
|
Improve wgsl lexer error messages.
Better errors for Unexpected, BadInteger, BadFloat, BadTexture, BadTypeCast, UnknownScalarType, UnknownStorageClass, UnknownAttribute, UnknownBuiltin, UnknownShaderStage, UnknownStorageFormat and UnknownConservativeDepth, ZeroStride, ZeroSizeOrAlign and UnknownType.
Also adds lexer::capture_span.
Also fixes some validation for texture sample types and and issue that cauld cause e.g. the type `f33` to be parsed as `f32`.
2021-05-25 14:58:17 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_texture_sample_type() {
|
|
|
|
check(
|
2023-01-12 17:26:28 +00:00
|
|
|
"const x: texture_2d<bool>;",
|
2022-12-05 10:39:15 +00:00
|
|
|
r###"error: texture sample type must be one of f32, i32 or u32, but found bool
|
2023-01-12 17:26:28 +00:00
|
|
|
┌─ wgsl:1:21
|
Improve wgsl lexer error messages.
Better errors for Unexpected, BadInteger, BadFloat, BadTexture, BadTypeCast, UnknownScalarType, UnknownStorageClass, UnknownAttribute, UnknownBuiltin, UnknownShaderStage, UnknownStorageFormat and UnknownConservativeDepth, ZeroStride, ZeroSizeOrAlign and UnknownType.
Also adds lexer::capture_span.
Also fixes some validation for texture sample types and and issue that cauld cause e.g. the type `f33` to be parsed as `f32`.
2021-05-25 14:58:17 +00:00
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
1 │ const x: texture_2d<bool>;
|
|
|
|
│ ^^^^ must be one of f32, i32 or u32
|
2021-03-04 03:28:54 +00:00
|
|
|
|
2021-04-14 18:20:48 +00:00
|
|
|
"###,
|
2021-03-04 03:28:54 +00:00
|
|
|
);
|
|
|
|
}
|
2021-05-13 17:29:03 +00:00
|
|
|
|
2021-05-17 23:19:33 +00:00
|
|
|
#[test]
|
|
|
|
fn unknown_identifier() {
|
|
|
|
check(
|
|
|
|
r###"
|
|
|
|
fn f(x: f32) -> f32 {
|
|
|
|
return x * schmoo;
|
|
|
|
}
|
|
|
|
"###,
|
2021-05-18 18:20:25 +00:00
|
|
|
r###"error: no definition in scope for identifier: 'schmoo'
|
2021-05-17 23:19:33 +00:00
|
|
|
┌─ wgsl:3:30
|
|
|
|
│
|
|
|
|
3 │ return x * schmoo;
|
|
|
|
│ ^^^^^^ unknown identifier
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-04 03:12:22 +00:00
|
|
|
#[test]
|
|
|
|
fn bad_texture() {
|
|
|
|
check(
|
|
|
|
r#"
|
2022-01-19 15:33:06 +00:00
|
|
|
@group(0) @binding(0) var sampler1 : sampler;
|
2021-07-04 03:12:22 +00:00
|
|
|
|
2022-04-15 09:54:15 +00:00
|
|
|
@fragment
|
2022-01-19 15:33:06 +00:00
|
|
|
fn main() -> @location(0) vec4<f32> {
|
2021-07-04 03:12:22 +00:00
|
|
|
let a = 3;
|
2021-11-24 13:28:23 +00:00
|
|
|
return textureSample(a, sampler1, vec2<f32>(0.0));
|
2021-07-04 03:12:22 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: expected an image, but found 'a' which is not an image
|
|
|
|
┌─ wgsl:7:38
|
|
|
|
│
|
2021-11-24 13:28:23 +00:00
|
|
|
7 │ return textureSample(a, sampler1, vec2<f32>(0.0));
|
2021-07-04 03:12:22 +00:00
|
|
|
│ ^ not an image
|
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-04 03:12:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_type_cast() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn x() -> i32 {
|
|
|
|
return i32(vec2<f32>(0.0));
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: cannot cast a vec2<f32> to a i32
|
2022-03-30 05:51:54 +00:00
|
|
|
┌─ wgsl:3:28
|
2021-07-04 03:12:22 +00:00
|
|
|
│
|
|
|
|
3 │ return i32(vec2<f32>(0.0));
|
2022-03-30 05:51:54 +00:00
|
|
|
│ ^^^^^^^^^^^^^^ cannot cast a vec2<f32> to a i32
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn type_not_constructible() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn x() {
|
2022-04-26 16:40:37 +00:00
|
|
|
_ = atomic<i32>(0);
|
2022-03-30 05:51:54 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: type `atomic` is not constructible
|
2022-04-26 16:40:37 +00:00
|
|
|
┌─ wgsl:3:21
|
2022-03-30 05:51:54 +00:00
|
|
|
│
|
2022-04-26 16:40:37 +00:00
|
|
|
3 │ _ = atomic<i32>(0);
|
|
|
|
│ ^^^^^^ type is not constructible
|
2022-03-30 05:51:54 +00:00
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn type_not_inferrable() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn x() {
|
2022-04-26 16:40:37 +00:00
|
|
|
_ = vec2();
|
2022-03-30 05:51:54 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: type can't be inferred
|
2022-04-26 16:40:37 +00:00
|
|
|
┌─ wgsl:3:21
|
2022-03-30 05:51:54 +00:00
|
|
|
│
|
2022-04-26 16:40:37 +00:00
|
|
|
3 │ _ = vec2();
|
|
|
|
│ ^^^^ type can't be inferred
|
2022-03-30 05:51:54 +00:00
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unexpected_constructor_parameters() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn x() {
|
2022-04-26 16:40:37 +00:00
|
|
|
_ = i32(0, 1);
|
2022-03-30 05:51:54 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: unexpected components
|
2023-01-12 17:26:28 +00:00
|
|
|
┌─ wgsl:3:28
|
2022-03-30 05:51:54 +00:00
|
|
|
│
|
2022-04-26 16:40:37 +00:00
|
|
|
3 │ _ = i32(0, 1);
|
2023-01-12 17:26:28 +00:00
|
|
|
│ ^ unexpected components
|
2022-03-30 05:51:54 +00:00
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn constructor_parameter_type_mismatch() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn x() {
|
2022-04-26 16:40:37 +00:00
|
|
|
_ = mat2x2<f32>(array(0, 1), vec2(2, 3));
|
2022-03-30 05:51:54 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: invalid type for constructor component at index [0]
|
2022-04-26 16:40:37 +00:00
|
|
|
┌─ wgsl:3:33
|
2022-03-30 05:51:54 +00:00
|
|
|
│
|
2022-04-26 16:40:37 +00:00
|
|
|
3 │ _ = mat2x2<f32>(array(0, 1), vec2(2, 3));
|
|
|
|
│ ^^^^^^^^^^^ invalid component type
|
2021-07-04 03:12:22 +00:00
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_texture_sample_type() {
|
|
|
|
check(
|
|
|
|
r#"
|
2022-01-19 15:33:06 +00:00
|
|
|
@group(0) @binding(0) var sampler1 : sampler;
|
|
|
|
@group(0) @binding(1) var texture : texture_2d<bool>;
|
2021-07-04 03:12:22 +00:00
|
|
|
|
2022-04-15 09:54:15 +00:00
|
|
|
@fragment
|
2022-01-19 15:33:06 +00:00
|
|
|
fn main() -> @location(0) vec4<f32> {
|
2021-11-24 13:28:23 +00:00
|
|
|
return textureSample(texture, sampler1, vec2<f32>(0.0));
|
2021-07-04 03:12:22 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: texture sample type must be one of f32, i32 or u32, but found bool
|
2022-01-19 15:33:06 +00:00
|
|
|
┌─ wgsl:3:60
|
2021-07-04 03:12:22 +00:00
|
|
|
│
|
2022-01-19 15:33:06 +00:00
|
|
|
3 │ @group(0) @binding(1) var texture : texture_2d<bool>;
|
|
|
|
│ ^^^^ must be one of f32, i32 or u32
|
2021-07-04 03:12:22 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-04 03:12:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_for_initializer() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn x() {
|
|
|
|
for ({};;) {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: for(;;) initializer is not an assignment or a function call: '{}'
|
|
|
|
┌─ wgsl:3:22
|
|
|
|
│
|
|
|
|
3 │ for ({};;) {}
|
|
|
|
│ ^^ not an assignment or function call
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown_storage_class() {
|
|
|
|
check(
|
|
|
|
r#"
|
2022-01-19 15:33:06 +00:00
|
|
|
@group(0) @binding(0) var<bad> texture: texture_2d<f32>;
|
2021-07-04 03:12:22 +00:00
|
|
|
"#,
|
2022-01-25 23:29:02 +00:00
|
|
|
r#"error: unknown address space: 'bad'
|
2022-01-19 15:33:06 +00:00
|
|
|
┌─ wgsl:2:39
|
2021-07-04 03:12:22 +00:00
|
|
|
│
|
2022-01-19 15:33:06 +00:00
|
|
|
2 │ @group(0) @binding(0) var<bad> texture: texture_2d<f32>;
|
2022-01-25 23:29:02 +00:00
|
|
|
│ ^^^ unknown address space
|
2021-07-04 03:12:22 +00:00
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown_attribute() {
|
|
|
|
check(
|
|
|
|
r#"
|
2022-01-19 15:33:06 +00:00
|
|
|
@a
|
2021-07-04 03:12:22 +00:00
|
|
|
fn x() {}
|
|
|
|
"#,
|
|
|
|
r#"error: unknown attribute: 'a'
|
2022-01-19 15:33:06 +00:00
|
|
|
┌─ wgsl:2:14
|
2021-07-04 03:12:22 +00:00
|
|
|
│
|
2022-01-19 15:33:06 +00:00
|
|
|
2 │ @a
|
|
|
|
│ ^ unknown attribute
|
2021-07-04 03:12:22 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-04 03:12:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-05 03:01:15 +00:00
|
|
|
#[test]
|
|
|
|
fn unknown_built_in() {
|
|
|
|
check(
|
|
|
|
r#"
|
2022-01-19 15:33:06 +00:00
|
|
|
fn x(@builtin(unknown_built_in) y: u32) {}
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
|
|
|
r#"error: unknown builtin: 'unknown_built_in'
|
2022-01-19 15:33:06 +00:00
|
|
|
┌─ wgsl:2:27
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2022-01-19 15:33:06 +00:00
|
|
|
2 │ fn x(@builtin(unknown_built_in) y: u32) {}
|
|
|
|
│ ^^^^^^^^^^^^^^^^ unknown builtin
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown_access() {
|
|
|
|
check(
|
|
|
|
r#"
|
2021-07-28 05:47:18 +00:00
|
|
|
var<storage,unknown_access> x: array<u32>;
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
|
|
|
r#"error: unknown access: 'unknown_access'
|
2021-07-28 05:47:18 +00:00
|
|
|
┌─ wgsl:2:25
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2021-07-28 05:47:18 +00:00
|
|
|
2 │ var<storage,unknown_access> x: array<u32>;
|
|
|
|
│ ^^^^^^^^^^^^^^ unknown access
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown_ident() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let a = b;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: no definition in scope for identifier: 'b'
|
|
|
|
┌─ wgsl:3:25
|
|
|
|
│
|
|
|
|
3 │ let a = b;
|
|
|
|
│ ^ unknown identifier
|
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown_scalar_type() {
|
|
|
|
check(
|
|
|
|
r#"
|
2023-01-12 17:26:28 +00:00
|
|
|
const a: vec2<something>;
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
|
|
|
r#"error: unknown scalar type: 'something'
|
2023-01-12 17:26:28 +00:00
|
|
|
┌─ wgsl:2:27
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
2 │ const a: vec2<something>;
|
|
|
|
│ ^^^^^^^^^ unknown scalar type
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2022-12-05 10:39:15 +00:00
|
|
|
= note: Valid scalar types are f32, f64, i32, u32, bool
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown_type() {
|
|
|
|
check(
|
|
|
|
r#"
|
2023-01-12 17:26:28 +00:00
|
|
|
const a: Vec = 10;
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
|
|
|
r#"error: unknown type: 'Vec'
|
2023-01-12 17:26:28 +00:00
|
|
|
┌─ wgsl:2:22
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
2 │ const a: Vec = 10;
|
|
|
|
│ ^^^ unknown type
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown_storage_format() {
|
|
|
|
check(
|
|
|
|
r#"
|
2023-01-12 17:26:28 +00:00
|
|
|
const storage1: texture_storage_1d<rgba>;
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
|
|
|
r#"error: unknown storage format: 'rgba'
|
2023-01-12 17:26:28 +00:00
|
|
|
┌─ wgsl:2:48
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
2 │ const storage1: texture_storage_1d<rgba>;
|
|
|
|
│ ^^^^ unknown storage format
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown_conservative_depth() {
|
|
|
|
check(
|
|
|
|
r#"
|
2022-01-19 15:33:06 +00:00
|
|
|
@early_depth_test(abc) fn main() {}
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
|
|
|
r#"error: unknown conservative depth: 'abc'
|
2022-01-19 15:33:06 +00:00
|
|
|
┌─ wgsl:2:31
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2022-01-19 15:33:06 +00:00
|
|
|
2 │ @early_depth_test(abc) fn main() {}
|
|
|
|
│ ^^^ unknown conservative depth
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-09 14:27:51 +00:00
|
|
|
fn struct_member_size_too_low() {
|
2021-07-05 03:01:15 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Bar {
|
2022-03-12 21:48:10 +00:00
|
|
|
@size(0) data: array<f32>
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
2022-06-09 14:27:51 +00:00
|
|
|
r#"error: struct member size must be at least 4
|
2022-01-19 15:33:06 +00:00
|
|
|
┌─ wgsl:3:23
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2022-03-12 21:48:10 +00:00
|
|
|
3 │ @size(0) data: array<f32>
|
2022-06-09 14:27:51 +00:00
|
|
|
│ ^ must be at least 4
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-09 14:27:51 +00:00
|
|
|
fn struct_member_align_too_low() {
|
2021-07-05 03:01:15 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Bar {
|
2022-06-09 14:27:51 +00:00
|
|
|
@align(8) data: vec3<f32>
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
2022-06-09 14:27:51 +00:00
|
|
|
r#"error: struct member alignment must be at least 16
|
2022-01-19 15:33:06 +00:00
|
|
|
┌─ wgsl:3:24
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2022-06-09 14:27:51 +00:00
|
|
|
3 │ @align(8) data: vec3<f32>
|
|
|
|
│ ^ must be at least 16
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_member_non_po2_align() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Bar {
|
|
|
|
@align(7) data: array<f32>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: struct member alignment must be a power of 2
|
|
|
|
┌─ wgsl:3:24
|
|
|
|
│
|
|
|
|
3 │ @align(7) data: array<f32>
|
|
|
|
│ ^ must be a power of 2
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inconsistent_binding() {
|
|
|
|
check(
|
|
|
|
r#"
|
2022-01-19 15:33:06 +00:00
|
|
|
fn foo(@builtin(vertex_index) @location(0) x: u32) {}
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
|
|
|
r#"error: input/output binding is not consistent
|
|
|
|
┌─ wgsl:2:16
|
|
|
|
│
|
2022-01-19 15:33:06 +00:00
|
|
|
2 │ fn foo(@builtin(vertex_index) @location(0) x: u32) {}
|
|
|
|
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ input/output binding is not consistent
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2021-07-05 20:06:30 +00:00
|
|
|
"#,
|
2021-07-05 03:01:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unknown_local_function() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn x() {
|
|
|
|
for (a();;) {}
|
|
|
|
}
|
|
|
|
"#,
|
2023-01-12 17:26:28 +00:00
|
|
|
r#"error: no definition in scope for identifier: 'a'
|
2021-07-05 03:01:15 +00:00
|
|
|
┌─ wgsl:3:22
|
|
|
|
│
|
|
|
|
3 │ for (a();;) {}
|
2023-01-12 17:26:28 +00:00
|
|
|
│ ^ unknown identifier
|
2021-07-05 03:01:15 +00:00
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_type_mismatch() {
|
|
|
|
check(
|
|
|
|
r#"
|
2023-01-12 17:26:28 +00:00
|
|
|
const x: i32 = 1.0;
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
2023-01-12 17:26:28 +00:00
|
|
|
r#"error: the type of `x` is expected to be `i32`, but got `f32`
|
|
|
|
┌─ wgsl:2:19
|
2021-07-05 03:01:15 +00:00
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
2 │ const x: i32 = 1.0;
|
|
|
|
│ ^ definition of `x`
|
2021-07-05 03:01:15 +00:00
|
|
|
|
2022-01-10 13:04:59 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let x: f32 = true;
|
|
|
|
}
|
|
|
|
"#,
|
2023-01-12 17:26:28 +00:00
|
|
|
r#"error: the type of `x` is expected to be `f32`, but got `bool`
|
2022-01-10 13:04:59 +00:00
|
|
|
┌─ wgsl:3:21
|
|
|
|
│
|
|
|
|
3 │ let x: f32 = true;
|
|
|
|
│ ^ definition of `x`
|
|
|
|
|
2021-07-05 03:01:15 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-06 05:16:15 +00:00
|
|
|
#[test]
|
2022-01-10 13:04:59 +00:00
|
|
|
fn var_type_mismatch() {
|
2021-07-06 05:16:15 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
2023-01-12 17:26:28 +00:00
|
|
|
var x: f32 = 1u;
|
2021-07-06 05:16:15 +00:00
|
|
|
}
|
|
|
|
"#,
|
2023-01-12 17:26:28 +00:00
|
|
|
r#"error: the type of `x` is expected to be `f32`, but got `u32`
|
2021-07-06 05:16:15 +00:00
|
|
|
┌─ wgsl:3:21
|
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
3 │ var x: f32 = 1u;
|
2021-07-06 05:16:15 +00:00
|
|
|
│ ^ definition of `x`
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn local_var_missing_type() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
var x;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: variable `x` needs a type
|
|
|
|
┌─ wgsl:3:21
|
|
|
|
│
|
|
|
|
3 │ var x;
|
|
|
|
│ ^ definition of `x`
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-15 02:59:59 +00:00
|
|
|
#[test]
|
|
|
|
fn postfix_pointers() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
var v: vec4<f32> = vec4<f32>(1.0, 1.0, 1.0, 1.0);
|
|
|
|
let pv = &v;
|
|
|
|
let a = *pv[3]; // Problematic line
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: the value indexed by a `[]` subscripting expression must not be a pointer
|
|
|
|
┌─ wgsl:5:26
|
|
|
|
│
|
|
|
|
5 │ let a = *pv[3]; // Problematic line
|
|
|
|
│ ^^ expression is a pointer
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r#"
|
2022-03-12 21:48:10 +00:00
|
|
|
struct S { m: i32 };
|
2021-09-15 02:59:59 +00:00
|
|
|
fn main() {
|
|
|
|
var s: S = S(42);
|
|
|
|
let ps = &s;
|
|
|
|
let a = *ps.m; // Problematic line
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: the value accessed by a `.member` expression must not be a pointer
|
|
|
|
┌─ wgsl:6:26
|
|
|
|
│
|
|
|
|
6 │ let a = *ps.m; // Problematic line
|
|
|
|
│ ^^ expression is a pointer
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-24 13:28:23 +00:00
|
|
|
#[test]
|
|
|
|
fn reserved_keyword() {
|
|
|
|
// global var
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
var bool: bool = true;
|
|
|
|
"#,
|
2023-01-12 17:26:28 +00:00
|
|
|
r###"error: name `bool` is a reserved keyword
|
2022-09-13 17:53:14 +00:00
|
|
|
┌─ wgsl:2:17
|
2021-11-24 13:28:23 +00:00
|
|
|
│
|
|
|
|
2 │ var bool: bool = true;
|
2023-01-12 17:26:28 +00:00
|
|
|
│ ^^^^ definition of `bool`
|
2021-11-24 13:28:23 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
// global constant
|
|
|
|
check(
|
|
|
|
r#"
|
2023-01-12 17:26:28 +00:00
|
|
|
const break: bool = true;
|
2021-11-24 13:28:23 +00:00
|
|
|
fn foo() {
|
|
|
|
var foo = break;
|
|
|
|
}
|
|
|
|
"#,
|
2021-11-24 22:36:57 +00:00
|
|
|
r###"error: name `break` is a reserved keyword
|
2023-01-12 17:26:28 +00:00
|
|
|
┌─ wgsl:2:19
|
2021-11-24 13:28:23 +00:00
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
2 │ const break: bool = true;
|
|
|
|
│ ^^^^^ definition of `break`
|
2021-11-24 13:28:23 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
// local let
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let atomic: f32 = 1.0;
|
|
|
|
}
|
|
|
|
"#,
|
2021-11-24 22:36:57 +00:00
|
|
|
r###"error: name `atomic` is a reserved keyword
|
2021-11-24 13:28:23 +00:00
|
|
|
┌─ wgsl:3:21
|
|
|
|
│
|
|
|
|
3 │ let atomic: f32 = 1.0;
|
|
|
|
│ ^^^^^^ definition of `atomic`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
// local var
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
var sampler: f32 = 1.0;
|
|
|
|
}
|
|
|
|
"#,
|
2021-11-24 22:36:57 +00:00
|
|
|
r###"error: name `sampler` is a reserved keyword
|
2021-11-24 13:28:23 +00:00
|
|
|
┌─ wgsl:3:21
|
|
|
|
│
|
|
|
|
3 │ var sampler: f32 = 1.0;
|
|
|
|
│ ^^^^^^^ definition of `sampler`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
// fn name
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn break() {}
|
|
|
|
"#,
|
2021-11-24 22:36:57 +00:00
|
|
|
r###"error: name `break` is a reserved keyword
|
2021-11-24 13:28:23 +00:00
|
|
|
┌─ wgsl:2:16
|
|
|
|
│
|
|
|
|
2 │ fn break() {}
|
|
|
|
│ ^^^^^ definition of `break`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
// struct
|
|
|
|
check(
|
|
|
|
r#"
|
2022-03-27 05:35:08 +00:00
|
|
|
struct array {}
|
2021-11-24 13:28:23 +00:00
|
|
|
"#,
|
2021-11-24 22:36:57 +00:00
|
|
|
r###"error: name `array` is a reserved keyword
|
2021-11-24 13:28:23 +00:00
|
|
|
┌─ wgsl:2:20
|
|
|
|
│
|
2022-03-27 05:35:08 +00:00
|
|
|
2 │ struct array {}
|
2021-11-24 13:28:23 +00:00
|
|
|
│ ^^^^^ definition of `array`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
// struct member
|
|
|
|
check(
|
|
|
|
r#"
|
2022-03-27 05:35:08 +00:00
|
|
|
struct Foo { sampler: f32 }
|
2021-11-24 13:28:23 +00:00
|
|
|
"#,
|
2021-11-24 22:36:57 +00:00
|
|
|
r###"error: name `sampler` is a reserved keyword
|
2021-11-24 13:28:23 +00:00
|
|
|
┌─ wgsl:2:26
|
|
|
|
│
|
2022-03-27 05:35:08 +00:00
|
|
|
2 │ struct Foo { sampler: f32 }
|
2021-11-24 13:28:23 +00:00
|
|
|
│ ^^^^^^^ definition of `sampler`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-24 22:36:57 +00:00
|
|
|
#[test]
|
|
|
|
fn module_scope_identifier_redefinition() {
|
2023-01-12 17:26:28 +00:00
|
|
|
// const
|
2021-11-24 22:36:57 +00:00
|
|
|
check(
|
|
|
|
r#"
|
2023-01-12 17:26:28 +00:00
|
|
|
const foo: bool = true;
|
|
|
|
const foo: bool = true;
|
2021-11-24 22:36:57 +00:00
|
|
|
"#,
|
|
|
|
r###"error: redefinition of `foo`
|
2023-01-12 17:26:28 +00:00
|
|
|
┌─ wgsl:2:19
|
2021-11-24 22:36:57 +00:00
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
2 │ const foo: bool = true;
|
|
|
|
│ ^^^ previous definition of `foo`
|
|
|
|
3 │ const foo: bool = true;
|
|
|
|
│ ^^^ redefinition of `foo`
|
2021-11-24 22:36:57 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
// var
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
var foo: bool = true;
|
|
|
|
var foo: bool = true;
|
|
|
|
"#,
|
2023-01-12 17:26:28 +00:00
|
|
|
r###"error: redefinition of `foo`
|
2022-09-13 17:53:14 +00:00
|
|
|
┌─ wgsl:2:17
|
2021-11-24 22:36:57 +00:00
|
|
|
│
|
|
|
|
2 │ var foo: bool = true;
|
2023-01-12 17:26:28 +00:00
|
|
|
│ ^^^ previous definition of `foo`
|
2021-11-24 22:36:57 +00:00
|
|
|
3 │ var foo: bool = true;
|
2023-01-12 17:26:28 +00:00
|
|
|
│ ^^^ redefinition of `foo`
|
2021-11-24 22:36:57 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
// let and var
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
var foo: bool = true;
|
2023-01-12 17:26:28 +00:00
|
|
|
const foo: bool = true;
|
2021-11-24 22:36:57 +00:00
|
|
|
"#,
|
|
|
|
r###"error: redefinition of `foo`
|
2022-09-13 17:53:14 +00:00
|
|
|
┌─ wgsl:2:17
|
2021-11-24 22:36:57 +00:00
|
|
|
│
|
|
|
|
2 │ var foo: bool = true;
|
2023-01-12 17:26:28 +00:00
|
|
|
│ ^^^ previous definition of `foo`
|
|
|
|
3 │ const foo: bool = true;
|
|
|
|
│ ^^^ redefinition of `foo`
|
2021-11-24 22:36:57 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
// function
|
|
|
|
check(
|
|
|
|
r#"fn foo() {}
|
|
|
|
fn bar() {}
|
|
|
|
fn foo() {}"#,
|
|
|
|
r###"error: redefinition of `foo`
|
|
|
|
┌─ wgsl:1:4
|
|
|
|
│
|
|
|
|
1 │ fn foo() {}
|
|
|
|
│ ^^^ previous definition of `foo`
|
|
|
|
2 │ fn bar() {}
|
|
|
|
3 │ fn foo() {}
|
|
|
|
│ ^^^ redefinition of `foo`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
// let and function
|
|
|
|
check(
|
|
|
|
r#"
|
2023-01-12 17:26:28 +00:00
|
|
|
const foo: bool = true;
|
2021-11-24 22:36:57 +00:00
|
|
|
fn foo() {}
|
|
|
|
"#,
|
|
|
|
r###"error: redefinition of `foo`
|
2023-01-12 17:26:28 +00:00
|
|
|
┌─ wgsl:2:19
|
2021-11-24 22:36:57 +00:00
|
|
|
│
|
2023-01-12 17:26:28 +00:00
|
|
|
2 │ const foo: bool = true;
|
|
|
|
│ ^^^ previous definition of `foo`
|
2021-11-24 22:36:57 +00:00
|
|
|
3 │ fn foo() {}
|
|
|
|
│ ^^^ redefinition of `foo`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-14 02:49:23 +00:00
|
|
|
#[test]
|
|
|
|
fn matrix_with_bad_type() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let m = mat2x2<i32>();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: matrix scalar type must be floating-point, but found `i32`
|
|
|
|
┌─ wgsl:3:32
|
|
|
|
│
|
|
|
|
3 │ let m = mat2x2<i32>();
|
|
|
|
│ ^^^ must be floating-point (e.g. `f32`)
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let m: mat3x3<i32>;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"error: matrix scalar type must be floating-point, but found `i32`
|
|
|
|
┌─ wgsl:3:31
|
|
|
|
│
|
|
|
|
3 │ let m: mat3x3<i32>;
|
|
|
|
│ ^^^ must be floating-point (e.g. `f32`)
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-29 22:30:46 +00:00
|
|
|
/// Check the result of validating a WGSL program against a pattern.
|
|
|
|
///
|
|
|
|
/// Unless you are generating code programmatically, the
|
|
|
|
/// `check_validation_error` macro will probably be more convenient to
|
|
|
|
/// use.
|
|
|
|
macro_rules! check_one_validation {
|
|
|
|
( $source:expr, $pattern:pat $( if $guard:expr )? ) => {
|
|
|
|
let source = $source;
|
|
|
|
let error = validation_error($source);
|
|
|
|
if ! matches!(&error, $pattern $( if $guard )? ) {
|
|
|
|
eprintln!("validation error does not match pattern:\n\
|
|
|
|
source code: {}\n\
|
|
|
|
\n\
|
|
|
|
actual result:\n\
|
|
|
|
{:#?}\n\
|
|
|
|
\n\
|
|
|
|
expected match for pattern:\n\
|
|
|
|
{}",
|
|
|
|
&source,
|
|
|
|
error,
|
|
|
|
stringify!($pattern));
|
|
|
|
$( eprintln!("if {}", stringify!($guard)); )?
|
|
|
|
panic!("validation error does not match pattern");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 20:18:13 +00:00
|
|
|
macro_rules! check_validation {
|
2021-05-26 16:11:18 +00:00
|
|
|
// We want to support an optional guard expression after the pattern, so
|
|
|
|
// that we can check values we can't match against, like strings.
|
|
|
|
// Unfortunately, we can't simply include `$( if $guard:expr )?` in the
|
|
|
|
// pattern, because Rust treats `?` as a repetition operator, and its count
|
|
|
|
// (0 or 1) will not necessarily match `$source`.
|
2021-05-13 17:29:03 +00:00
|
|
|
( $( $source:literal ),* : $pattern:pat ) => {
|
2022-04-29 22:30:46 +00:00
|
|
|
$(
|
|
|
|
check_one_validation!($source, $pattern);
|
|
|
|
)*
|
2021-05-26 16:11:18 +00:00
|
|
|
};
|
|
|
|
( $( $source:literal ),* : $pattern:pat if $guard:expr ) => {
|
2021-05-13 17:29:03 +00:00
|
|
|
$(
|
2022-04-29 22:30:46 +00:00
|
|
|
check_one_validation!($source, $pattern if $guard);
|
2021-05-13 17:29:03 +00:00
|
|
|
)*
|
2022-04-29 22:30:46 +00:00
|
|
|
}
|
2021-05-13 17:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn validation_error(source: &str) -> Result<naga::valid::ModuleInfo, naga::valid::ValidationError> {
|
2021-05-27 22:02:38 +00:00
|
|
|
let module = match naga::front::wgsl::parse_str(source) {
|
|
|
|
Ok(module) => module,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("WGSL parse failed:");
|
|
|
|
panic!("{}", err.emit_to_string(source));
|
|
|
|
}
|
|
|
|
};
|
2021-05-13 17:29:03 +00:00
|
|
|
naga::valid::Validator::new(
|
|
|
|
naga::valid::ValidationFlags::all(),
|
2023-02-16 20:44:29 +00:00
|
|
|
naga::valid::Capabilities::default(),
|
2021-05-13 17:29:03 +00:00
|
|
|
)
|
|
|
|
.validate(&module)
|
2021-10-25 02:47:03 +00:00
|
|
|
.map_err(|e| e.into_inner()) // TODO: Add tests for spans, too?
|
2021-05-13 17:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_arrays() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2023-04-04 10:35:32 +00:00
|
|
|
"alias Bad = array<array<f32>, 4>;",
|
|
|
|
"alias Bad = array<sampler, 4>;",
|
|
|
|
"alias Bad = array<texture_2d<f32>, 4>;":
|
2021-05-13 17:29:03 +00:00
|
|
|
Err(naga::valid::ValidationError::Type {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::TypeError::InvalidArrayBaseType(_),
|
2021-05-13 17:29:03 +00:00
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-05 21:03:07 +00:00
|
|
|
check_validation! {
|
|
|
|
r#"
|
|
|
|
fn main() -> f32 {
|
|
|
|
let a = array<f32, 3>(0., 1., 2.);
|
|
|
|
return a[-1];
|
|
|
|
}
|
|
|
|
"#:
|
|
|
|
Err(
|
|
|
|
naga::valid::ValidationError::Function {
|
|
|
|
name,
|
|
|
|
source: naga::valid::FunctionError::Expression {
|
|
|
|
source: naga::valid::ExpressionError::NegativeIndex(_),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if name == "main"
|
|
|
|
}
|
|
|
|
|
2023-05-12 12:38:50 +00:00
|
|
|
check(
|
2023-04-04 10:35:32 +00:00
|
|
|
"alias Bad = array<f32, true>;",
|
2023-04-19 12:55:07 +00:00
|
|
|
r###"error: must be a const-expression that resolves to a concrete integer scalar (u32 or i32)
|
2023-05-12 12:38:50 +00:00
|
|
|
┌─ wgsl:1:24
|
|
|
|
│
|
|
|
|
1 │ alias Bad = array<f32, true>;
|
2023-04-19 12:55:07 +00:00
|
|
|
│ ^^^^ must resolve to u32 or i32
|
2023-05-12 12:38:50 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
2021-05-13 17:29:03 +00:00
|
|
|
r#"
|
2023-01-12 17:26:28 +00:00
|
|
|
const length: f32 = 2.718;
|
2023-04-04 10:35:32 +00:00
|
|
|
alias Bad = array<f32, length>;
|
2023-05-12 12:38:50 +00:00
|
|
|
"#,
|
2023-04-19 12:55:07 +00:00
|
|
|
r###"error: must be a const-expression that resolves to a concrete integer scalar (u32 or i32)
|
2023-05-12 12:38:50 +00:00
|
|
|
┌─ wgsl:3:36
|
|
|
|
│
|
|
|
|
3 │ alias Bad = array<f32, length>;
|
2023-04-19 12:55:07 +00:00
|
|
|
│ ^^^^^^ must resolve to u32 or i32
|
2021-06-16 21:42:20 +00:00
|
|
|
|
2023-05-12 12:38:50 +00:00
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
2023-04-04 10:35:32 +00:00
|
|
|
"alias Bad = array<f32, 0>;",
|
2023-04-19 12:55:07 +00:00
|
|
|
r###"error: array element count must be positive (> 0)
|
2023-05-12 12:38:50 +00:00
|
|
|
┌─ wgsl:1:24
|
|
|
|
│
|
|
|
|
1 │ alias Bad = array<f32, 0>;
|
2023-04-19 12:55:07 +00:00
|
|
|
│ ^ must be positive
|
2023-05-12 12:38:50 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
"alias Bad = array<f32, -1>;",
|
2023-04-19 12:55:07 +00:00
|
|
|
r###"error: array element count must be positive (> 0)
|
2023-05-12 12:38:50 +00:00
|
|
|
┌─ wgsl:1:24
|
|
|
|
│
|
|
|
|
1 │ alias Bad = array<f32, -1>;
|
2023-04-19 12:55:07 +00:00
|
|
|
│ ^^ must be positive
|
2023-05-12 12:38:50 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
2021-05-13 17:29:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 09:47:10 +00:00
|
|
|
#[test]
|
|
|
|
fn discard_in_wrong_stage() {
|
|
|
|
check_validation! {
|
|
|
|
"@compute @workgroup_size(1)
|
|
|
|
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
|
|
|
if global_id.x == 3u {
|
|
|
|
discard;
|
|
|
|
}
|
|
|
|
}":
|
|
|
|
Err(naga::valid::ValidationError::EntryPoint {
|
|
|
|
stage: naga::ShaderStage::Compute,
|
|
|
|
source: naga::valid::EntryPointError::ForbiddenStageOperations,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
check_validation! {
|
|
|
|
"@vertex
|
2023-04-12 14:48:57 +00:00
|
|
|
fn main() -> @builtin(position) vec4<f32> {
|
|
|
|
if true {
|
|
|
|
discard;
|
|
|
|
}
|
|
|
|
return vec4<f32>();
|
2023-02-27 09:47:10 +00:00
|
|
|
}":
|
|
|
|
Err(naga::valid::ValidationError::EntryPoint {
|
|
|
|
stage: naga::ShaderStage::Vertex,
|
|
|
|
source: naga::valid::EntryPointError::ForbiddenStageOperations,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:29:03 +00:00
|
|
|
#[test]
|
|
|
|
fn invalid_structs() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2022-03-27 05:35:08 +00:00
|
|
|
"struct Bad { data: sampler }",
|
|
|
|
"struct Bad { data: texture_2d<f32> }":
|
2021-05-13 17:29:03 +00:00
|
|
|
Err(naga::valid::ValidationError::Type {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::TypeError::InvalidData(_),
|
2021-05-13 17:29:03 +00:00
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2022-03-27 05:35:08 +00:00
|
|
|
"struct Bad { data: array<f32>, other: f32, }":
|
2021-05-13 17:29:03 +00:00
|
|
|
Err(naga::valid::ValidationError::Type {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::TypeError::InvalidDynamicArray(_, _),
|
2021-05-13 17:29:03 +00:00
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
2022-04-14 16:36:53 +00:00
|
|
|
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2022-04-14 16:36:53 +00:00
|
|
|
"struct Empty {}":
|
|
|
|
Err(naga::valid::ValidationError::Type {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::TypeError::EmptyStruct,
|
2022-04-14 16:36:53 +00:00
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
2021-05-13 17:29:03 +00:00
|
|
|
}
|
2021-05-21 20:11:35 +00:00
|
|
|
|
2021-05-26 16:11:18 +00:00
|
|
|
#[test]
|
|
|
|
fn invalid_functions() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-05-27 21:01:40 +00:00
|
|
|
"fn unacceptable_unsized(arg: array<f32>) { }",
|
|
|
|
"
|
2022-03-27 05:35:08 +00:00
|
|
|
struct Unsized { data: array<f32> }
|
2021-05-27 21:01:40 +00:00
|
|
|
fn unacceptable_unsized(arg: Unsized) { }
|
|
|
|
":
|
2021-05-26 16:11:18 +00:00
|
|
|
Err(naga::valid::ValidationError::Function {
|
|
|
|
name: function_name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::InvalidArgumentType {
|
2021-05-26 16:11:18 +00:00
|
|
|
index: 0,
|
|
|
|
name: argument_name,
|
|
|
|
},
|
|
|
|
..
|
|
|
|
})
|
2021-05-27 21:01:40 +00:00
|
|
|
if function_name == "unacceptable_unsized" && argument_name == "arg"
|
|
|
|
}
|
|
|
|
|
2022-01-25 23:29:02 +00:00
|
|
|
// Pointer's address space cannot hold unsized data.
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-11-11 01:32:40 +00:00
|
|
|
"fn unacceptable_unsized(arg: ptr<workgroup, array<f32>>) { }",
|
2021-05-27 21:01:40 +00:00
|
|
|
"
|
2022-03-27 05:35:08 +00:00
|
|
|
struct Unsized { data: array<f32> }
|
2021-11-11 01:32:40 +00:00
|
|
|
fn unacceptable_unsized(arg: ptr<workgroup, Unsized>) { }
|
2021-05-27 21:01:40 +00:00
|
|
|
":
|
2021-11-11 01:32:40 +00:00
|
|
|
Err(naga::valid::ValidationError::Type {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::TypeError::InvalidPointerToUnsized {
|
2021-11-11 01:32:40 +00:00
|
|
|
base: _,
|
2022-01-25 23:29:02 +00:00
|
|
|
space: naga::AddressSpace::WorkGroup { .. },
|
2021-11-11 01:32:40 +00:00
|
|
|
},
|
|
|
|
..
|
|
|
|
})
|
2021-05-26 16:11:18 +00:00
|
|
|
}
|
2021-09-27 22:49:28 +00:00
|
|
|
|
2023-09-25 15:49:56 +00:00
|
|
|
// Pointers of these address spaces cannot be passed as arguments.
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2022-01-25 23:29:02 +00:00
|
|
|
"fn unacceptable_ptr_space(arg: ptr<storage, array<f32>>) { }":
|
2021-11-11 01:32:40 +00:00
|
|
|
Err(naga::valid::ValidationError::Function {
|
|
|
|
name: function_name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::InvalidArgumentPointerSpace {
|
2021-11-11 01:32:40 +00:00
|
|
|
index: 0,
|
|
|
|
name: argument_name,
|
2022-01-25 23:29:02 +00:00
|
|
|
space: naga::AddressSpace::Storage { .. },
|
2021-11-11 01:32:40 +00:00
|
|
|
},
|
|
|
|
..
|
|
|
|
})
|
2022-01-25 23:29:02 +00:00
|
|
|
if function_name == "unacceptable_ptr_space" && argument_name == "arg"
|
2021-11-11 01:32:40 +00:00
|
|
|
}
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2022-01-25 23:29:02 +00:00
|
|
|
"fn unacceptable_ptr_space(arg: ptr<uniform, f32>) { }":
|
2021-09-27 22:49:28 +00:00
|
|
|
Err(naga::valid::ValidationError::Function {
|
|
|
|
name: function_name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::InvalidArgumentPointerSpace {
|
2021-09-27 22:49:28 +00:00
|
|
|
index: 0,
|
|
|
|
name: argument_name,
|
2022-01-25 23:29:02 +00:00
|
|
|
space: naga::AddressSpace::Uniform,
|
2021-09-27 22:49:28 +00:00
|
|
|
},
|
|
|
|
..
|
|
|
|
})
|
2022-01-25 23:29:02 +00:00
|
|
|
if function_name == "unacceptable_ptr_space" && argument_name == "arg"
|
2021-09-27 22:49:28 +00:00
|
|
|
}
|
2023-09-25 15:49:56 +00:00
|
|
|
check_validation! {
|
|
|
|
"fn unacceptable_ptr_space(arg: ptr<workgroup, f32>) { }":
|
|
|
|
Err(naga::valid::ValidationError::Function {
|
|
|
|
name: function_name,
|
|
|
|
source: naga::valid::FunctionError::InvalidArgumentPointerSpace {
|
|
|
|
index: 0,
|
|
|
|
name: argument_name,
|
|
|
|
space: naga::AddressSpace::WorkGroup,
|
|
|
|
},
|
|
|
|
..
|
|
|
|
})
|
|
|
|
if function_name == "unacceptable_ptr_space" && argument_name == "arg"
|
|
|
|
}
|
2022-05-01 05:22:19 +00:00
|
|
|
|
|
|
|
check_validation! {
|
|
|
|
"
|
|
|
|
struct AFloat {
|
|
|
|
said_float: f32
|
|
|
|
};
|
|
|
|
@group(0) @binding(0)
|
|
|
|
var<storage> float: AFloat;
|
|
|
|
|
|
|
|
fn return_pointer() -> ptr<storage, f32> {
|
|
|
|
return &float.said_float;
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::Function {
|
|
|
|
name: function_name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::NonConstructibleReturnType,
|
2022-05-01 05:22:19 +00:00
|
|
|
..
|
|
|
|
})
|
|
|
|
if function_name == "return_pointer"
|
|
|
|
}
|
|
|
|
|
|
|
|
check_validation! {
|
|
|
|
"
|
|
|
|
@group(0) @binding(0)
|
|
|
|
var<storage> atom: atomic<u32>;
|
|
|
|
|
|
|
|
fn return_atomic() -> atomic<u32> {
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::Function {
|
|
|
|
name: function_name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::NonConstructibleReturnType,
|
2022-05-01 05:22:19 +00:00
|
|
|
..
|
|
|
|
})
|
|
|
|
if function_name == "return_atomic"
|
|
|
|
}
|
2021-05-26 16:11:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 21:42:13 +00:00
|
|
|
#[test]
|
|
|
|
fn pointer_type_equivalence() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-09-16 21:42:13 +00:00
|
|
|
r#"
|
|
|
|
fn f(pv: ptr<function, vec2<f32>>, pf: ptr<function, f32>) { }
|
|
|
|
|
|
|
|
fn g() {
|
|
|
|
var m: mat2x2<f32>;
|
|
|
|
let pv: ptr<function, vec2<f32>> = &m.x;
|
|
|
|
let pf: ptr<function, f32> = &m.x.x;
|
|
|
|
|
|
|
|
f(pv, pf);
|
|
|
|
}
|
|
|
|
"#:
|
|
|
|
Ok(_)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 20:11:35 +00:00
|
|
|
#[test]
|
|
|
|
fn missing_bindings() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-05-21 20:11:35 +00:00
|
|
|
"
|
2023-04-12 14:48:57 +00:00
|
|
|
@fragment
|
|
|
|
fn fragment(_input: vec4<f32>) -> @location(0) vec4<f32> {
|
2022-04-17 13:56:54 +00:00
|
|
|
return _input;
|
2021-05-21 20:11:35 +00:00
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::EntryPoint {
|
2023-04-12 14:48:57 +00:00
|
|
|
stage: naga::ShaderStage::Fragment,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::EntryPointError::Argument(
|
2021-05-21 20:11:35 +00:00
|
|
|
0,
|
|
|
|
naga::valid::VaryingError::MissingBinding,
|
|
|
|
),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-05-21 20:11:35 +00:00
|
|
|
"
|
2023-04-12 14:48:57 +00:00
|
|
|
@fragment
|
|
|
|
fn fragment(@location(0) _input: vec4<f32>, more_input: f32) -> @location(0) vec4<f32> {
|
2022-04-17 13:56:54 +00:00
|
|
|
return _input + more_input;
|
2021-05-21 20:11:35 +00:00
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::EntryPoint {
|
2023-04-12 14:48:57 +00:00
|
|
|
stage: naga::ShaderStage::Fragment,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::EntryPointError::Argument(
|
2021-05-21 20:11:35 +00:00
|
|
|
1,
|
|
|
|
naga::valid::VaryingError::MissingBinding,
|
|
|
|
),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-05-21 20:11:35 +00:00
|
|
|
"
|
2023-04-12 14:48:57 +00:00
|
|
|
@fragment
|
|
|
|
fn fragment(@location(0) _input: vec4<f32>) -> vec4<f32> {
|
2022-04-17 13:56:54 +00:00
|
|
|
return _input;
|
2021-05-21 20:11:35 +00:00
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::EntryPoint {
|
2023-04-12 14:48:57 +00:00
|
|
|
stage: naga::ShaderStage::Fragment,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::EntryPointError::Result(
|
2021-05-21 20:11:35 +00:00
|
|
|
naga::valid::VaryingError::MissingBinding,
|
|
|
|
),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-05-21 20:11:35 +00:00
|
|
|
"
|
2023-04-12 14:48:57 +00:00
|
|
|
struct FragmentIn {
|
2022-03-12 21:48:10 +00:00
|
|
|
@location(0) pos: vec4<f32>,
|
|
|
|
uv: vec2<f32>
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2021-05-21 20:11:35 +00:00
|
|
|
|
2023-04-12 14:48:57 +00:00
|
|
|
@fragment
|
|
|
|
fn fragment(_input: FragmentIn) -> @location(0) vec4<f32> {
|
2022-04-17 13:56:54 +00:00
|
|
|
return _input.pos;
|
2021-05-21 20:11:35 +00:00
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::EntryPoint {
|
2023-04-12 14:48:57 +00:00
|
|
|
stage: naga::ShaderStage::Fragment,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::EntryPointError::Argument(
|
2021-05-21 20:11:35 +00:00
|
|
|
0,
|
|
|
|
naga::valid::VaryingError::MemberMissingBinding(1),
|
|
|
|
),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 16:57:20 +00:00
|
|
|
|
2023-04-12 14:48:57 +00:00
|
|
|
#[test]
|
|
|
|
fn missing_bindings2() {
|
|
|
|
check_validation! {
|
|
|
|
"
|
|
|
|
@vertex
|
|
|
|
fn vertex() {}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::EntryPoint {
|
|
|
|
stage: naga::ShaderStage::Vertex,
|
|
|
|
source: naga::valid::EntryPointError::MissingVertexOutputPosition,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
check_validation! {
|
|
|
|
"
|
|
|
|
struct VertexOut {
|
|
|
|
@location(0) a: vec4<f32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
@vertex
|
|
|
|
fn vertex() -> VertexOut {
|
|
|
|
return VertexOut(vec4<f32>());
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::EntryPoint {
|
|
|
|
stage: naga::ShaderStage::Vertex,
|
|
|
|
source: naga::valid::EntryPointError::MissingVertexOutputPosition,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:57:20 +00:00
|
|
|
#[test]
|
|
|
|
fn invalid_access() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-06-04 16:57:20 +00:00
|
|
|
"
|
|
|
|
fn array_by_value(a: array<i32, 5>, i: i32) -> i32 {
|
|
|
|
return a[i];
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn matrix_by_value(m: mat4x4<f32>, i: i32) -> vec4<f32> {
|
|
|
|
return m[i];
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::Function {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::Expression {
|
|
|
|
source: naga::valid::ExpressionError::IndexMustBeConstant(_),
|
2021-06-04 16:57:20 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
2021-05-27 22:02:38 +00:00
|
|
|
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-05-27 22:02:38 +00:00
|
|
|
r#"
|
|
|
|
fn main() -> f32 {
|
|
|
|
let a = array<f32, 3>(0., 1., 2.);
|
|
|
|
return a[3];
|
|
|
|
}
|
|
|
|
"#:
|
|
|
|
Err(naga::valid::ValidationError::Function {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::Expression {
|
|
|
|
source: naga::valid::ExpressionError::IndexOutOfBounds(_, _),
|
2021-05-27 22:02:38 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
2021-06-04 16:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn valid_access() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-06-04 16:57:20 +00:00
|
|
|
"
|
|
|
|
fn vector_by_value(v: vec4<i32>, i: i32) -> i32 {
|
|
|
|
return v[i];
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn matrix_dynamic(m: mat4x4<f32>, i: i32, j: i32) -> f32 {
|
|
|
|
var temp: mat4x4<f32> = m;
|
|
|
|
// Dynamically indexing the column vector applies
|
|
|
|
// `Access` to a `ValuePointer`.
|
|
|
|
return temp[i][j];
|
|
|
|
}
|
2021-09-15 02:59:59 +00:00
|
|
|
",
|
|
|
|
"
|
|
|
|
fn main() {
|
|
|
|
var v: vec4<f32> = vec4<f32>(1.0, 1.0, 1.0, 1.0);
|
|
|
|
let pv = &v;
|
|
|
|
let a = (*pv)[3];
|
|
|
|
}
|
2021-06-04 16:57:20 +00:00
|
|
|
":
|
|
|
|
Ok(_)
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 21:01:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_local_vars() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-05-27 21:01:40 +00:00
|
|
|
"
|
2022-03-27 05:35:08 +00:00
|
|
|
struct Unsized { data: array<f32> }
|
2021-05-27 21:01:40 +00:00
|
|
|
fn local_ptr_dynamic_array(okay: ptr<storage, Unsized>) {
|
2021-09-15 02:59:59 +00:00
|
|
|
var not_okay: ptr<storage, array<f32>> = &(*okay).data;
|
2021-05-27 21:01:40 +00:00
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::Function {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::LocalVariable {
|
2021-05-27 21:01:40 +00:00
|
|
|
name: local_var_name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::LocalVariableError::InvalidType(_),
|
2021-05-27 21:01:40 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
})
|
|
|
|
if local_var_name == "not_okay"
|
|
|
|
}
|
2023-10-08 20:53:05 +00:00
|
|
|
|
|
|
|
check_validation! {
|
|
|
|
"
|
|
|
|
fn f() {
|
|
|
|
var x: atomic<u32>;
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::Function {
|
|
|
|
source: naga::valid::FunctionError::LocalVariable {
|
|
|
|
name: local_var_name,
|
|
|
|
source: naga::valid::LocalVariableError::InvalidType(_),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
})
|
|
|
|
if local_var_name == "x"
|
|
|
|
}
|
2021-05-27 21:01:40 +00:00
|
|
|
}
|
2021-09-20 15:45:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dead_code() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-09-20 15:45:53 +00:00
|
|
|
"
|
|
|
|
fn dead_code_after_if(condition: bool) -> i32 {
|
|
|
|
if (condition) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Ok(_)
|
|
|
|
}
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-09-20 15:45:53 +00:00
|
|
|
"
|
|
|
|
fn dead_code_after_block() -> i32 {
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::Function {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::InstructionsAfterReturn,
|
2021-09-20 15:45:53 +00:00
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-10-01 13:40:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_runtime_sized_arrays() {
|
|
|
|
// You can't have structs whose last member is an unsized struct. An unsized
|
|
|
|
// array may only appear as the last member of a struct used directly as a
|
|
|
|
// variable's store type.
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-10-01 13:40:18 +00:00
|
|
|
"
|
|
|
|
struct Unsized {
|
2022-03-12 21:48:10 +00:00
|
|
|
arr: array<f32>
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2021-10-01 13:40:18 +00:00
|
|
|
|
|
|
|
struct Outer {
|
2022-03-12 21:48:10 +00:00
|
|
|
legit: i32,
|
2022-04-17 13:56:54 +00:00
|
|
|
_unsized: Unsized
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2021-10-01 13:40:18 +00:00
|
|
|
|
2022-01-19 15:33:06 +00:00
|
|
|
@group(0) @binding(0) var<storage> outer: Outer;
|
2021-10-01 13:40:18 +00:00
|
|
|
|
|
|
|
fn fetch(i: i32) -> f32 {
|
2022-04-17 13:56:54 +00:00
|
|
|
return outer._unsized.arr[i];
|
2021-10-01 13:40:18 +00:00
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(naga::valid::ValidationError::Type {
|
|
|
|
name: struct_name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::TypeError::InvalidDynamicArray(member_name, _),
|
2021-10-01 13:40:18 +00:00
|
|
|
..
|
|
|
|
})
|
2022-04-17 13:56:54 +00:00
|
|
|
if struct_name == "Outer" && member_name == "_unsized"
|
2021-10-01 13:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-01 16:47:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn select() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-10-01 16:47:39 +00:00
|
|
|
"
|
|
|
|
fn select_pointers(which: bool) -> i32 {
|
|
|
|
var x: i32 = 1;
|
|
|
|
var y: i32 = 2;
|
2022-04-17 13:56:54 +00:00
|
|
|
let p = select(&x, &y, which);
|
|
|
|
return *p;
|
2021-10-01 16:47:39 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn select_arrays(which: bool) -> i32 {
|
|
|
|
var x: array<i32, 4>;
|
|
|
|
var y: array<i32, 4>;
|
|
|
|
let s = select(x, y, which);
|
|
|
|
return s[0];
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
2022-03-27 05:35:08 +00:00
|
|
|
struct S { member: i32 }
|
2021-10-01 16:47:39 +00:00
|
|
|
fn select_structs(which: bool) -> S {
|
|
|
|
var x: S = S(1);
|
|
|
|
var y: S = S(2);
|
|
|
|
let s = select(x, y, which);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(
|
|
|
|
naga::valid::ValidationError::Function {
|
|
|
|
name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::Expression {
|
|
|
|
source: naga::valid::ExpressionError::InvalidSelectTypes,
|
2021-10-01 16:47:39 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if name.starts_with("select_")
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 08:59:15 +00:00
|
|
|
|
2021-11-15 00:07:55 +00:00
|
|
|
#[test]
|
|
|
|
fn missing_default_case() {
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-11-15 00:07:55 +00:00
|
|
|
"
|
|
|
|
fn test_missing_default_case() {
|
|
|
|
switch(0) {
|
|
|
|
case 0: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(
|
|
|
|
naga::valid::ValidationError::Function {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::MissingDefaultCase,
|
2021-11-15 00:07:55 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 06:46:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn wrong_access_mode() {
|
|
|
|
// The assignments to `global.i` should be forbidden, because they are in
|
|
|
|
// variables whose access mode is `read`, not `read_write`.
|
2022-04-29 20:18:13 +00:00
|
|
|
check_validation! {
|
2021-11-16 06:46:00 +00:00
|
|
|
"
|
|
|
|
struct Globals {
|
2022-03-12 21:48:10 +00:00
|
|
|
i: i32
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2021-11-16 06:46:00 +00:00
|
|
|
|
2022-01-19 15:33:06 +00:00
|
|
|
@group(0) @binding(0)
|
2021-11-16 06:46:00 +00:00
|
|
|
var<storage> globals: Globals;
|
|
|
|
|
|
|
|
fn store(v: i32) {
|
|
|
|
globals.i = v;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
struct Globals {
|
2022-03-12 21:48:10 +00:00
|
|
|
i: i32
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2021-11-16 06:46:00 +00:00
|
|
|
|
2022-01-19 15:33:06 +00:00
|
|
|
@group(0) @binding(0)
|
2021-11-16 06:46:00 +00:00
|
|
|
var<uniform> globals: Globals;
|
|
|
|
|
|
|
|
fn store(v: i32) {
|
|
|
|
globals.i = v;
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(
|
|
|
|
naga::valid::ValidationError::Function {
|
|
|
|
name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::InvalidStorePointer(_),
|
2021-11-16 06:46:00 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if name == "store"
|
|
|
|
}
|
|
|
|
}
|
2022-04-30 05:02:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn io_shareable_types() {
|
|
|
|
for numeric in "i32 u32 f32".split_whitespace() {
|
2023-01-31 00:34:04 +00:00
|
|
|
let types = format!("{numeric} vec2<{numeric}> vec3<{numeric}> vec4<{numeric}>");
|
2022-04-30 05:02:45 +00:00
|
|
|
for ty in types.split_whitespace() {
|
|
|
|
check_one_validation! {
|
|
|
|
&format!("@vertex
|
2023-01-31 00:34:04 +00:00
|
|
|
fn f(@location(0) arg: {ty}) -> @builtin(position) vec4<f32>
|
|
|
|
{{ return vec4<f32>(0.0); }}"),
|
2022-04-30 05:02:45 +00:00
|
|
|
Ok(_module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ty in "bool
|
|
|
|
vec2<bool> vec3<bool> vec4<bool>
|
|
|
|
array<f32,4>
|
|
|
|
mat2x2<f32>
|
|
|
|
ptr<function,f32>"
|
|
|
|
.split_whitespace()
|
|
|
|
{
|
|
|
|
check_one_validation! {
|
|
|
|
&format!("@vertex
|
2023-01-31 00:34:04 +00:00
|
|
|
fn f(@location(0) arg: {ty}) -> @builtin(position) vec4<f32>
|
|
|
|
{{ return vec4<f32>(0.0); }}"),
|
2022-04-30 05:02:45 +00:00
|
|
|
Err(
|
|
|
|
naga::valid::ValidationError::EntryPoint {
|
|
|
|
stage: naga::ShaderStage::Vertex,
|
|
|
|
name,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::EntryPointError::Argument(
|
2022-04-30 05:02:45 +00:00
|
|
|
0,
|
|
|
|
naga::valid::VaryingError::NotIOShareableType(
|
|
|
|
_,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if name == "f"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn host_shareable_types() {
|
|
|
|
// Host-shareable, constructible types.
|
|
|
|
let types = "i32 u32 f32
|
|
|
|
vec2<i32> vec3<u32> vec4<f32>
|
|
|
|
mat4x4<f32>
|
|
|
|
array<mat4x4<f32>,4>
|
|
|
|
AStruct";
|
|
|
|
for ty in types.split_whitespace() {
|
|
|
|
check_one_validation! {
|
|
|
|
&format!("struct AStruct {{ member: array<mat4x4<f32>, 8> }};
|
2023-01-31 00:34:04 +00:00
|
|
|
@group(0) @binding(0) var<uniform> ubuf: {ty};
|
|
|
|
@group(0) @binding(1) var<storage> sbuf: {ty};"),
|
2022-04-30 05:02:45 +00:00
|
|
|
Ok(_module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host-shareable but not constructible types.
|
|
|
|
let types = "atomic<i32> atomic<u32>
|
|
|
|
array<atomic<u32>,4>
|
|
|
|
array<u32>
|
|
|
|
AStruct";
|
|
|
|
for ty in types.split_whitespace() {
|
|
|
|
check_one_validation! {
|
|
|
|
&format!("struct AStruct {{ member: array<atomic<u32>, 8> }};
|
2023-01-31 00:34:04 +00:00
|
|
|
@group(0) @binding(1) var<storage> sbuf: {ty};"),
|
2022-04-30 05:02:45 +00:00
|
|
|
Ok(_module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Types that are neither host-shareable nor constructible.
|
|
|
|
for ty in "bool ptr<storage,i32>".split_whitespace() {
|
|
|
|
check_one_validation! {
|
2023-01-31 00:34:04 +00:00
|
|
|
&format!("@group(0) @binding(0) var<storage> sbuf: {ty};"),
|
2022-04-30 05:02:45 +00:00
|
|
|
Err(
|
|
|
|
naga::valid::ValidationError::GlobalVariable {
|
|
|
|
name,
|
|
|
|
handle: _,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::GlobalVariableError::MissingTypeFlags { .. },
|
2022-04-30 05:02:45 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if name == "sbuf"
|
|
|
|
}
|
|
|
|
|
|
|
|
check_one_validation! {
|
2023-01-31 00:34:04 +00:00
|
|
|
&format!("@group(0) @binding(0) var<uniform> ubuf: {ty};"),
|
2022-04-30 05:02:45 +00:00
|
|
|
Err(naga::valid::ValidationError::GlobalVariable {
|
|
|
|
name,
|
|
|
|
handle: _,
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::GlobalVariableError::MissingTypeFlags { .. },
|
2022-04-30 05:02:45 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if name == "ubuf"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 21:45:21 +00:00
|
|
|
|
2023-09-28 18:32:46 +00:00
|
|
|
#[test]
|
|
|
|
fn var_init() {
|
|
|
|
check_validation! {
|
|
|
|
"
|
|
|
|
var<workgroup> initialized: u32 = 0u;
|
|
|
|
":
|
|
|
|
Err(
|
|
|
|
naga::valid::ValidationError::GlobalVariable {
|
|
|
|
source: naga::valid::GlobalVariableError::InitializerNotAllowed(naga::AddressSpace::WorkGroup),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:45:21 +00:00
|
|
|
#[test]
|
|
|
|
fn misplaced_break_if() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn test_misplaced_break_if() {
|
|
|
|
loop {
|
|
|
|
break if true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: A break if is only allowed in a continuing block
|
|
|
|
┌─ wgsl:4:17
|
|
|
|
│
|
|
|
|
4 │ break if true;
|
|
|
|
│ ^^^^^^^^ not in a continuing block
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn break_if_bad_condition() {
|
|
|
|
check_validation! {
|
|
|
|
"
|
|
|
|
fn test_break_if_bad_condition() {
|
|
|
|
loop {
|
|
|
|
continuing {
|
|
|
|
break if 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
":
|
|
|
|
Err(
|
|
|
|
naga::valid::ValidationError::Function {
|
2022-10-25 23:06:59 +00:00
|
|
|
source: naga::valid::FunctionError::InvalidIfType(_),
|
2022-06-16 21:45:21 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 16:17:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn swizzle_assignment() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn f() {
|
|
|
|
var v = vec2(0);
|
|
|
|
v.xy = vec2(1);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: invalid left-hand side of assignment
|
|
|
|
┌─ wgsl:4:13
|
|
|
|
│
|
|
|
|
4 │ v.xy = vec2(1);
|
|
|
|
│ ^^^^ cannot assign to this expression
|
|
|
|
│
|
|
|
|
= note: WGSL does not support assignments to swizzles
|
|
|
|
= note: consider assigning each component individually
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn binary_statement() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn f() {
|
|
|
|
3 + 5;
|
|
|
|
}
|
|
|
|
",
|
2023-01-12 17:26:28 +00:00
|
|
|
r###"error: expected assignment or increment/decrement, found ';'
|
|
|
|
┌─ wgsl:3:18
|
2022-09-15 16:17:00 +00:00
|
|
|
│
|
|
|
|
3 │ 3 + 5;
|
2023-01-12 17:26:28 +00:00
|
|
|
│ ^ expected assignment or increment/decrement
|
2022-09-15 16:17:00 +00:00
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assign_to_expr() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn f() {
|
|
|
|
3 + 5 = 10;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: invalid left-hand side of assignment
|
|
|
|
┌─ wgsl:3:13
|
|
|
|
│
|
|
|
|
3 │ 3 + 5 = 10;
|
|
|
|
│ ^^^^^ cannot assign to this expression
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assign_to_let() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn f() {
|
|
|
|
let a = 10;
|
|
|
|
a = 20;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: invalid left-hand side of assignment
|
2023-01-31 16:37:01 +00:00
|
|
|
┌─ wgsl:3:17
|
2022-09-15 16:17:00 +00:00
|
|
|
│
|
2023-01-31 16:37:01 +00:00
|
|
|
3 │ let a = 10;
|
|
|
|
│ ^ this is an immutable binding
|
2022-09-15 16:17:00 +00:00
|
|
|
4 │ a = 20;
|
|
|
|
│ ^ cannot assign to this expression
|
|
|
|
│
|
2023-01-31 16:37:01 +00:00
|
|
|
= note: consider declaring 'a' with `var` instead of `let`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
2023-09-29 17:20:50 +00:00
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn f() {
|
|
|
|
let a = array(1, 2);
|
|
|
|
a[0] = 1;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: invalid left-hand side of assignment
|
|
|
|
┌─ wgsl:3:17
|
|
|
|
│
|
|
|
|
3 │ let a = array(1, 2);
|
|
|
|
│ ^ this is an immutable binding
|
|
|
|
4 │ a[0] = 1;
|
|
|
|
│ ^^^^ cannot assign to this expression
|
|
|
|
│
|
|
|
|
= note: consider declaring 'a' with `var` instead of `let`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
struct S { a: i32 }
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let a = S(10);
|
|
|
|
a.a = 20;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: invalid left-hand side of assignment
|
|
|
|
┌─ wgsl:5:17
|
|
|
|
│
|
|
|
|
5 │ let a = S(10);
|
|
|
|
│ ^ this is an immutable binding
|
|
|
|
6 │ a.a = 20;
|
|
|
|
│ ^^^ cannot assign to this expression
|
|
|
|
│
|
|
|
|
= note: consider declaring 'a' with `var` instead of `let`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
2022-09-15 16:17:00 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_function() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn f() {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: declaration of `f` is recursive
|
|
|
|
┌─ wgsl:2:12
|
|
|
|
│
|
|
|
|
2 │ fn f() {
|
|
|
|
│ ^
|
|
|
|
3 │ f();
|
|
|
|
│ ^ uses itself here
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cyclic_function() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn f() {
|
|
|
|
g();
|
|
|
|
}
|
|
|
|
fn g() {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: declaration of `f` is cyclic
|
|
|
|
┌─ wgsl:2:12
|
|
|
|
│
|
|
|
|
2 │ fn f() {
|
|
|
|
│ ^
|
|
|
|
3 │ g();
|
|
|
|
│ ^ uses `g`
|
|
|
|
4 │ }
|
|
|
|
5 │ fn g() {
|
|
|
|
│ ^
|
|
|
|
6 │ f();
|
|
|
|
│ ^ ending the cycle
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn switch_signed_unsigned_mismatch() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn x(y: u32) {
|
|
|
|
switch y {
|
|
|
|
case 1: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: invalid switch value
|
|
|
|
┌─ wgsl:4:16
|
|
|
|
│
|
|
|
|
4 │ case 1: {}
|
|
|
|
│ ^ expected unsigned integer
|
|
|
|
│
|
|
|
|
= note: suffix the integer with a `u`: '1u'
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn x(y: i32) {
|
|
|
|
switch y {
|
|
|
|
case 1u: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: invalid switch value
|
|
|
|
┌─ wgsl:4:16
|
|
|
|
│
|
|
|
|
4 │ case 1u: {}
|
|
|
|
│ ^^ expected signed integer
|
|
|
|
│
|
|
|
|
= note: remove the `u` suffix: '1'
|
|
|
|
|
|
|
|
"###,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_returns_void() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn x() {
|
2023-11-21 18:34:31 +00:00
|
|
|
let a = vec2<f32>(1.0, 2.0);
|
2023-01-12 17:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
|
|
|
let a = x();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: function does not return any value
|
|
|
|
┌─ wgsl:7:18
|
|
|
|
│
|
|
|
|
7 │ let a = x();
|
|
|
|
│ ^
|
|
|
|
│
|
|
|
|
= note: perhaps you meant to call the function in a separate statement?
|
|
|
|
|
|
|
|
"###,
|
|
|
|
)
|
|
|
|
}
|
2023-04-25 04:30:49 +00:00
|
|
|
|
2023-05-25 11:13:27 +00:00
|
|
|
#[test]
|
|
|
|
fn function_param_redefinition_as_param() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn x(a: f32, a: vec2<f32>) {}
|
|
|
|
",
|
|
|
|
r###"error: redefinition of `a`
|
|
|
|
┌─ wgsl:2:14
|
|
|
|
│
|
|
|
|
2 │ fn x(a: f32, a: vec2<f32>) {}
|
|
|
|
│ ^ ^ redefinition of `a`
|
|
|
|
│ │
|
|
|
|
│ previous definition of `a`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_param_redefinition_as_local() {
|
|
|
|
check(
|
|
|
|
"
|
|
|
|
fn x(a: f32) {
|
|
|
|
let a = 0.0;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r###"error: redefinition of `a`
|
|
|
|
┌─ wgsl:2:14
|
|
|
|
│
|
|
|
|
2 │ fn x(a: f32) {
|
|
|
|
│ ^ previous definition of `a`
|
|
|
|
3 │ let a = 0.0;
|
|
|
|
│ ^ redefinition of `a`
|
|
|
|
|
|
|
|
"###,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-25 04:30:49 +00:00
|
|
|
#[test]
|
|
|
|
fn binding_array_local() {
|
|
|
|
check_validation! {
|
|
|
|
"fn f() { var x: binding_array<sampler, 4>; }":
|
|
|
|
Err(_)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn binding_array_private() {
|
|
|
|
check_validation! {
|
|
|
|
"var<private> x: binding_array<sampler, 4>;":
|
|
|
|
Err(_)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn binding_array_non_struct() {
|
|
|
|
check_validation! {
|
|
|
|
"var<storage> x: binding_array<i32, 4>;":
|
|
|
|
Err(naga::valid::ValidationError::Type {
|
|
|
|
source: naga::valid::TypeError::BindingArrayBaseTypeNotStruct(_),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-11-16 02:40:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compaction_preserves_spans() {
|
|
|
|
let source = r#"
|
|
|
|
const a: i32 = -(-(-(-42i)));
|
|
|
|
const b: vec2<u32> = vec2<u32>(42u, 43i);
|
|
|
|
"#; // ^^^^^^^^^^^^^^^^^^^ correct error span: 68..87
|
|
|
|
let mut module = naga::front::wgsl::parse_str(source).expect("source ought to parse");
|
|
|
|
naga::compact::compact(&mut module);
|
|
|
|
let err = naga::valid::Validator::new(
|
|
|
|
naga::valid::ValidationFlags::all(),
|
|
|
|
naga::valid::Capabilities::default(),
|
|
|
|
)
|
|
|
|
.validate(&module)
|
|
|
|
.expect_err("source ought to fail validation");
|
|
|
|
|
|
|
|
// Ideally this would all just be a `matches!` with a big pattern,
|
|
|
|
// but the `Span` API is full of opaque structs.
|
|
|
|
let mut spans = err.spans();
|
|
|
|
let first_span = spans.next().expect("error should have at least one span").0;
|
|
|
|
if !matches!(
|
|
|
|
first_span.to_range(),
|
|
|
|
Some(std::ops::Range { start: 68, end: 87 })
|
|
|
|
) {
|
|
|
|
panic!("Error message has wrong span:\n\n{err:#?}");
|
|
|
|
}
|
|
|
|
}
|