Skip to content

Commit

Permalink
[naga wgsl-in] Constructors with types don't make abstract values.
Browse files Browse the repository at this point in the history
When a constructor builtin has an explicit type parameter, like
`mat2x2<f32>`, it should not produce an abstract matrix, even if its
arguments are abstract.
  • Loading branch information
jimblandy authored and teoxoy committed Nov 29, 2023
1 parent fe4d412 commit 5a3887a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
30 changes: 22 additions & 8 deletions naga/src/front/wgsl/lower/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,20 +411,13 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
expr = crate::Expression::Compose { ty, components };
}

// Matrix constructor (by columns)
// Matrix constructor (by columns), partial
(
Components::Many {
mut components,
spans,
},
Constructor::PartialMatrix { columns, rows },
)
| (
Components::Many {
mut components,
spans,
},
Constructor::Type((_, &crate::TypeInner::Matrix { columns, rows, .. })),
) => {
let consensus_scalar =
automatic_conversion_consensus(&components, ctx).map_err(|index| {
Expand All @@ -439,6 +432,27 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
expr = crate::Expression::Compose { ty, components };
}

// Matrix constructor (by columns), type given
(
Components::Many { mut components, .. },
Constructor::Type((
ty,
&crate::TypeInner::Matrix {
columns: _,
rows,
scalar,
},
)),
) => {
let component_ty = crate::TypeInner::Vector { size: rows, scalar };
ctx.try_automatic_conversions_slice(
&mut components,
&Tr::Value(component_ty),
span,
)?;
expr = crate::Expression::Compose { ty, components };
}

// Array constructor - infer type
(components, Constructor::PartialArray) => {
let mut components = components.into_components_vec();
Expand Down
25 changes: 22 additions & 3 deletions naga/tests/wgsl_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,14 @@ fn constructor_parameter_type_mismatch() {
_ = mat2x2<f32>(array(0, 1), vec2(2, 3));
}
"#,
r#"error: invalid type for constructor component at index [0]
┌─ wgsl:3:33
r#"error: automatic conversions cannot convert `array<{AbstractInt}, 2>` to `vec2<f32>`
┌─ wgsl:3:21
3 │ _ = mat2x2<f32>(array(0, 1), vec2(2, 3));
│ ^^^^^^^^^^^ invalid component type
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
│ │ │
│ │ this expression has type array<{AbstractInt}, 2>
│ a value of type vec2<f32> is required here
"#,
);
Expand Down Expand Up @@ -829,6 +832,22 @@ fn matrix_with_bad_type() {
);
}

#[test]
fn matrix_constructor_inferred() {
check(
r#"
const m: mat2x2<f64> = mat2x2<f32>(vec2(0), vec2(1));
"#,
r#"error: the type of `m` is expected to be `mat2x2<f64>`, but got `mat2x2<f32>`
┌─ wgsl:2:19
2 │ const m: mat2x2<f64> = mat2x2<f32>(vec2(0), vec2(1));
│ ^ definition of `m`
"#,
);
}

/// Check the result of validating a WGSL program against a pattern.
///
/// Unless you are generating code programmatically, the
Expand Down

0 comments on commit 5a3887a

Please sign in to comment.