{"record":{"id":"49288306c628a8ab","repo":"rust-lang/rust","slug":"alt-layout-should-always-work","errorCode":null,"errorMessage":"alt layout should always work","messagePattern":"alt layout should always work","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_abi/src/layout.rs","lineNumber":300,"sourceCode":"        // run and bias niches to the right and then check which one is closer to one of the\n        // struct's edges.\n        if let Ok(layout) = &layout {\n            // Don't try to calculate an end-biased layout for unsizable structs,\n            // otherwise we could end up with different layouts for\n            // Foo<Type> and Foo<dyn Trait> which would break unsizing.\n            if !matches!(kind, StructKind::MaybeUnsized) {\n                if let Some(niche) = layout.largest_niche {\n                    let head_space = niche.offset.bytes();\n                    let niche_len = niche.value.size(dl).bytes();\n                    let tail_space = layout.size.bytes() - head_space - niche_len;\n\n                    // This may end up doing redundant work if the niche is already in the last\n                    // field (e.g. a trailing bool) and there is tail padding. But it's non-trivial\n                    // to get the unpadded size so we try anyway.\n                    if fields.len() > 1 && head_space != 0 && tail_space > 0 {\n                        let alt_layout = self\n                            .univariant_biased(fields, repr, kind, NicheBias::End)\n                            .expect(\"alt layout should always work\");\n                        let alt_niche = alt_layout\n                            .largest_niche\n                            .expect(\"alt layout should have a niche like the regular one\");\n                        let alt_head_space = alt_niche.offset.bytes();\n                        let alt_niche_len = alt_niche.value.size(dl).bytes();\n                        let alt_tail_space =\n                            alt_layout.size.bytes() - alt_head_space - alt_niche_len;\n\n                        debug_assert_eq!(layout.size.bytes(), alt_layout.size.bytes());\n\n                        let prefer_alt_layout =\n                            alt_head_space > head_space && alt_head_space > tail_space;\n\n                        debug!(\n                            \"sz: {}, default_niche_at: {}+{}, default_tail_space: {}, alt_niche_at/head_space: {}+{}, alt_tail: {}, num_fields: {}, better: {}\\n\\\n                            layout: {}\\n\\\n                            alt_layout: {}\\n\",\n                            layout.size.bytes(),","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_abi/src/layout.rs#L282-L318","documentation":"Internal layout invariant in rustc_abi's niche calculator (layout.rs:300). After a regular univariant layout with a niche is computed successfully, the compiler recomputes an end-biased (NicheBias::End) layout and expects it to also succeed via .expect(\"alt layout should always work\"). The assumption: if the default field ordering yields a valid layout, the end-biased ordering must too. A panic here is a compiler bug (ICE), not a user error.","triggerScenarios":"Lowering a multi-field struct/enum that has a niche (e.g. an Option/NonZero/bool field), head_space != 0, and tail_space > 0, so the biased-recomputation branch runs and univariant_biased returns Err.","commonSituations":"An ICE on nightly with unusual #[repr(...)] combinations or niche-bearing fields. This signals a bug in the layout algorithm, typically hit only by compiler developers or with exotic reprs.","solutions":["Treat this as an ICE: file a rustc issue with the minimized repro (the panic message is the anchor).","Reduce the offending type: strip niched fields (Option, NonZero*, bool) and remove #[repr(...)] attributes until the panic disappears.","Bisect with `cargo bisect-rustc` to find the regression, and try the same code on stable vs nightly.","As a workaround, reorder fields or change the repr so the biased-niche recomputation path is not taken."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["This is a rustc ICE, not a recoverable error; do not try to catch it in user code.","Minimize the offending type (remove niched fields and #[repr(...)]) and file a rustc issue.","Use cargo bisect-rustc to find the regression and compare stable vs nightly.","Pin to a known-good toolchain until the bug is fixed."],"tags":["rust","compiler-internal","layout","ice"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}