{"record":{"id":"06062e3e61a070c3","repo":"tracel-ai/burn","slug":"expected-bool-handle-got-06062e","errorCode":null,"errorMessage":"Expected bool handle, got {}","messagePattern":"Expected bool handle, got (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/backend.rs","lineNumber":151,"sourceCode":"\n    fn float_tensor(handle: TensorHandle<Self::Handle>) -> FloatTensor<Self> {\n        match handle.handle {\n            HandleKind::Float(handle) => handle,\n            _ => panic!(\"Expected float handle, got {}\", handle.handle.name()),\n        }\n    }\n\n    fn int_tensor(handle: TensorHandle<Self::Handle>) -> IntTensor<Self> {\n        match handle.handle {\n            HandleKind::Int(handle) => handle,\n            _ => panic!(\"Expected int handle, got {}\", handle.handle.name()),\n        }\n    }\n\n    fn bool_tensor(handle: TensorHandle<Self::Handle>) -> BoolTensor<Self> {\n        match handle.handle {\n            HandleKind::Bool(handle) => handle,\n            _ => panic!(\"Expected bool handle, got {}\", handle.handle.name()),\n        }\n    }\n\n    fn quantized_tensor(handle: TensorHandle<Self::Handle>) -> QuantizedTensor<Self> {\n        match handle.handle {\n            HandleKind::Quantized(handle) => handle,\n            _ => panic!(\"Expected quantized handle, got {}\", handle.handle.name()),\n        }\n    }\n\n    fn float_tensor_handle(tensor: FloatTensor<Self>) -> Self::Handle {\n        HandleKind::Float(tensor)\n    }\n\n    fn int_tensor_handle(tensor: IntTensor<Self>) -> Self::Handle {\n        HandleKind::Int(tensor)\n    }\n","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/backend.rs#L133-L169","documentation":"`NdArray::bool_tensor` (BackendIr) panics when the TensorHandle's `HandleKind` is not `HandleKind::Bool` — a Float, Int, or Quantized handle was passed where a boolean mask tensor was expected. This fires during conversion of type-erased handles into bool tensor primitives (masks for masked_fill, where, etc.).","triggerScenarios":"Calling `NdArray::bool_tensor(handle)` (or `get_bool_tensor`) with a non-bool handle; commonly a float tensor (e.g. result of a comparison done in float, or 0.0/1.0 mask) is passed where a bool mask is required.","commonSituations":"Passing a 0/1 float mask instead of a bool mask to masked_fill/where; registering a mask tensor with the wrong TensorKind in custom IR; frontend code comparing tensors without casting the result to bool.","solutions":["Convert the mask to bool in the frontend: `mask.bool()` or compare with `mask.equal_elem(1.0)` to get a proper bool tensor.","Use the accessor matching the handle's actual kind (`float_tensor`/`int_tensor`) in custom backend code.","Fix the TensorKind registration so mask tensors are declared as Bool.","Inspect the producing op of the mask to see why it yields a non-bool dtype."],"exampleFix":"// before\nlet masked = x.mask_fill(float_mask, 0.0); // panics inside bool_tensor\n// after\nlet masked = x.mask_fill(float_mask.greater_elem(0.5), 0.0); // bool mask","handlingStrategy":"type-guard","validationCode":"fn ensure_bool_mask(t: &TensorIr) -> Result<(), String> {\n    (t.kind == TensorKind::Bool && t.dtype == DType::Bool)\n        .then_some(())\n        .ok_or_else(|| format!(\"expected bool mask, got kind {:?} dtype {:?}\", t.kind, t.dtype))\n}","typeGuard":"fn as_bool_handle(h: HandleKind<NdArray>) -> Option<ArrayHandle> {\n    match h { HandleKind::Bool(a) => Some(a), _ => None }\n}","tryCatchPattern":"let b = std::panic::catch_unwind(|| NdArray::bool_tensor(th))\n    .map_err(|_| anyhow!(\"handle is not bool; expected {}\", th.handle.name()))?;","preventionTips":["Use typed frontend tensors (Tensor<B,D,bool>) so masks are bool at the type level","Convert 0/1 float masks with .greater_elem(0.5) or .bool() before mask ops","Register mask tensors as TensorKind::Bool in custom IR","Never reuse a float tensor as a mask directly"],"tags":["panic","burn","ndarray","backend","handle-mismatch"],"backgroundTag":"handle-kind-mismatch","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}