{"record":{"id":"1b4d0f4e38f7c1f5","repo":"tracel-ai/burn","slug":"expected-bool-data-type-got-dtype","errorCode":null,"errorMessage":"Expected bool data type, got {dtype:?}","messagePattern":"Expected bool data type, got (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tensor/src/bridge/ops/bool.rs","lineNumber":24,"sourceCode":"use burn_dispatch::Dispatch;\nuse burn_std::{DType, ExecutionError, IndexingUpdateOp, Shape, Slice};\n\nuse crate::{\n    Bool, Device,\n    bridge::{BasicAutodiffOps, BasicOps, TransactionOp},\n    ops::BridgeTensor,\n};\n\nimpl TransactionOp for Bool {\n    fn register_transaction(tr: &mut TransactionPrimitive<Dispatch>, tensor: BridgeTensor) {\n        tr.register_bool(tensor.into());\n    }\n}\n\nimpl BasicOps for Bool {\n    fn empty(shape: Shape, device: &Device, dtype: DType) -> BridgeTensor {\n        if !dtype.is_bool() {\n            panic!(\"Expected bool data type, got {dtype:?}\");\n        }\n        BridgeTensor::bool(Dispatch::bool_empty(\n            shape,\n            device.as_dispatch(),\n            dtype.into(),\n        ))\n    }\n\n    fn zeros(shape: Shape, device: &Device, dtype: DType) -> BridgeTensor {\n        if !dtype.is_bool() {\n            panic!(\"Expected bool data type, got {dtype:?}\");\n        }\n        BridgeTensor::bool(Dispatch::bool_zeros(\n            shape,\n            device.as_dispatch(),\n            dtype.into(),\n        ))\n    }","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tensor/src/bridge/ops/bool.rs#L6-L42","documentation":"Bool::empty in the tensor bridge validates that the requested DType is Bool before creating an empty bool tensor; any other dtype panics. This is an internal consistency guard: the bool bridge implementation must only ever be asked for bool tensors, so receiving a non-bool dtype is a dispatch/typing bug.","triggerScenarios":"Calling Tensor::<B, D, Bool>::empty(shape, device) — or any API resolving to BasicOps::empty on the Bool element type — with a dtype that is not Bool (e.g. via a generic helper that passes Float32/Int64 as the dtype).","commonSituations":"Generic tensor-building code parameterized over dtype that constructs Bool tensors with the wrong DType; a dispatch bug after changing dtype handling in the bridge; hand-written code that mixes the float/int/bool tensor kinds.","solutions":["Pass DType::BOOL (or no explicit dtype, letting Bool default to it) when creating boolean tensors via Tensor::<B, D, Bool>::empty","Audit generic code that threads a DType through tensor constructors and ensure it matches the element type parameter E","If you need an integer/float tensor, use Tensor::<B, D, Float or Int>::empty instead of the Bool kind"],"exampleFix":"// before\nlet t = Tensor::<B, 2, Bool>::empty([2, 2], &device); // if DType got set to Float32 upstream\nlet t = Tensor::<B, 2, Bool>::empty_dtype(&device, DType::BOOL, [2, 2]);\n// after: ensure dtype is DType::BOOL when constructing Bool tensors","handlingStrategy":"validation","validationCode":"fn create_bool_empty_checked<B: Backend, const D: usize>(shape: [usize; D], device: &B::Device, dtype: DType) -> Tensor<B, D, Bool> {\n    assert!(dtype.is_bool(), \"Bool::empty requires DType::BOOL, got {dtype:?}\");\n    Tensor::<B, D, Bool>::empty(shape, device)\n}","typeGuard":"fn is_bool_dtype(dtype: DType) -> bool { dtype.is_bool() }","tryCatchPattern":"std::panic::catch_unwind(|| Tensor::<B, 2, Bool>::empty(shape, &device))\n    .map_err(|_| \"dtype passed to Bool::empty was not Bool\")?;","preventionTips":["Never pass an explicit dtype other than DType::BOOL to Bool-kind tensor constructors","Derive dtype from the element type instead of threading a free DType parameter through generic helpers","Add unit tests for generic tensor builders covering Bool, Int, and Float element types"],"tags":["rust","burn","dtype-mismatch","bool","tensor-bridge"],"backgroundTag":"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"}