rust-lang/rust · critical

erroneous constant missed by mono item collection

Error message

erroneous constant missed by mono item collection

What it means

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.

Source

Thrown at compiler/rustc_codegen_cranelift/src/constant.rs:85

        );
        let local_data_id = fx.module.declare_data_in_func(data_id, fx.bcx.func);
        if fx.clif_comments.enabled() {
            fx.add_comment(local_data_id, format!("tls {:?}", def_id));
        }
        fx.bcx.ins().tls_value(fx.pointer_type, local_data_id)
    };
    CValue::by_val(tls_ptr, layout)
}

pub(crate) fn eval_mir_constant<'tcx>(
    fx: &FunctionCx<'_, '_, 'tcx>,
    constant: &ConstOperand<'tcx>,
) -> (ConstValue, Ty<'tcx>) {
    let cv = fx.monomorphize(constant.const_);
    // This cannot fail because we checked all required_consts in advance.
    let val = cv
        .eval(fx.tcx, ty::TypingEnv::fully_monomorphized(), constant.span)
        .expect("erroneous constant missed by mono item collection");
    (val, cv.ty())
}

pub(crate) fn codegen_constant_operand<'tcx>(
    fx: &mut FunctionCx<'_, '_, 'tcx>,
    constant: &ConstOperand<'tcx>,
) -> CValue<'tcx> {
    let (const_val, ty) = eval_mir_constant(fx, constant);
    codegen_const_value(fx, const_val, ty)
}

pub(crate) fn codegen_const_value<'tcx>(
    fx: &mut FunctionCx<'_, '_, 'tcx>,
    const_val: ConstValue,
    ty: Ty<'tcx>,
) -> CValue<'tcx> {
    let layout = fx.layout_of(ty);
    assert!(layout.is_sized(), "unsized const value");

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Clear the incremental compilation cache: `cargo clean` and rebuild.
  2. File an ICE bug report — this is a compiler invariant violation (required_consts check missed an error).
  3. Try `-Zincremental=no` to rule out cache corruption.
  4. Check for preceding compilation errors or warnings that may indicate the problematic constant.
  5. If contributing to rustc, ensure the `required_consts` check in mono item collection uses the same typing environment as codegen's `eval`.

Example fix

# before (stale incremental cache → constant eval mismatch → panic)
cargo build

# after (clean rebuild)
cargo clean && cargo build
# or disable incremental:
RUSTFLAGS='-Zincremental=no' cargo build
Defensive patterns

Strategy: retry

Validate before calling

// This is a compiler invariant; user-level validation is limited.
// Validate that incremental compilation cache is not corrupt:
// In CI: always build clean (cargo clean && cargo build)
// Check for preceding errors: cargo build 2>&1 | grep 'error\['

Try / catch

// In a build script, catch and retry clean on const-eval ICE:
use std::process::Command;
fn build() -> bool {
    Command::new("cargo").arg("build").status().unwrap().success()
}
fn build_clean() -> bool {
    Command::new("cargo").arg("clean").status().unwrap();
    build()
}
// if build() fails, try build_clean()

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/6d1e60e1a4a10d59. Report an issue: GitHub.