{"record":{"id":"cb2d2e645336fc96","repo":"rust-lang/rust","slug":"failed-to-get-layout-for-ty-err","errorCode":null,"errorMessage":"Failed to get layout for `{ty}`: {err}","messagePattern":"Failed to get layout for `(.+?)`: (.+?)","errorType":"exception","errorClass":"B::Error","httpStatus":null,"severity":"error","filePath":"compiler/rustc_public_bridge/src/context/mod.rs","lineNumber":59,"sourceCode":"        err: ty::layout::FnAbiError<'tcx>,\n        _span: rustc_span::Span,\n        fn_abi_request: ty::layout::FnAbiRequest<'tcx>,\n    ) -> B::Error {\n        B::Error::new(format!(\"Failed to get ABI for `{fn_abi_request:?}`: {err:?}\"))\n    }\n}\n\nimpl<'tcx, B: Bridge> LayoutOfHelpers<'tcx> for CompilerCtxt<'tcx, B> {\n    type LayoutOfResult = Result<ty::layout::TyAndLayout<'tcx>, B::Error>;\n\n    #[inline]\n    fn handle_layout_err(\n        &self,\n        err: ty::layout::LayoutError<'tcx>,\n        _span: rustc_span::Span,\n        ty: Ty<'tcx>,\n    ) -> B::Error {\n        B::Error::new(format!(\"Failed to get layout for `{ty}`: {err}\"))\n    }\n}\n\nimpl<'tcx, B: Bridge> HasTypingEnv<'tcx> for CompilerCtxt<'tcx, B> {\n    fn typing_env(&self) -> ty::TypingEnv<'tcx> {\n        ty::TypingEnv::fully_monomorphized()\n    }\n}\n\nimpl<'tcx, B: Bridge> HasTyCtxt<'tcx> for CompilerCtxt<'tcx, B> {\n    fn tcx(&self) -> TyCtxt<'tcx> {\n        self.tcx\n    }\n}\n\nimpl<'tcx, B: Bridge> HasDataLayout for CompilerCtxt<'tcx, B> {\n    fn data_layout(&self) -> &rustc_abi::TargetDataLayout {\n        self.tcx.data_layout()","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_public_bridge/src/context/mod.rs#L41-L77","documentation":"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`.","triggerScenarios":"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.","commonSituations":"Querying the layout of `impl Trait`/opaque types; recursing into recursively-defined types; cross-crate types whose definitions are not loaded.","solutions":["Check whether the type is sized and has a known kind before requesting layout.","Handle the `Result` from `ty_layout` and skip/log types that fail.","Resolve the root LayoutError variant (e.g. eliminate size-overflow or break the cycle)."],"exampleFix":"// before\nlet layout = ctxt.ty_layout(ty)?;\n\n// after\nlet layout = ctxt.ty_layout(ty).map_err(|e| {\n    log::warn!(\"no layout for {ty}: {e}\");\n    e\n})?;","handlingStrategy":"try-catch","validationCode":"// Reject obviously non-layout-computable types before the query.\nif !ty.is_sized(tcx) {\n    return Err(format!(\"{ty} is unsized; no static layout\"));\n}","typeGuard":"fn has_static_layout<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, env: TypingEnv<'tcx>) -> bool {\n    tcx.layout_of(env.as_query_input(ty)).is_ok()\n}","tryCatchPattern":"let layout = match ctxt.ty_layout(ty) {\n    Ok(l) => l,\n    Err(e) => {\n        log::warn!(\"layout error for {ty}: {e}\");\n        continue;\n    }\n};","preventionTips":["Confirm a type is sized before requesting its layout.","Treat Unknown/SizeOverflow/Cycle variants distinctly when diagnosing."],"tags":["rustc-public-bridge","layout","type-check"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}