rust-lang/rust · error

required_consts for {:?} have not yet been set

Error message

required_consts for {:?} have not yet been set

What it means

`Body::required_consts()` (mir/mod.rs:730) panics if the `required_consts` field is still `None`, i.e. the MIR pass that populates evaluated-constant operands (`set_required_consts`) has not yet run for this `Body`. `Body` uses an `Option<Vec<ConstOperand>>` to distinguish "unset" from "empty", so the getter is only valid after the setter has been invoked in the MIR pipeline.

Source

Thrown at compiler/rustc_middle/src/mir/mod.rs:730

        // No inlined `SourceScope`s, or all of them were `#[track_caller]`.
        caller_location.unwrap_or_else(|| from_span(source_info.span))
    }

    #[track_caller]
    pub fn set_required_consts(&mut self, required_consts: Vec<ConstOperand<'tcx>>) {
        assert!(
            self.required_consts.is_none(),
            "required_consts for {:?} have already been set",
            self.source.def_id()
        );
        self.required_consts = Some(required_consts);
    }
    #[track_caller]
    pub fn required_consts(&self) -> &[ConstOperand<'tcx>] {
        match &self.required_consts {
            Some(l) => l,
            None => panic!("required_consts for {:?} have not yet been set", self.source.def_id()),
        }
    }

    #[track_caller]
    pub fn set_mentioned_items(&mut self, mentioned_items: Vec<Spanned<MentionedItem<'tcx>>>) {
        assert!(
            self.mentioned_items.is_none(),
            "mentioned_items for {:?} have already been set",
            self.source.def_id()
        );
        self.mentioned_items = Some(mentioned_items);
    }
    #[track_caller]
    pub fn mentioned_items(&self) -> &[Spanned<MentionedItem<'tcx>>] {
        match &self.mentioned_items {
            Some(l) => l,
            None => panic!("mentioned_items for {:?} have not yet been set", self.source.def_id()),
        }

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Ensure the populating pass runs before your query: in the pipeline registration, add the appropriate run-before/run-after constraint.
  2. If you build the `Body` yourself (tests, tools), call `body.set_required_consts(vec![...])` before reading.
  3. Guard the read with `body.required_consts.opt()` if an `Option` accessor exists, or check `is_some()` first if you add one.
  4. Check the MIR `Phase` (e.g. `Body::phase`) — `required_consts` is typically only set past a specific phase boundary.

Example fix

// before
let consts = body.required_consts(); // None → panic

// after
body.set_required_consts(collected);
let consts = body.required_consts();
Defensive patterns

Strategy: validation

Validate before calling

// Body::required_consts() panics if set_required_consts has not run yet.
// There is no public is_set accessor, so track construction state yourself.
struct BodyView<'a, 'tcx> {
    body: &'a Body<'tcx>,
    consts_set: bool,
}

fn required_consts<'a, 'tcx>(v: &BodyView<'a, 'tcx>) -> &'a [ConstOperand<'tcx>] {
    if v.consts_set {
        v.body.required_consts()
    } else {
        &[] // or return an Err
    }
}

Try / catch

let consts = std::panic::catch_unwind(|| body.required_consts());
match consts {
    Ok(c) => /* use c */,
    Err(_) => /* the MIR pass that sets consts has not run; run it or skip */,
}

Prevention

When it happens

Trigger: Calling `body.required_consts()` on a freshly constructed `Body` before the `required_consts` MIR pass (in `rustc_mir_transform`) has run; querying `required_consts` from a pass that executes earlier in the pipeline ordering than the populating pass; or holding a `Body` built by a test harness that skipped the populate step.

Common situations: Adding a new MIR pass and reading `required_consts` without declaring a `before`/`after` constraint on the populating pass; refactoring the MIR pass pipeline order; mir-json/mir-output tools that build a `Body` directly and forget to populate fields; downstream tools (Miri, cargo-call-stack) consuming partially-built MIR.

Related errors


AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03). Data as JSON: /data/errors/34039daa5ab2cbb2.json. Report an issue: GitHub.