{"record":{"id":"50908bf62df5f975","repo":"tracel-ai/burn","slug":"should-be-float-got-bool","errorCode":null,"errorMessage":"Should be float, got bool","messagePattern":"Should be float, got bool","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":42,"sourceCode":"    Float(B::FloatTensorPrimitive),\n    /// Int tensor handle.\n    Int(B::IntTensorPrimitive),\n    /// Bool tensor handle.\n    Bool(B::BoolTensorPrimitive),\n    /// Quantized tensor handle.\n    Quantized(B::QuantizedTensorPrimitive),\n    #[cfg(feature = \"autodiff\")]\n    /// Autodiff float tensor handle.\n    Autodiff(FloatTensor<Autodiff<B>>),\n}\n\nimpl<B: Backend> BackendTensor<B> {\n    /// Returns the inner float tensor primitive.\n    pub fn float(self) -> B::FloatTensorPrimitive {\n        match self {\n            BackendTensor::Float(tensor) => tensor,\n            BackendTensor::Int(_) => panic!(\"Should be float, got int\"),\n            BackendTensor::Bool(_) => panic!(\"Should be float, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be float, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be float, got autodiff\"),\n        }\n    }\n    /// Returns the inner float tensor primitive.\n    pub fn as_float(&self) -> &B::FloatTensorPrimitive {\n        match self {\n            BackendTensor::Float(tensor) => tensor,\n            BackendTensor::Int(_) => panic!(\"Should be float, got int\"),\n            BackendTensor::Bool(_) => panic!(\"Should be float, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be float, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be float, got autodiff\"),\n        }\n    }\n\n    /// Returns the inner int tensor primitive.","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L24-L60","documentation":"BackendTensor::float() panics with this message when called on a Bool tensor. The method only unwraps the Float variant; boolean tensors (from comparisons/logical ops) must be converted to a numeric dtype before float extraction, so receiving one indicates a dtype-handling bug.","triggerScenarios":"Calling BackendTensor::float() on a tensor holding BackendTensor::Bool(_), typically the result of comparison (==, <, >), logical ops, or mask creation flowing into float-only code.","commonSituations":"Using boolean masks as if they were float tensors (e.g., multiplying without casting); APIs whose signatures changed to accept generic BackendTensor where callers pass masks directly.","solutions":["Cast the Bool tensor to float first (bool -> {0.0, 1.0} cast op) before calling .float().","Match on the BackendTensor variant and convert Bool explicitly with a numeric cast.","If the tensor should never be bool, fix the upstream op producing it (e.g., use a numeric comparison result instead of a mask).","Use checked accessors like as_float() with a variant check for mixed-dtype paths."],"exampleFix":"// before\nlet f = mask_tensor.float(); // panics: Bool\n// after\nlet f = BackendTensor::Float(bool_to_float_cast::<B>(mask_bool)).float(); // or mask_bool.cast::<f32>() upstream","handlingStrategy":"type-guard","validationCode":"// before calling .float()\nif let BackendTensor::Bool(_) = &tensor {\n    // cast bool to float first\n}","typeGuard":"fn is_bool_tensor<B: Backend>(t: &BackendTensor<B>) -> bool {\n    matches!(t, BackendTensor::Bool(_))\n}","tryCatchPattern":"// guard rather than catch the panic\nlet f = match tensor {\n    BackendTensor::Bool(b) => BackendTensor::Float(bool_cast::<B>(b)),\n    BackendTensor::Float(f) => f,\n    other => bail!(\"expected float or bool, got {other:?}\"),\n};","preventionTips":["Cast boolean masks to numeric dtype before arithmetic.","Track mask-producing ops (comparisons, logical ops) and convert them at the boundary.","Use typed tensor APIs where the dtype is encoded in the type to get compile-time errors instead."],"tags":["rust","dtype","tensor","bool","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"}