{"id":"34039daa5ab2cbb2","repo":"rust-lang/rust","slug":"required-consts-for-have-not-yet-been-set","errorCode":null,"errorMessage":"required_consts for {:?} have not yet been set","messagePattern":"required_consts for (.+?) have not yet been set","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_middle/src/mir/mod.rs","lineNumber":730,"sourceCode":"\n        // No inlined `SourceScope`s, or all of them were `#[track_caller]`.\n        caller_location.unwrap_or_else(|| from_span(source_info.span))\n    }\n\n    #[track_caller]\n    pub fn set_required_consts(&mut self, required_consts: Vec<ConstOperand<'tcx>>) {\n        assert!(\n            self.required_consts.is_none(),\n            \"required_consts for {:?} have already been set\",\n            self.source.def_id()\n        );\n        self.required_consts = Some(required_consts);\n    }\n    #[track_caller]\n    pub fn required_consts(&self) -> &[ConstOperand<'tcx>] {\n        match &self.required_consts {\n            Some(l) => l,\n            None => panic!(\"required_consts for {:?} have not yet been set\", self.source.def_id()),\n        }\n    }\n\n    #[track_caller]\n    pub fn set_mentioned_items(&mut self, mentioned_items: Vec<Spanned<MentionedItem<'tcx>>>) {\n        assert!(\n            self.mentioned_items.is_none(),\n            \"mentioned_items for {:?} have already been set\",\n            self.source.def_id()\n        );\n        self.mentioned_items = Some(mentioned_items);\n    }\n    #[track_caller]\n    pub fn mentioned_items(&self) -> &[Spanned<MentionedItem<'tcx>>] {\n        match &self.mentioned_items {\n            Some(l) => l,\n            None => panic!(\"mentioned_items for {:?} have not yet been set\", self.source.def_id()),\n        }","sourceCodeStart":712,"sourceCodeEnd":748,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_middle/src/mir/mod.rs#L712-L748","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the populating pass runs before your query: in the pipeline registration, add the appropriate run-before/run-after constraint.","If you build the `Body` yourself (tests, tools), call `body.set_required_consts(vec![...])` before reading.","Guard the read with `body.required_consts.opt()` if an `Option` accessor exists, or check `is_some()` first if you add one.","Check the MIR `Phase` (e.g. `Body::phase`) — `required_consts` is typically only set past a specific phase boundary."],"exampleFix":"// before\nlet consts = body.required_consts(); // None → panic\n\n// after\nbody.set_required_consts(collected);\nlet consts = body.required_consts();","handlingStrategy":"validation","validationCode":"// Body::required_consts() panics if set_required_consts has not run yet.\n// There is no public is_set accessor, so track construction state yourself.\nstruct BodyView<'a, 'tcx> {\n    body: &'a Body<'tcx>,\n    consts_set: bool,\n}\n\nfn required_consts<'a, 'tcx>(v: &BodyView<'a, 'tcx>) -> &'a [ConstOperand<'tcx>] {\n    if v.consts_set {\n        v.body.required_consts()\n    } else {\n        &[] // or return an Err\n    }\n}","typeGuard":null,"tryCatchPattern":"let consts = std::panic::catch_unwind(|| body.required_consts());\nmatch consts {\n    Ok(c) => /* use c */,\n    Err(_) => /* the MIR pass that sets consts has not run; run it or skip */,\n}","preventionTips":["Drive MIR construction through a single builder that calls set_required_consts exactly once before anyone reads it; record the 'set' flag in your own wrapper.","Before reading required_consts, confirm the body has passed the constant-collection MIR pass (built/mir_for_ctfe depending on context).","Never assume a freshly-constructed Body has required_consts populated; the field starts as None.","If you cache Body instances, invalidate the cache when the query that sets consts is re-run."],"tags":["rustc","mir","mir-body","query","pipeline","internal"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}