rust-lang/rust · critical

aborting after {n} errors due to `-Z treat-err-as-bug={n}`

Error message

aborting after {n} errors due to `-Z treat-err-as-bug={n}`

What it means

Sibling of the single-error case: panic_if_treat_err_as_bug fires when -Z treat-err-as-bug=N (N>1) is set and the recorded count of error-guaranteeing diagnostics reaches N. The panic intentionally aborts compilation to give a backtrace at the Nth error, used by compiler developers to investigate error-emission code paths.

Source

Thrown at compiler/rustc_errors/src/lib.rs:1530

                bug.sub(Note, msg, bug.span.primary_span().unwrap().into());
            }
            bug.level = Bug;

            self.emit_diagnostic(bug, None);
        }

        // Panic with `DelayedBugPanic` to avoid "unexpected panic" messages.
        panic::panic_any(DelayedBugPanic);
    }

    fn panic_if_treat_err_as_bug(&self) {
        if self.treat_err_as_bug() {
            let n = self.flags.treat_err_as_bug.map(|c| c.get()).unwrap();
            assert_eq!(n, self.err_guars.len() + self.lint_err_guars.len());
            if n == 1 {
                panic!("aborting due to `-Z treat-err-as-bug=1`");
            } else {
                panic!("aborting after {n} errors due to `-Z treat-err-as-bug={n}`");
            }
        }
    }
}

struct DelayedDiagInner {
    inner: DiagInner,
    note: Backtrace,
}

impl DelayedDiagInner {
    fn with_backtrace(diagnostic: DiagInner, backtrace: Backtrace) -> Self {
        DelayedDiagInner { inner: diagnostic, note: backtrace }
    }

    fn decorate(self) -> DiagInner {
        // We are at the `DiagInner`/`DiagCtxtInner` level rather than the
        // usual `Diag`/`DiagCtxt` level, so we must construct `diag` in a

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Remove or lower -Z treat-err-as-bug from RUSTFLAGS / config.toml / .cargo/config
  2. If debugging: inspect the backtrace printed with the panic to find the Nth emission site
  3. Verify no wrapper (rustup, bootstrap) is injecting the flag (rustc -vV + verbose cargo)

Example fix

# before
RUSTFLAGS="-Z treat-err-as-bug=5" cargo build
# after
cargo build
Defensive patterns

Strategy: validation

Validate before calling

// Same trigger as 195 but the n-errors variant. Validate build args upfront.
fn deny_treat_err_as_bug(args: &[String]) -> Result<(), String> {
    for a in args {
        if a == "-Ztreat-err-as-bug"
            || a.starts_with("-Ztreat-err-as-bug=")
        {
            return Err(format!(
                "abort-on-N-errors mode enabled by {}; remove for production",
                a
            ));
        }
    }
    Ok(())
}

// deny_treat_err_as_bug(&args)?;

Type guard

struct RustcFlags(Vec<String>);

impl RustcFlags {
    fn abort_mode(&self) -> Option<Option<u32>> {
        for f in &self.0 {
            if f == "-Ztreat-err-as-bug" { return Some(Some(1)); }
            if let Some(rest) = f.strip_prefix("-Ztreat-err-as-bug=") {
                let n = rest.parse::<u32>().ok()?;
                return Some(Some(n));
            }
        }
        None
    }
}

Try / catch

// Process-level boundary: this variant aborts after N errors; detect from status/stderr.
let out = std::process::Command::new("rustc").args(&args).output()?;
let stderr = String::from_utf8_lossy(&out.stderr);
if stderr.contains("-Z treat-err-as-bug=") {
    let n = stderr
        .lines()
        .find_map(|l| l.split("treat-err-as-bug=").nth(1)?.parse::<u32>().ok());
    return Err(format!(
        "rustc aborted after {} errors due to treat-err-as-bug; this is a rustc-dev flag. Remove it.",
        n.unwrap_or(0)
    ));
}

Prevention

When it happens

Trigger: rustc invoked with -Z treat-err-as-bug=N where N>1, and self.flags.treat_err_as_bug.unwrap().get() equals err_guars.len()+lint_err_guars.len() at the panic site. The interpolated {n} in the message is that threshold.

Common situations: Compiler contributors setting treat-err-as-bug to a higher threshold to skip early, expected errors and break on a later one; leftover -Z flag in CI RUSTFLAGS; bootstrap profile unintentionally enabling debug flags.

Related errors


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