From 52f060abe77eb9afba125390d575e50c717b9fd5 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 17 Nov 2025 11:34:28 +0100 Subject: [PATCH] emit stash diagnostics before flushing delayed errors --- src/bin/miri.rs | 19 +++++++++++++------ .../issue-miri-4698-stashed-diagnostic.rs | 10 ++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 tests/pass/issues/issue-miri-4698-stashed-diagnostic.rs diff --git a/src/bin/miri.rs b/src/bin/miri.rs index 9ce2aab865..7e80e52041 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -158,19 +158,24 @@ impl rustc_driver::Callbacks for MiriCompilerCalls { _: &rustc_interface::interface::Compiler, tcx: TyCtxt<'tcx>, ) -> Compilation { + // Compilation is done, interpretation is starting. Deal with diagnostics from the + // compilation part. We cannot call `sess.finish_diagnostics()` as then "aborting due to + // previous errors" gets printed twice. + tcx.dcx().emit_stashed_diagnostics(); tcx.dcx().abort_if_errors(); tcx.dcx().flush_delayed(); + // Miri is taking over. Start logging. + init_late_loggers(&EarlyDiagCtxt::new(tcx.sess.opts.error_format), tcx); + + // Find the entry point. if !tcx.crate_types().contains(&CrateType::Executable) { tcx.dcx().fatal("miri only makes sense on bin crates"); } - - let early_dcx = EarlyDiagCtxt::new(tcx.sess.opts.error_format); - init_late_loggers(&early_dcx, tcx); - let (entry_def_id, entry_type) = entry_fn(tcx); - let mut config = self.miri_config.take().expect("after_analysis must only be called once"); + // Obtain and complete the Miri configuration. + let mut config = self.miri_config.take().expect("after_analysis must only be called once"); // Add filename to `miri` arguments. config.args.insert(0, tcx.sess.io.input.filestem().to_string()); @@ -179,6 +184,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls { env::set_current_dir(cwd).unwrap(); } + // Emit warnings for some unusual configurations. if tcx.sess.opts.optimize != OptLevel::No { tcx.dcx().warn("Miri does not support optimizations: the opt-level is ignored. The only effect \ of selecting a Cargo profile that enables optimizations (such as --release) is to apply \ @@ -193,6 +199,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls { optimizations is usually marginal at best."); } + // Invoke the interpreter. let res = if config.genmc_config.is_some() { assert!(self.many_seeds.is_none()); run_genmc_mode(tcx, &config, |genmc_ctx: Rc| { @@ -209,7 +216,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls { } else { miri::eval_entry(tcx, entry_def_id, entry_type, &config, None) }; - + // Process interpreter result. if let Err(return_code) = res { tcx.dcx().abort_if_errors(); exit(return_code.get()); diff --git a/tests/pass/issues/issue-miri-4698-stashed-diagnostic.rs b/tests/pass/issues/issue-miri-4698-stashed-diagnostic.rs new file mode 100644 index 0000000000..3467d2d273 --- /dev/null +++ b/tests/pass/issues/issue-miri-4698-stashed-diagnostic.rs @@ -0,0 +1,10 @@ +// This test seems to involve a "stashed diagnostic" (or at least it used to at the time of +// writing). Ensure we handle that correctly. + +pub trait Trait { + type Assoc: Assoc; +} + +pub trait Assoc {} + +fn main() {}