rust-lang/rust · error · B::Error
Failed to get ABI for `{fn_abi_request:?}`: {err:?}
Error message
Failed to get ABI for `{fn_abi_request:?}`: {err:?} What it means
Emitted by CompilerCtxt::handle_fn_abi_err (compiler/rustc_public_bridge/src/context/mod.rs:45), the FnAbiOfHelpers impl for CompilerCtxt. It wraps the ty::layout::FnAbiError returned by rustc's fn_abi_of_instance / fn_abi_of_fn_ptr queries into a Bridge::Error so that consumers of the stable rustc_public API receive a recoverable Result::Err instead of an internal compiler panic. Note that FnAbiError currently has a single variant, Layout(LayoutError), so every ABI failure is ultimately rooted in a layout failure of some type participating in the function signature.
Source
Thrown at compiler/rustc_public_bridge/src/context/mod.rs:45
impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
Self { tcx, _marker: Default::default() }
}
}
/// Implement error handling for extracting function ABI information.
impl<'tcx, B: Bridge> FnAbiOfHelpers<'tcx> for CompilerCtxt<'tcx, B> {
type FnAbiOfResult = Result<&'tcx rustc_target::callconv::FnAbi<'tcx, Ty<'tcx>>, B::Error>;
#[inline]
fn handle_fn_abi_err(
&self,
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> {View on GitHub (pinned to 22057b88b0)
Solutions
- Before calling instance_abi/fn_ptr_abi, verify the instance is fully monomorphized: gate on !instance.has_non_region_param() (see the assert already in instance_ty at impls.rs:615).
- Inspect the inner LayoutError variant to act specifically: Unknown/TooGeneric -> skip the item; SizeOverflow/InvalidSimd -> reject the input as malformed; NormalizationFailure -> the crate needs clean type-checking first; ReferencesError -> a prior compile error must be fixed.
- Skip ABI requests for InstanceKinds that cannot have a meaningful ABI (Virtual, Intrinsic that must_be_overridden) by checking instance_has_body / item_has_body first.
- Pin the exact nightly toolchain whose rustc_public_bridge matches your rustc_public consumer crate version; the API is explicitly unstable and signature shape changes between versions.
- Ensure the crate under analysis type-checks with zero errors before driving ABI/layout queries; run cargo check first and stop on errors.
Example fix
// before
let abi = ctxt.instance_abi(instance)?;
// after: only request ABI for fully monomorphized, body-having instances
if !instance.has_non_region_param() && ctxt.has_body(instance.def_id()) {
let abi = ctxt.instance_abi(instance)?;
// ...use abi
} else {
// skip generic or shim/intrinsic instances
} Defensive patterns
Strategy: try-catch
Validate before calling
// Before instance_abi(): reject Instances that still carry generic params or error types.
// The bridge queries under TypingEnv::fully_monomorphized(), so any leftover param fails normalization.
if instance.has_non_region_param() {
return Err(MyError::NotMonomorphized(format!("{instance:?} still has generic parameters")));
}
if instance
.ty(tcx, rustc_middle::ty::TypingEnv::fully_monomorphized())
.references_error()
{
return Err(MyError::TypeError(format!("{instance:?} contains Ty::Error")));
}
// For fn_ptr_abi(): a PolyFnSig must have no escaping bound variables.
if sig.escape_depth() != 0 {
return Err(MyError::BoundVars(format!("{sig:?} has escaping bound vars")));
} Type guard
/// True iff `inst` is safe to pass to `CompilerCtxt::instance_abi` (fully monomorphized, no error types).
fn instance_abi_safe<'tcx>(tcx: rustc_middle::ty::TyCtxt<'tcx>, inst: rustc_middle::ty::Instance<'tcx>) -> bool {
!inst.has_non_region_param()
&& !inst
.ty(tcx, rustc_middle::ty::TypingEnv::fully_monomorphized())
.references_error()
} Try / catch
// instance_abi() / fn_ptr_abi() return Result<&FnAbi, B::Error>; this is an expected,
// type-system-dependent runtime condition, not a bug. Propagate or degrade, never .unwrap().
match ctxt.instance_abi(instance) {
Ok(fn_abi) => { /* use fn_abi */ }
Err(e) => {
// ABI not computable for this fn: log with the failing FnAbiRequest and skip it.
log::warn!("skipping {instance:?}: {e}");
}
} Prevention
- Monomorphize every Instance before querying its ABI: the bridge runs under TypingEnv::fully_monomorphized(), so any leftover generic parameter (gate on instance.has_non_region_param()) fails normalization and surfaces error 350.
- Never call fn_ptr_abi on a PolyFnSig with escaping bound variables; substitute/erase them first (sig.escape_depth() == 0).
- Filter out functions whose signature references Ty::Error (ty.references_error()) early in your analysis pipeline — their ABI cannot be computed.
- Treat ABI computation as advisory, not mandatory: wrap every instance_abi / fn_ptr_abi call in Result handling and degrade by skipping the function rather than aborting the tool.
- Cache successful FnAbi results by InstanceDef so a single malformed function does not force re-computation across passes.
When it happens
Trigger: Calling CompilerCtxt::instance_abi(instance) (impls.rs:625) or fn_ptr_abi(sig) (impls.rs:633) when one of the types in the signature yields a LayoutError; an Instance that is not fully monomorphized (still has type parameters) passed to fn_abi_of_instance under TypingEnv::fully_monomorphized(); a function whose signature references an opaque/impl Trait type that cannot normalize after type revelation; SIMD or unsized tail types that violate layout rules; shim/intrinsic instances whose ABI legitimately cannot be computed.
Common situations: Static analyzers, codegen tools, ABI extractors, and rustdoc-json consumers built on the unstable rustc_public crate iterating over all DefIds and requesting ABIs indiscriminately; analyzing a crate that did not itself type-check cleanly (rustc cannot produce layouts for error-tainted types); version skew between the rustc_public consumer pinned by a tool and the rustc_public_bridge shipped in the nightly toolchain; processing FFI items with exotic calling conventions or vtable shims (Virtual/Intrinsic InstanceKind).
Related errors
- Failed to get layout for `{ty}`: {err}
- aggregates can't have `FieldsShape::Primitive`
- layout decided on a larger discriminant type ({min_ity:?}) t
- encountered a non-arbitrary layout during enum layout
- unsupported integer: {self:?}
AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03).
Data as JSON: /data/errors/39009b5c16e1c3e3.json.
Report an issue: GitHub.