[naga wgsl-in] Apply automatic conversions to values being assigned.

Apply automatic conversions to the right-hand sides of assignment and
compound assignment statements.

Fix `try_automatic_conversion_for_leaf_scalar` to handle arrays,
not just scalars, vectors, and matrices. Previously this was only used
for converting the right-hand sides of bit shift operators to `u32`,
and bit shift operators don't support arrays. But now we're using
`try_automatic_conversion_for_leaf_scalar` for ordinary assignments,
and you can assign arrays.

Update a test of propagating source code spans through compaction.
This test needs validation to report an error, but this commit's fix
causes the error in the test to be detected in the front end, which is
too early to exercise compaction. Use a new error that the front end
still won't notice.
This commit is contained in:
Jim Blandy authored and Erich Gubler committed 2025-01-06 17:29:58 -05:00
1 parent 04f0de6d64
commit 4f22f8d7b6
3 files changed
+63 -17

No files matched your search

+27 -1
View File
@@ -93,7 +93,7 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
}))
};
let expr_scalar = match expr_inner.scalar() {
let expr_scalar = match expr_inner.automatically_convertible_scalar(&self.module.types) {
Some(scalar) => scalar,
None => return Err(make_error()),
};
@@ -436,6 +436,32 @@ impl crate::TypeInner {
| Ti::BindingArray { .. } => None,
}
}
/// Return the leaf scalar type of `pointer`.
///
/// `pointer` must be a `TypeInner` representing a pointer type.
pub fn pointer_automatically_convertible_scalar(
&self,
types: &crate::UniqueArena<crate::Type>,
) -> Option<crate::Scalar> {
use crate::TypeInner as Ti;
match *self {
Ti::Scalar(scalar) | Ti::Vector { scalar, .. } | Ti::Matrix { scalar, .. } => {
Some(scalar)
}
Ti::Atomic(_) => None,
Ti::Pointer { base, .. } | Ti::Array { base, .. } => {
types[base].inner.automatically_convertible_scalar(types)
}
Ti::ValuePointer { scalar, .. } => Some(scalar),
Ti::Struct { .. }
| Ti::Image { .. }
| Ti::Sampler { .. }
| Ti::AccelerationStructure
| Ti::RayQuery
| Ti::BindingArray { .. } => None,
}
}
}
impl crate::Scalar {
+29 -12
View File
@@ -1700,31 +1700,48 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
} => {
let mut emitter = Emitter::default();
emitter.start(&ctx.function.expressions);
let target_span = ctx.ast_expressions.get_span(ast_target);
let target = self.expression_for_reference(
ast_target,
&mut ctx.as_expression(block, &mut emitter),
)?;
let mut value =
self.expression(value, &mut ctx.as_expression(block, &mut emitter))?;
let mut ectx = ctx.as_expression(block, &mut emitter);
let target = self.expression_for_reference(ast_target, &mut ectx)?;
let target_handle = match target {
Typed::Reference(handle) => handle,
Typed::Plain(handle) => {
let ty = ctx.invalid_assignment_type(handle);
return Err(Error::InvalidAssignment {
span: ctx.ast_expressions.get_span(ast_target),
span: target_span,
ty,
});
}
};
// Usually the value needs to be converted to match the type of
// the memory view you're assigning it to. The bit shift
// operators are exceptions, in that the right operand is always
// a `u32` or `vecN<u32>`.
let target_scalar = match op {
Some(crate::BinaryOperator::ShiftLeft | crate::BinaryOperator::ShiftRight) => {
Some(crate::Scalar::U32)
}
_ => resolve_inner!(ectx, target_handle)
.pointer_automatically_convertible_scalar(&ectx.module.types),
};
let value = self.expression_for_abstract(value, &mut ectx)?;
let mut value = match target_scalar {
Some(target_scalar) => ectx.try_automatic_conversion_for_leaf_scalar(
value,
target_scalar,
target_span,
)?,
None => value,
};
let value = match op {
Some(op) => {
let mut ctx = ctx.as_expression(block, &mut emitter);
let mut left = ctx.apply_load_rule(target)?;
ctx.binary_op_splat(op, &mut left, &mut value)?;
ctx.append_expression(
let mut left = ectx.apply_load_rule(target)?;
ectx.binary_op_splat(op, &mut left, &mut value)?;
ectx.append_expression(
crate::Expression::Binary {
op,
left,
+7 -4
View File
@@ -2108,10 +2108,10 @@ fn compaction_preserves_spans() {
let source = r#"
fn f() {
var a: i32 = -(-(-(-42i)));
var x: i32;
x = 42u;
var x: array<i32,1>;
var y = x[1.0];
}
"#; // ^^^ correct error span: 95..98
"#; // ^^^ correct error span: 108..114
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(
@@ -2135,7 +2135,10 @@ fn compaction_preserves_spans() {
.0;
if !matches!(
dest_span.to_range(),
Some(std::ops::Range { start: 95, end: 98 })
Some(std::ops::Range {
start: 108,
end: 114
})
) {
panic!("Error message has wrong span:\n\n{err:#?}");
}