mirror of
https://github.com/storytold/wgpu.git
synced 2026-12-01 10:34:57 +01:00
[wgsl] allow concretization to other valid scalars
This commit is contained in:
1 parent
990e12ebbc
commit
0a4cfc40f1
2 files changed
+43
-28
No files matched your search
@@ -480,8 +480,7 @@ pub(crate) struct AutoConversionLeafScalarError {
|
||||
pub(crate) struct ConcretizationFailedError {
|
||||
pub expr_span: Span,
|
||||
pub expr_type: String,
|
||||
pub scalar: String,
|
||||
pub inner: ConstantEvaluatorError,
|
||||
pub concretization_preferences: Vec<(String, ConstantEvaluatorError)>,
|
||||
}
|
||||
|
||||
impl<'a> Error<'a> {
|
||||
@@ -1161,19 +1160,20 @@ impl<'a> Error<'a> {
|
||||
let ConcretizationFailedError {
|
||||
expr_span,
|
||||
ref expr_type,
|
||||
ref scalar,
|
||||
ref inner,
|
||||
ref concretization_preferences,
|
||||
} = **error;
|
||||
ParseError {
|
||||
message: format!("failed to convert expression to a concrete type: {inner}"),
|
||||
message: "failed to convert expression to a concrete type".to_string(),
|
||||
labels: vec![(
|
||||
expr_span,
|
||||
format!("this expression has type {expr_type}").into(),
|
||||
)],
|
||||
notes: vec![format!(
|
||||
"the expression should have been converted to have {} scalar type",
|
||||
scalar
|
||||
)],
|
||||
notes: concretization_preferences
|
||||
.iter()
|
||||
.map(|&(ref scalar, ref err)|
|
||||
format!("the expression couldn't be converted to have {scalar} scalar type: {err}")
|
||||
)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
Error::ExceededLimitForNestedBraces { span, limit } => ParseError {
|
||||
|
||||
@@ -256,29 +256,44 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
|
||||
/// If `expr` is already concrete, return it unchanged.
|
||||
pub fn concretize(
|
||||
&mut self,
|
||||
mut expr: Handle<crate::Expression>,
|
||||
expr: Handle<crate::Expression>,
|
||||
) -> Result<'source, Handle<crate::Expression>> {
|
||||
let inner = super::resolve_inner!(self, expr);
|
||||
if let Some(scalar) = inner.automatically_convertible_scalar(&self.module.types) {
|
||||
let concretized = scalar.concretize();
|
||||
if concretized != scalar {
|
||||
assert!(scalar.is_abstract());
|
||||
let expr_span = self.get_expression_span(expr);
|
||||
expr = self
|
||||
use crate::ScalarKind as Sk;
|
||||
let concretization_preferences = match scalar.kind {
|
||||
// already concrete
|
||||
Sk::Sint | Sk::Uint | Sk::Float | Sk::Bool => return Ok(expr),
|
||||
Sk::AbstractInt => {
|
||||
[crate::Scalar::I32, crate::Scalar::U32, crate::Scalar::F32].as_slice()
|
||||
}
|
||||
Sk::AbstractFloat => [crate::Scalar::F32].as_slice(),
|
||||
};
|
||||
let expr_span = self.get_expression_span(expr);
|
||||
let mut errors = Vec::new();
|
||||
for concrete_scalar in concretization_preferences {
|
||||
match self
|
||||
.as_const_evaluator()
|
||||
.cast_array(expr, concretized, expr_span)
|
||||
.map_err(|err| {
|
||||
// A `TypeResolution` includes the type's full name, if
|
||||
// it has one. Also, avoid holding the borrow of `inner`
|
||||
// across the call to `cast_array`.
|
||||
let expr_type = &self.typifier()[expr];
|
||||
super::Error::ConcretizationFailed(Box::new(ConcretizationFailedError {
|
||||
expr_span,
|
||||
expr_type: self.type_resolution_to_string(expr_type),
|
||||
scalar: concretized.to_wgsl_for_diagnostics(),
|
||||
inner: err,
|
||||
}))
|
||||
})?;
|
||||
.cast_array(expr, *concrete_scalar, expr_span)
|
||||
{
|
||||
Ok(expr) => return Ok(expr),
|
||||
Err(err) => {
|
||||
errors.push((concrete_scalar.to_wgsl_for_diagnostics(), err));
|
||||
}
|
||||
}
|
||||
}
|
||||
if !errors.is_empty() {
|
||||
// A `TypeResolution` includes the type's full name, if
|
||||
// it has one. Also, avoid holding the borrow of `inner`
|
||||
// across the call to `cast_array`.
|
||||
let expr_type = &self.typifier()[expr];
|
||||
return Err(Box::new(super::Error::ConcretizationFailed(Box::new(
|
||||
ConcretizationFailedError {
|
||||
expr_span,
|
||||
expr_type: self.type_resolution_to_string(expr_type),
|
||||
concretization_preferences: errors,
|
||||
},
|
||||
))));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user