gfx-rs/wgpu · error
Unexpected max({:?})
Error message
Unexpected max({:?}) What it means
Same family as 108 but for MathFunction::Max: the backend maps arg_scalar_kind Float/Sint/Uint to GlslStd450 FMax/SMax/UMax; any other kind (including Bool — note the WGSL spec only defines max for numeric types) triggers `unimplemented!("Unexpected max({:?})", other)` in cache_expression_value under write_block, aborting SPIR-V generation.
Source
Thrown at naga/src/back/spv/block.rs:1412
result_type_id,
id,
arg0_id,
))
}
other => unimplemented!("Unexpected abs({:?})", other),
}
}
Mf::Min => MathOp::Ext(match arg_scalar_kind {
Some(crate::ScalarKind::Float) => spirv::GlslStd450Op::FMin,
Some(crate::ScalarKind::Sint) => spirv::GlslStd450Op::SMin,
Some(crate::ScalarKind::Uint) => spirv::GlslStd450Op::UMin,
other => unimplemented!("Unexpected min({:?})", other),
}),
Mf::Max => MathOp::Ext(match arg_scalar_kind {
Some(crate::ScalarKind::Float) => spirv::GlslStd450Op::FMax,
Some(crate::ScalarKind::Sint) => spirv::GlslStd450Op::SMax,
Some(crate::ScalarKind::Uint) => spirv::GlslStd450Op::UMax,
other => unimplemented!("Unexpected max({:?})", other),
}),
Mf::Clamp => match arg_scalar_kind {
// Clamp is undefined if min > max. In practice this means it can use a median-of-three
// instruction to determine the value. This is fine according to the WGSL spec for float
// clamp, but integer clamp _must_ use min-max. As such we write out min/max.
Some(crate::ScalarKind::Float) => MathOp::Ext(spirv::GlslStd450Op::FClamp),
Some(_) => {
let (min_op, max_op) = match arg_scalar_kind {
Some(crate::ScalarKind::Sint) => {
(spirv::GlslStd450Op::SMin, spirv::GlslStd450Op::SMax)
}
Some(crate::ScalarKind::Uint) => {
(spirv::GlslStd450Op::UMin, spirv::GlslStd450Op::UMax)
}
_ => unreachable!(),
};
let max_id = self.gen_id();View on GitHub (pinned to 3e11ff59bf)
Solutions
- Restrict max() calls to numeric (f32/i32/u32, or matching vectors) arguments
- Validate the IR module before SPIR-V emission
- Dump IR to identify the bad max call site
- File a naga issue with the shader if it is valid WGSL
Example fix
// before let m = max(flag_a, flag_b); // bools // after let m = select(flag_b, flag_a, flag_a); // or compute on numeric values
Defensive patterns
Strategy: validation
Validate before calling
assert!(matches!(arg_scalar_kind, Some(crate::ScalarKind::Float | crate::ScalarKind::Sint | crate::ScalarKind::Uint)), "max() requires numeric argument");
Type guard
fn min_max_supported(kind: Option<crate::ScalarKind>) -> bool {
matches!(kind, Some(crate::ScalarKind::Float | crate::ScalarKind::Sint | crate::ScalarKind::Uint))
} Prevention
- Use max only on numeric scalars/vectors
- Validate IR before invoking the SPIR-V writer
- Wrap untrusted-shader translation in catch_unwind and report panics upstream
When it happens
Trigger: A `max(a, b)` call whose argument scalar kind is None or otherwise unmapped (bool, composite, pointer) when the SPIR-V backend lowers math functions.
Common situations: Fuzzed/unvalidated IR with max over booleans; calling naga's spv backend directly on hand-built modules; type-inference changes after naga upgrades altering arg_scalar_kind.
Related errors
- Unexpected abs({:?})
- Unexpected min({:?})
- not implemented
- metal does not support vertex ray hit return
- not implemented
AI-assisted analysis of gfx-rs/wgpu@3e11ff59bf (2026-09-03).
Data as JSON: /api/errors/c84f7894269ac00d.
Report an issue: GitHub.