rust-lang/rust · error · B::Error

Failed to get layout for

Error message

Failed to get layout for `{ty}`: {err}

What it means

Produced by `LayoutOfHelpers::handle_layout_err` (mod.rs:59), invoked whenever `layout_of` fails for a type. The `LayoutError` variants are `Unknown` (no layout, e.g. opaque/foreign types), `SizeOverflow`, `NormalizationFailure`, and `Cycle`.

Solutions

  1. Check whether the type is sized and has a known kind before requesting layout.
  2. Handle the `Result` from `ty_layout` and skip/log types that fail.
  3. Resolve the root LayoutError variant (e.g. eliminate size-overflow or break the cycle).

Example fix

// before
let layout = ctxt.ty_layout(ty)?;

// after
let layout = ctxt.ty_layout(ty).map_err(|e| {
    log::warn!("no layout for {ty}: {e}");
    e
})?;
Defensive patterns

Strategy: try-catch

Validate before calling

// Reject obviously non-layout-computable types before the query.
if !ty.is_sized(tcx) {
    return Err(format!("{ty} is unsized; no static layout"));
}

Type guard

fn has_static_layout<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, env: TypingEnv<'tcx>) -> bool {
    tcx.layout_of(env.as_query_input(ty)).is_ok()
}

Try / catch

let layout = match ctxt.ty_layout(ty) {
    Ok(l) => l,
    Err(e) => {
        log::warn!("layout error for {ty}: {e}");
        continue;
    }
};

Prevention

When it happens

Trigger: Calling `ty_layout` / `layout_of` on a type whose layout cannot be computed: unsized types without a sized representative, types referencing opaque items, types whose size overflows, or cyclic type definitions.

Common situations: Querying the layout of `impl Trait`/opaque types; recursing into recursively-defined types; cross-crate types whose definitions are not loaded.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/cb2d2e645336fc96. Report an issue: GitHub.

Appendix: source

Thrown at compiler/rustc_public_bridge/src/context/mod.rs:59

        err: ty::layout::FnAbiError<'tcx>,
        _span: rustc_span::Span,
        fn_abi_request: ty::layout::FnAbiRequest<'tcx>,
    ) -> B::Error {
        B::Error::new(format!("Failed to get ABI for `{fn_abi_request:?}`: {err:?}"))
    }
}

impl<'tcx, B: Bridge> LayoutOfHelpers<'tcx> for CompilerCtxt<'tcx, B> {
    type LayoutOfResult = Result<ty::layout::TyAndLayout<'tcx>, B::Error>;

    #[inline]
    fn handle_layout_err(
        &self,
        err: ty::layout::LayoutError<'tcx>,
        _span: rustc_span::Span,
        ty: Ty<'tcx>,
    ) -> B::Error {
        B::Error::new(format!("Failed to get layout for `{ty}`: {err}"))
    }
}

impl<'tcx, B: Bridge> HasTypingEnv<'tcx> for CompilerCtxt<'tcx, B> {
    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
        ty::TypingEnv::fully_monomorphized()
    }
}

impl<'tcx, B: Bridge> HasTyCtxt<'tcx> for CompilerCtxt<'tcx, B> {
    fn tcx(&self) -> TyCtxt<'tcx> {
        self.tcx
    }
}

impl<'tcx, B: Bridge> HasDataLayout for CompilerCtxt<'tcx, B> {
    fn data_layout(&self) -> &rustc_abi::TargetDataLayout {
        self.tcx.data_layout()

View on GitHub (pinned to 7088e4b63a)