[naga] Add new function, GuardedIndex::from_expression.

Pull out the code to build a `naga::proc::index::GuardedIndex` from a
`Handle<Expression>` into its own function,
`GuardedIndex::from_expression`. Use that function in
`GuardedIndex::try_resolve_to_constant`.
This commit is contained in:
Jim Blandy 2024-10-10 12:45:55 -07:00
parent 0392613b5a
commit 0f17ad6455

View File

@ -340,12 +340,21 @@ impl GuardedIndex {
module: &crate::Module,
) {
if let GuardedIndex::Expression(expr) = *self {
if let Ok(value) = module
.to_ctx()
.eval_expr_to_u32_from(expr, &function.expressions)
{
*self = GuardedIndex::Known(value);
}
*self = GuardedIndex::from_expression(expr, function, module);
}
}
pub(crate) fn from_expression(
expr: Handle<crate::Expression>,
function: &crate::Function,
module: &crate::Module,
) -> Self {
match module
.to_ctx()
.eval_expr_to_u32_from(expr, &function.expressions)
{
Ok(value) => Self::Known(value),
Err(_) => Self::Expression(expr),
}
}
}