rust-lang/rust · error
slice element type unknown
Error message
slice element type unknown
What it means
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.
Source
Thrown at compiler/rustc_codegen_llvm/src/builder/autodiff.rs:54
// FIXME(Sa4dUs): pass proper varargs once we have support for differentiating variadic functions
let Ok(fn_abi) = tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((fn_sig, ty::List::empty())))
else {
bug!("failed to get fn_abi of fn_ptr with empty varargs");
};
let mut new_activities = vec![];
let mut new_positions = vec![];
let mut del_activities = 0;
for (i, ty) in sig.inputs().iter().enumerate() {
if let Some(inner_ty) = ty.builtin_deref(true) {
if inner_ty.is_slice() {
// Now we need to figure out the size of each slice element in memory to allow
// safety checks and usability improvements in the backend.
let sty = match inner_ty.builtin_index() {
Some(sty) => sty,
None => {
panic!("slice element type unknown");
}
};
let pci = PseudoCanonicalInput {
typing_env: TypingEnv::fully_monomorphized(),
value: sty,
};
let layout = tcx.layout_of(pci);
let elem_size = match layout {
Ok(layout) => layout.size,
Err(_) => {
bug!("autodiff failed to compute slice element size");
}
};
let elem_size: u32 = elem_size.bytes() as u32;
// We know that the length will be passed as extra arg.
if !da.is_empty() {View on GitHub (pinned to 22057b88b0)
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.
Example fix
// before: differentiating a slice whose element has no builtin index
#[autodiff(df, dup, Active, Duplicated)]
fn grad(xs: &[Box<dyn Fn(f64)->f64>]) -> f64 { /* ... */ } // panic: slice element type unknown
// after: differentiate a slice of a concrete, indexable element type
#[autodiff(df, dup, Active, Duplicated)]
fn grad(xs: &[f64]) -> f64 { /* ... */ } Defensive patterns
Strategy: type-guard
Type guard
trait KnownSliceElem: Sized + Copy {}
impl KnownSliceElem for f32 {}
impl KnownSliceElem for f64 {}
impl KnownSliceElem for f32x4 {}
// Only allow autodiff over slices whose element type has a statically known layout:
fn diff_slice<T: KnownSliceElem>(x: &[T]) { /* safe to pass to autodiff */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- unsized locals must not be `extern` types
- unsupported integer: {self:?}
- unsupported float: {self:?}
- ptr_sized_integer: unknown pointer bit size {bits}
- `Self` generic param is not found while expected
AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03).
Data as JSON: /data/errors/88f6ffe03f3d0585.json.
Report an issue: GitHub.