{"record":{"id":"6d1e60e1a4a10d59","repo":"rust-lang/rust","slug":"erroneous-constant-missed-by-mono-item-collection","errorCode":null,"errorMessage":"erroneous constant missed by mono item collection","messagePattern":"erroneous constant missed by mono item collection","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_codegen_cranelift/src/constant.rs","lineNumber":85,"sourceCode":"        );\n        let local_data_id = fx.module.declare_data_in_func(data_id, fx.bcx.func);\n        if fx.clif_comments.enabled() {\n            fx.add_comment(local_data_id, format!(\"tls {:?}\", def_id));\n        }\n        fx.bcx.ins().tls_value(fx.pointer_type, local_data_id)\n    };\n    CValue::by_val(tls_ptr, layout)\n}\n\npub(crate) fn eval_mir_constant<'tcx>(\n    fx: &FunctionCx<'_, '_, 'tcx>,\n    constant: &ConstOperand<'tcx>,\n) -> (ConstValue, Ty<'tcx>) {\n    let cv = fx.monomorphize(constant.const_);\n    // This cannot fail because we checked all required_consts in advance.\n    let val = cv\n        .eval(fx.tcx, ty::TypingEnv::fully_monomorphized(), constant.span)\n        .expect(\"erroneous constant missed by mono item collection\");\n    (val, cv.ty())\n}\n\npub(crate) fn codegen_constant_operand<'tcx>(\n    fx: &mut FunctionCx<'_, '_, 'tcx>,\n    constant: &ConstOperand<'tcx>,\n) -> CValue<'tcx> {\n    let (const_val, ty) = eval_mir_constant(fx, constant);\n    codegen_const_value(fx, const_val, ty)\n}\n\npub(crate) fn codegen_const_value<'tcx>(\n    fx: &mut FunctionCx<'_, '_, 'tcx>,\n    const_val: ConstValue,\n    ty: Ty<'tcx>,\n) -> CValue<'tcx> {\n    let layout = fx.layout_of(ty);\n    assert!(layout.is_sized(), \"unsized const value\");","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_codegen_cranelift/src/constant.rs#L67-L103","documentation":"This fires in Cranelift codegen's `eval_mir_constant`. The code evaluates a constant via `cv.eval(...)` and the comment states 'This cannot fail because we checked all required_consts in advance.' The `.expect(...)` panics if the constant evaluation fails (returns `Err`) despite having supposedly been validated during mono item collection's `required_consts` check. This means a constant that should have been caught as erroneous earlier reached codegen.","triggerScenarios":"Codegen evaluates a constant that was marked as required/checked during mono item collection but is actually erroneous when re-evaluated. The mismatch between the mono-item-collection check and the codegen evaluation causes the panic — e.g., the const was ok during collection but the typing environment differs at codegen time, or a dependency changed.","commonSituations":"Complex const evaluation involving `const fn`, associated consts, or cross-crate const dependencies where the evaluation context differs between MIR generation and codegen. Can also happen after incremental compilation cache corruption, or when a const's evaluation depends on feature flags that changed between compilation stages.","solutions":["Clear the incremental compilation cache: `cargo clean` and rebuild.","File an ICE bug report — this is a compiler invariant violation (required_consts check missed an error).","Try `-Zincremental=no` to rule out cache corruption.","Check for preceding compilation errors or warnings that may indicate the problematic constant.","If contributing to rustc, ensure the `required_consts` check in mono item collection uses the same typing environment as codegen's `eval`."],"exampleFix":"# before (stale incremental cache → constant eval mismatch → panic)\ncargo build\n\n# after (clean rebuild)\ncargo clean && cargo build\n# or disable incremental:\nRUSTFLAGS='-Zincremental=no' cargo build","handlingStrategy":"retry","validationCode":"// This is a compiler invariant; user-level validation is limited.\n// Validate that incremental compilation cache is not corrupt:\n// In CI: always build clean (cargo clean && cargo build)\n// Check for preceding errors: cargo build 2>&1 | grep 'error\\['","typeGuard":null,"tryCatchPattern":"// In a build script, catch and retry clean on const-eval ICE:\nuse std::process::Command;\nfn build() -> bool {\n    Command::new(\"cargo\").arg(\"build\").status().unwrap().success()\n}\nfn build_clean() -> bool {\n    Command::new(\"cargo\").arg(\"clean\").status().unwrap();\n    build()\n}\n// if build() fails, try build_clean()","preventionTips":["Run cargo clean when encountering const-eval ICEs.","Disable incremental compilation in CI: CARGO_INCREMENTAL=0.","Fix all preceding compilation errors and warnings before investigating ICEs.","Pin stable toolchain versions for production builds.","Report required_consts check mismatches as compiler bugs."],"tags":["rustc","cranelift","codegen","ice","const-eval","incremental","mono-item-collection","compiler-internal"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}