rust-lang/rust · error

mentioned_items for {:?} have not yet been set

Error message

mentioned_items for {:?} have not yet been set

What it means

`Body::mentioned_items()` (mir/mod.rs:747) panics if the `mentionedItems` field is still `None` — the MIR pass that collects "mentioned items" (a more inclusive set than `required_consts`, used by the coverage/inlineability checks) has not yet run for this `Body`. Same Option-vs-empty invariant as index 248.

Source

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

            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()),
        }
    }
}

impl<'tcx> Index<BasicBlock> for Body<'tcx> {
    type Output = BasicBlockData<'tcx>;

    #[inline]
    fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
        &self.basic_blocks[index]
    }
}

impl<'tcx> IndexMut<BasicBlock> for Body<'tcx> {
    #[inline]
    fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
        &mut self.basic_blocks.as_mut()[index]
    }

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Declare the pipeline ordering so the populating pass runs before your consumer pass.
  2. If constructing the `Body` directly, call `body.set_mentioned_items(vec![...])` before any read.
  3. Gate the read on the MIR phase (`Body::phase`) that guarantees population.
  4. Use an Option-style accessor or check `mentioned_items.is_some()` if you add one locally.

Example fix

// before
let items = body.mentioned_items(); // None → panic

// after
body.set_mentioned_items(collected);
let items = body.mentioned_items();
Defensive patterns

Strategy: validation

Validate before calling

// Body::mentioned_items() panics until set_mentioned_items has run.
// Mirror the guard used for required_consts.
struct BodyView<'a, 'tcx> {
    body: &'a Body<'tcx>,
    mentioned_set: bool,
}

fn mentioned_items<'a, 'tcx>(v: &BodyView<'a, 'tcx>) -> &'a [Spanned<MentionedItem<'tcx>>] {
    if v.mentioned_set {
        v.body.mentioned_items()
    else {
        &[]
    }
}

Try / catch

let items = std::panic::catch_unwind(|| body.mentioned_items());
match items {
    Ok(i) => /* use i */,
    Err(_) => /* mentioned-item collection pass not run yet; defer this consumer */,
}

Prevention

When it happens

Trigger: Calling `body.mentioned_items()` before the mentioned-items MIR pass invoked `set_mentioned_items`. Happens when a downstream pass, tool, or test queries the field on a `Body` whose pipeline phase predates the populate step, or when a `Body` is constructed by an external tool (mir-json, diff-testing harness) without populating the field.

Common situations: Adding a pass that consumes mentioned items for coverage/inline-cost analysis without declaring ordering against the populating pass; rustc upgrades where mentioned-items was introduced (or its populate phase moved); Miri/tools reading MIR bodies produced by a stripped rustc build.

Related errors


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