{"record":{"id":"13934de44fb4b40c","repo":"rust-lang/rust","slug":"expected-monomorphic-const-in-codegen","errorCode":null,"errorMessage":"expected monomorphic const in codegen","messagePattern":"expected monomorphic const in codegen","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_codegen_cranelift/src/base.rs","lineNumber":845,"sourceCode":"                Rvalue::Cast(\n                    CastKind::Transmute | CastKind::BoxDerefTransmute | CastKind::Subtype,\n                    ref operand,\n                    _to_ty,\n                ) => {\n                    let operand = codegen_operand(fx, operand);\n                    lval.write_cvalue_transmute(fx, operand);\n                }\n                Rvalue::Discriminant(place) => {\n                    let place = codegen_place(fx, place);\n                    let value = place.to_cvalue(fx);\n                    crate::discriminant::codegen_get_discriminant(fx, lval, value, dest_layout);\n                }\n                Rvalue::Repeat(ref operand, times) => {\n                    let operand = codegen_operand(fx, operand);\n                    let times = fx\n                        .monomorphize(times)\n                        .try_to_target_usize(fx.tcx)\n                        .expect(\"expected monomorphic const in codegen\");\n                    if operand.layout().size.bytes() == 0 {\n                        // Do nothing for ZST's\n                    } else if fx.clif_type(operand.layout().ty) == Some(types::I8) {\n                        let times = fx.bcx.ins().iconst(fx.pointer_type, times as i64);\n                        // FIXME use emit_small_memset where possible\n                        let addr = lval.to_ptr().get_addr(fx);\n                        let val = operand.load_scalar(fx);\n                        fx.bcx.call_memset(fx.target_config, addr, val, times);\n                    } else {\n                        let loop_block = fx.bcx.create_block();\n                        let loop_block2 = fx.bcx.create_block();\n                        let done_block = fx.bcx.create_block();\n                        let index = fx.bcx.append_block_param(loop_block, fx.pointer_type);\n                        let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);\n                        fx.bcx.ins().jump(loop_block, &[zero.into()]);\n\n                        fx.bcx.switch_to_block(loop_block);\n                        let done = fx.bcx.ins().icmp_imm_u(IntCC::Equal, index, times as i64);","sourceCodeStart":827,"sourceCodeEnd":863,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_codegen_cranelift/src/base.rs#L827-L863","documentation":"This fires in Cranelift codegen when processing `Rvalue::Repeat` (e.g., `[value; N]`). The repeat count `N` is a const that should be monomorphized to a concrete `usize` by codegen time. The code calls `fx.monomorphize(times).try_to_target_usize(fx.tcx).expect(...)`. If after monomorphization the const is still generic, unevaluated, or not a concrete target usize, the conversion fails and panics.","triggerScenarios":"Codegen encounters a `[val; COUNT]` expression where `COUNT` is a generic const parameter or a const that monomorphization failed to evaluate to a concrete `usize`. In a correctly functioning compiler, all generics are instantiated before codegen, so this const should always be concrete by this point.","commonSituations":"Using generic const expressions in array repeat syntax (e.g., `[0; N]` where `N` is a generic parameter) in combinations that confuse monomorphization or const-eval. Can also happen with unstable `generic_const_exprs` feature where const evaluation is not yet fully wired through to codegen. Most often a compiler bug rather than user error.","solutions":["File an ICE bug report — monomorphization should guarantee a concrete const by codegen.","Avoid generic const expressions in array repeat counts; use a runtime `vec![val; count]` or a concrete array size if possible.","Try `-Zmir-opt-level=0` or the LLVM backend to narrow the issue.","If using `generic_const_exprs`, update to the latest nightly — the feature pipeline changes frequently.","If contributing to rustc, verify that mono item collection properly instantiates the const before passing the body to codegen."],"exampleFix":"// before (generic const in array repeat, may reach codegen unevaluated)\nfn f<const N: usize>() -> [u32; N] {\n    [0; N]\n}\n\n// after (use Vec for runtime-size, or concrete size)\nfn f<const N: usize>() -> Vec<u32> {\n    vec![0; N]\n}","handlingStrategy":"fallback","validationCode":"// Avoid generic const expressions in array repeat counts\n// Type-check at the source level:\n// Prefer: const N: usize = 10; [0; N]\n// Over:   fn f<const N: usize>() -> [u32; N] { [0; N] }","typeGuard":"// Ensure array repeat counts are concrete, not generic\nfn assert_concrete_array_repeat<T, const N: usize>() {\n    // N is generic; [0; N] may fail in codegen on some nightly versions\n    // Use Vec instead for generic sizes\n}\n// Prefer:\nfn make_array<const N: usize>() -> Vec<u32> { vec![0; N] }","tryCatchPattern":null,"preventionTips":["Avoid generic const exprs in [val; N] until the feature stabilizes.","Use vec![val; N] for runtime-sized repetition.","Pin nightly versions for generic_const_exprs testing.","Report monomorphization pipeline bugs with minimal reproductions."],"tags":["rustc","cranelift","codegen","ice","monomorphization","const-eval","mir","compiler-internal"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}