{"id":"88f6ffe03f3d0585","repo":"rust-lang/rust","slug":"slice-element-type-unknown","errorCode":null,"errorMessage":"slice element type unknown","messagePattern":"slice element type unknown","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_codegen_llvm/src/builder/autodiff.rs","lineNumber":54,"sourceCode":"\n    // FIXME(Sa4dUs): pass proper varargs once we have support for differentiating variadic functions\n    let Ok(fn_abi) = tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((fn_sig, ty::List::empty())))\n    else {\n        bug!(\"failed to get fn_abi of fn_ptr with empty varargs\");\n    };\n\n    let mut new_activities = vec![];\n    let mut new_positions = vec![];\n    let mut del_activities = 0;\n    for (i, ty) in sig.inputs().iter().enumerate() {\n        if let Some(inner_ty) = ty.builtin_deref(true) {\n            if inner_ty.is_slice() {\n                // Now we need to figure out the size of each slice element in memory to allow\n                // safety checks and usability improvements in the backend.\n                let sty = match inner_ty.builtin_index() {\n                    Some(sty) => sty,\n                    None => {\n                        panic!(\"slice element type unknown\");\n                    }\n                };\n                let pci = PseudoCanonicalInput {\n                    typing_env: TypingEnv::fully_monomorphized(),\n                    value: sty,\n                };\n\n                let layout = tcx.layout_of(pci);\n                let elem_size = match layout {\n                    Ok(layout) => layout.size,\n                    Err(_) => {\n                        bug!(\"autodiff failed to compute slice element size\");\n                    }\n                };\n                let elem_size: u32 = elem_size.bytes() as u32;\n\n                // We know that the length will be passed as extra arg.\n                if !da.is_empty() {","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_codegen_llvm/src/builder/autodiff.rs#L36-L72","documentation":"Thrown by `panic!(\"slice element type unknown\")` in `adjust_activity_to_abi` (autodiff.rs:54) inside the Enzyme-based automatic differentiation (`#[autodiff]`) codegen. When a differentiated function has a `&[T]` / `&mut [T]` argument, rustc needs the element type's memory size; it calls `inner_ty.builtin_index()` to recover `T`, and if that returns `None` (the slice element isn't a builtin indexable type) it panics. This is a known incompleteness of autodiff: it cannot yet differentiate slices whose element type is itself unsized, generic-opaque, or a trait object.","triggerScenarios":"Triggered when a function annotated with `#[autodiff(...)]` (the `autodiff` nightly feature) takes a slice/reference argument whose element type has no `builtin_index()` — e.g. `&[&dyn Trait]`, a slice of an unsized/generic type, or a user type that does not reduce to a builtin index type. Differentiating such a function causes the codegen to hit the `None` arm and panic.","commonSituations":"Early experimentation with `#[autodiff]` on ML/numerical code that passes slices of trait objects or nested references; using autodiff on generic functions not yet monomorphized to a concrete indexable `T`; relying on autodiff before it supported all slice element shapes. Feature-gated and evolving.","solutions":["Avoid differentiating functions whose slice element is non-indexable: monomorphize to a concrete indexable element type (e.g. `&[f64]`, `&[Vec<f64>]` of known layout).","Replace `&[&dyn Trait]` / unsized-element slices with a concrete struct or `Vec<T>` of known-size elements before differentiating.","Disable `#[autodiff]` on the offending function until autodiff gains support for that element type.","Check the rustc/autodiff version — support is being added; update to a newer nightly that handles your slice type.","File an issue in the autodiff/rustc tracking repo with the exact signature, as this is a 'not yet supported' path, not a user-config error."],"exampleFix":"// before: differentiating a slice whose element has no builtin index\n#[autodiff(df, dup, Active, Duplicated)]\nfn grad(xs: &[Box<dyn Fn(f64)->f64>]) -> f64 { /* ... */ }  // panic: slice element type unknown\n\n// after: differentiate a slice of a concrete, indexable element type\n#[autodiff(df, dup, Active, Duplicated)]\nfn grad(xs: &[f64]) -> f64 { /* ... */ }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"trait KnownSliceElem: Sized + Copy {}\nimpl KnownSliceElem for f32 {}\nimpl KnownSliceElem for f64 {}\nimpl KnownSliceElem for f32x4 {}\n// Only allow autodiff over slices whose element type has a statically known layout:\nfn diff_slice<T: KnownSliceElem>(x: &[T]) { /* safe to pass to autodiff */ }","tryCatchPattern":null,"preventionTips":["Only pass slices of concrete, monomorphic, fully-Sized element types to autodiff functions.","Add explicit type annotations on slice parameters so element type resolution is unambiguous.","Avoid dyn Trait, opaque impl Trait, and generic T without a KnownSliceElem bound.","Keep element types to primitives or fixed-layout structs when differentiating."],"tags":["rustc","autodiff","enzyme","codegen","nightly"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}