{"record":{"id":"ec35c701de775a5b","repo":"rust-lang/rust","slug":"alt-layout-should-have-a-niche-like-the-regular-on","errorCode":null,"errorMessage":"alt layout should have a niche like the regular one","messagePattern":"alt layout should have a niche like the regular one","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_abi/src/layout.rs","lineNumber":303,"sourceCode":"            // 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(),\n                            head_space,\n                            niche_len,\n                            tail_space,","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_abi/src/layout.rs#L285-L321","documentation":"Companion invariant to error 241 (layout.rs:303). Once the end-biased alt layout is computed, the code expects it to have a largest_niche via .expect(\"alt layout should have a niche like the regular one\"). The invariant: biasing the niche to the end does not eliminate the niche that the regular layout had. A panic here is a rustc internal bug in the niche-finding logic.","triggerScenarios":"Same multi-field, niche-bearing struct/enum path as error 241, where univariant_biased succeeds but its computed layout has largest_niche == None while the regular layout had one.","commonSituations":"Nightly ICE triggered by niche + repr combinations where end-biasing loses the niche; a layout-algorithm regression.","solutions":["File a rustc ICE report with the minimized type definition and repr attributes.","Remove niched fields and #[repr(...)] to avoid the biased-niche recomputation path.","Bisect with cargo bisect-rustc and compare stable vs nightly.","Reorder fields so the default niche position already satisfies the bias heuristic."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat as a compiler ICE; report it with a minimal repro.","Simplify repr attributes and niche-bearing fields to avoid the biased-layout path.","Bisect nightlies and pin to a working toolchain.","Reorder struct fields so the default niche position already satisfies the heuristic."],"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"}