{"record":{"id":"d93168ee95e2d79e","repo":"tracel-ai/burn","slug":"should-be-bool-got-float","errorCode":null,"errorMessage":"Should be bool, got float","messagePattern":"Should be bool, got float","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":76,"sourceCode":"    }\n\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>> {","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L58-L94","documentation":"BackendTensor::bool() was called on a Float tensor handle. Only the Bool variant can be unwrapped to a bool primitive, so the code panics. A float tensor reached a path expecting a boolean tensor, most often a mask/condition slot (where/masked_fill, comparison consumption).","triggerScenarios":"Calling bool() on a float tensor produced by float ops; passing float values (or 0.0/1.0 flags) where a boolean mask/condition is required; forgetting that comparison ops (==, <, >=) — not casts — produce Bool tensors.","commonSituations":"Using 0/1 float tensors as masks instead of comparison results; PyTorch-style implicit truthiness assumptions; upstream change replaced a Bool output with Float; custom kernels that assumed a mask input was Bool.","solutions":["Produce the mask via a comparison op (x.equal(y), x.lower(z)) instead of casting a float tensor","If a numeric-to-bool conversion is truly intended, apply it through an explicit backend cast to DType::Bool rather than bool() on the enum","Match the BackendTensor variant at the call site and handle Float deliberately","Validate dtype via TensorMetadata before consuming mask/condition tensors"],"exampleFix":"// before\nlet mask = handle.bool(); // panics: Float variant\n// after\nlet mask = handle.float().greater_elem(0.0); // build a real Bool mask via comparison","handlingStrategy":"type-guard","validationCode":"if !matches!(handle, BackendTensor::Bool(_)) {\n    panic!(\"expected bool mask, got {:?}\", handle.dtype());\n}","typeGuard":"fn is_bool<B: BackendTypes>(t: &BackendTensor<B>) -> bool {\n    matches!(t, BackendTensor::Bool(_))\n}","tryCatchPattern":null,"preventionTips":["Build masks with comparison ops, not numeric tensors","Use explicit casts to DType::Bool when a numeric-to-bool conversion is intended","Validate that mask/condition parameters are Bool at the API boundary"],"tags":["rust","panic","dtype-mismatch","bool"],"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"}