{"record":{"id":"99c6636c42e9756e","repo":"tracel-ai/burn","slug":"should-be-bool-got-int","errorCode":null,"errorMessage":"Should be bool, got int","messagePattern":"Should be bool, got int","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":77,"sourceCode":"\n    /// Returns the inner int tensor primitive.\n    pub fn int(self) -> B::IntTensorPrimitive {\n        match self {\n            BackendTensor::Int(tensor) => tensor,\n            BackendTensor::Float(_) => panic!(\"Should be int, got float\"),\n            BackendTensor::Bool(_) => panic!(\"Should be int, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be int, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be int, got autodiff\"),\n        }\n    }\n\n    /// Returns the inner bool tensor primitive.\n    pub fn bool(self) -> B::BoolTensorPrimitive {\n        match self {\n            BackendTensor::Bool(tensor) => tensor,\n            BackendTensor::Float(_) => panic!(\"Should be bool, got float\"),\n            BackendTensor::Int(_) => panic!(\"Should be bool, got int\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be bool, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be bool, got autodiff\"),\n        }\n    }\n\n    /// Returns the inner quantized tensor primitive.\n    pub fn quantized(self) -> B::QuantizedTensorPrimitive {\n        match self {\n            BackendTensor::Quantized(tensor) => tensor,\n            _ => unreachable!(),\n        }\n    }\n\n    #[cfg(feature = \"autodiff\")]\n    /// Returns the inner autodiff tensor primitive.\n    pub fn autodiff(self) -> FloatTensor<Autodiff<B>> {\n        match self {","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L59-L95","documentation":"`BackendTensor::bool()` unwraps the enum variant holding a bool tensor primitive and returns it. Because the enum can hold Float, Int, Quantized, or Autodiff variants, calling `bool()` on a tensor that is actually an Int tensor cannot return a value, so the library panics with 'Should be bool, got int'. It is a guard against mis-typed tensor access in the dispatch layer.","triggerScenarios":"Calling `BackendTensor::bool()` on a wrapper whose inner variant is `BackendTensor::Int(_)` — e.g. retrieving a tensor result from a kernel/op dispatch assuming it is bool when the op produced an integer tensor.","commonSituations":"Developers mixing up dtype when reading op outputs (e.g. treating an `int` mask/comparison result as bool), or generic code that downcasts tensors without checking the variant first.","solutions":["Check the tensor's dtype/variant before calling `bool()` and convert explicitly (e.g. cast int -> bool via a comparison or `bool_cast` op) first.","Match on `BackendTensor` yourself and handle each variant so the wrong dtype is handled gracefully.","Trace upstream op calls to fix the source that produced an Int tensor where a Bool tensor was expected."],"exampleFix":"// before\nlet b = tensor.bool(); // panics: got int\n// after\nlet b = match tensor {\n    BackendTensor::Bool(t) => t,\n    BackendTensor::Int(t) => t.equal_elem(0).bool(), // explicit conversion path\n    other => panic!(\"unexpected variant\"),\n};","handlingStrategy":"type-guard","validationCode":"if !matches!(tensor, BackendTensor::Bool(_)) { /* handle wrong dtype before calling .bool() */ }","typeGuard":"fn is_bool_tensor<B: Backend>(t: &BackendTensor<B>) -> bool {\n    matches!(t, BackendTensor::Bool(_))\n}","tryCatchPattern":"// Rust panics are not catchable in stable without panic::catch_unwind\nlet result = std::panic::catch_unwind(AssertUnwindSafe(|| tensor.clone().bool()));\nmatch result {\n    Ok(b) => use_bool(b),\n    Err(_) => eprintln!(\"tensor was not bool\"),\n}","preventionTips":["Track tensor dtype in your own types and only call `.bool()` on bool-typed wrappers.","Use matches! or a match statement to narrow the variant before unwrapping.","Add debug_assert! checks at op boundaries to catch dtype drift early."],"tags":["rust","tensor","dtype-mismatch","panic"],"backgroundTag":"tensor-dtype-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"}