{"record":{"id":"1554eaf91e479d66","repo":"tracel-ai/burn","slug":"internal-error-entered-unreachable-code","errorCode":null,"errorMessage":"internal error: entered unreachable code","messagePattern":"internal error: entered unreachable code","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":98,"sourceCode":"            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 {\n            BackendTensor::Autodiff(tensor) => tensor,\n            // NOTE: this is the panicking code reached in tensor.rs:74:18:\n            _ => unreachable!(),\n        }\n    }\n\n    #[cfg(feature = \"autodiff\")]\n    /// Returns the inner autodiff tensor primitive.\n    pub fn as_autodiff(&self) -> &FloatTensor<Autodiff<B>> {\n        match self {\n            BackendTensor::Autodiff(tensor) => tensor,\n            _ => unreachable!(),\n        }\n    }\n\n    #[cfg(feature = \"autodiff\")]\n    /// Returns the inner autodiff tensor primitive.\n    pub fn autodiff_inner(self) -> B::FloatTensorPrimitive {\n        match self {\n            BackendTensor::Autodiff(tensor) => tensor.primitive,\n            _ => unreachable!(),","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L80-L116","documentation":"BackendTensor::autodiff() was called on a tensor that is not the Autodiff variant; the fallback arm uses unreachable!(), which panics with 'internal error: entered unreachable code'. The author treated the non-autodiff case as impossible, but at runtime a plain Float/Int/Bool/Quantized tensor reached this function — e.g. calling autodiff() on a tensor from a non-gradient (inference) path.","triggerScenarios":"Calling BackendTensor::autodiff() on a handle built from BackendTensor::Float/Int/Bool/Quantized; running under the autodiff feature but invoking autodiff() in inference mode (no_grad/valid) where handles are plain Float primitives.","commonSituations":"Mixing inference-mode tensors with training-mode code paths; passing a tensor created outside the Autodiff backend into gradient-requiring code; missing mark/convert to Autodiff before calling autodiff().","solutions":["Ensure the tensor was created through the Autodiff backend (Autodiff::float / marked as requiring gradients) before calling autodiff()","Only call autodiff() on training-path tensors; use the non-autodiff primitive in inference paths","Match the enum yourself and produce a descriptive error instead of relying on the unreachable!() arm","Verify the feature configuration: under feature = \"autodiff\" all tensors in the training graph must be Autodiff-wrapped"],"exampleFix":"// before\nlet inner = handle.autodiff(); // panics if not Autodiff variant\n// after\nlet inner = match handle {\n    BackendTensor::Autodiff(t) => t,\n    other => panic!(\"expected autodiff tensor, got {:?}; create it via Autodiff backend\", std::mem::discriminant(&other) != std::mem::discriminant(&handle)),\n};","handlingStrategy":"type-guard","validationCode":"// only call autodiff() when the handle is the Autodiff variant\nif !matches!(handle, BackendTensor::Autodiff(_)) {\n    panic!(\"autodiff() requires a training-mode (Autodiff) tensor\");\n}","typeGuard":"#[cfg(feature = \"autodiff\")]\nfn is_autodiff<B: BackendTypes>(t: &BackendTensor<B>) -> bool {\n    matches!(t, BackendTensor::Autodiff(_))\n}","tryCatchPattern":null,"preventionTips":["Create training tensors through the Autodiff backend so the variant is Autodiff","Do not call autodiff() on inference/valid-path tensors","Replace unreachable!() fallbacks with descriptive panics or Result in library code you control"],"tags":["rust","panic","unreachable","autodiff"],"backgroundTag":"tensor-variant-unreachable","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"}