rust-lang/rust · critical

missing polonius context with `-Zpolonius=next`

Error message

missing polonius context with `-Zpolonius=next`

What it means

This panic occurs in the Polonius MIR dump path (`-Zdump-mir=polonius`). After confirming that `-Zpolonius=next` is enabled, the code unwraps the `Option<&PoloniusContext>` via `.expect(...)`. The PoloniusContext should always be constructed when `-Zpolonius=next` is active. If it is `None` — meaning the borrow checker did not build a polonius context for this body despite the flag being on — the compiler panics.

Source

Thrown at compiler/rustc_borrowck/src/polonius/dump.rs:35

/// `-Zdump-mir=polonius` dumps MIR annotated with NLL and polonius specific information.
pub(crate) fn dump_polonius_mir<'tcx>(
    infcx: &BorrowckInferCtxt<'tcx>,
    body: &Body<'tcx>,
    regioncx: &RegionInferenceContext<'tcx>,
    closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
    borrow_set: &BorrowSet<'tcx>,
    polonius_context: Option<&PoloniusContext>,
) {
    let tcx = infcx.tcx;
    if !tcx.sess.opts.unstable_opts.polonius.is_next_enabled() {
        return;
    }

    let Some(dumper) = MirDumper::new(tcx, "polonius", body) else { return };

    let polonius_context =
        polonius_context.expect("missing polonius context with `-Zpolonius=next`");

    // If we have a polonius graph to dump along the rest of the MIR and NLL info, we extract its
    // constraints here.
    let mut collector = LocalizedOutlivesConstraintCollector { constraints: Vec::new() };
    if let Some(graph) = &polonius_context.graph {
        graph.traverse(
            body,
            regioncx.liveness_constraints(),
            &polonius_context.live_region_variances,
            regioncx.universal_regions(),
            borrow_set,
            &mut collector,
        );
    }

    let extra_data = &|pass_where, out: &mut dyn io::Write| {
        emit_polonius_mir(
            tcx,

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Ensure both flags are consistently set: `-Zpolonius=next` must be the active analysis mode for the dump to have data.
  2. Remove `-Zdump-mir=polonius` if you don't need the polonius-specific MIR annotation; the panic only fires in the dump path.
  3. Update to the latest nightly — the Polonius integration is under active development and flag wiring changes frequently.
  4. File a bug if the flags are consistent and you still hit it; the dump function at `dump.rs:34-35` should guard against `None` more gracefully.

Example fix

# before (may trigger ICE)
RUSTFLAGS='-Zpolonius=next -Zdump-mir=polonius' cargo build

# after (use a single consistent flag set; drop the dump if not needed)
RUSTFLAGS='-Zpolonius=next' cargo build
Defensive patterns

Strategy: validation

Validate before calling

// Validate flag consistency before compiling
// In a build script or CI config:
// Ensure -Zpolonius=next is set if -Zdump-mir=polonius is used
// Shell check:
// if echo "$RUSTFLAGS" | grep -q 'dump-mir=polonius'; then
//   echo "$RUSTFLAGS" | grep -q 'polonius=next' || \
//     export RUSTFLAGS="$RUSTFLAGS -Zpolonius=next"
// fi

Try / catch

// In a custom rustc driver, validate polonius context before dumping:
if tcx.sess.opts.unstable_opts.polonius.is_next_enabled() {
    if let Some(_ctx) = polonius_context {
        dump_polonius_mir(/* ... */);
    } else {
        tcx.sess.warn("polonius=next enabled but no context; skipping dump");
    }
}

Prevention

When it happens

Trigger: Compiling with `-Zpolonius=next` combined with `-Zdump-mir=polonius` on a MIR body where the polonius context was not constructed (e.g., a body that was skipped by polonius, a body where polonius was disabled mid-pipeline, or a configuration mismatch between the dump request and the analysis that ran).

Common situations: Nightly Rust developers experimenting with the next-generation Polonius borrow checker. Happens when the polonius pipeline is partially configured — for example, using `-Zpolonius=next` on a crate that has bodies excluded from polonius analysis, or when a tooling change decoupled the dump from the analysis pass.

Related errors


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