Skip to content

Commit 092a83d

Browse files
authored
Merge pull request #4731 from bjorn3/simplify_driver
Call rustc_driver::main() for MIRI_BE_RUSTC=host
2 parents b64f34c + e6100ef commit 092a83d

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

src/bin/miri.rs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,13 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
272272
}
273273
}
274274

275-
struct MiriBeRustCompilerCalls {
276-
target_crate: bool,
277-
}
275+
/// This compiler produces rlibs that are meant for later consumption by Miri.
276+
struct MiriDepCompilerCalls;
278277

279-
impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
278+
impl rustc_driver::Callbacks for MiriDepCompilerCalls {
280279
#[allow(rustc::potential_query_instability)] // rustc_codegen_ssa (where this code is copied from) also allows this lint
281280
fn config(&mut self, config: &mut Config) {
282-
if !self.target_crate {
283-
// For a host crate, we fully behave like rustc.
284-
return;
285-
}
286-
// For a target crate, we emit an rlib that Miri can later consume.
281+
// We don't need actual codegen, we just emit an rlib that Miri can later consume.
287282
config.make_codegen_backend = Some(Box::new(make_miri_codegen_backend));
288283

289284
// Avoid warnings about unsupported crate types. However, only do that we we are *not* being
@@ -367,16 +362,12 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
367362
_: &rustc_interface::interface::Compiler,
368363
tcx: TyCtxt<'tcx>,
369364
) -> Compilation {
370-
if self.target_crate {
371-
// cargo-miri has patched the compiler flags to make these into check-only builds,
372-
// but we are still emulating regular rustc builds, which would perform post-mono
373-
// const-eval during collection. So let's also do that here, even if we might be
374-
// running with `--emit=metadata`. In particular this is needed to make
375-
// `compile_fail` doc tests trigger post-mono errors.
376-
// In general `collect_and_partition_mono_items` is not safe to call in check-only
377-
// builds, but we are setting `-Zalways-encode-mir` which avoids those issues.
378-
let _ = tcx.collect_and_partition_mono_items(());
379-
}
365+
// While the dummy codegen backend doesn't do any codegen, we are still emulating
366+
// regular rustc builds, which would perform post-mono const-eval during collection.
367+
// So let's also do that here. In particular this is needed to make `compile_fail`
368+
// doc tests trigger post-mono errors.
369+
let _ = tcx.collect_and_partition_mono_items(());
370+
380371
Compilation::Continue
381372
}
382373
}
@@ -457,32 +448,28 @@ fn main() {
457448

458449
// If the environment asks us to actually be rustc, then do that.
459450
if let Some(crate_kind) = env::var_os("MIRI_BE_RUSTC") {
451+
if crate_kind == "host" {
452+
// For host crates like proc macros and build scripts, we are an entirely normal rustc.
453+
// These eventually produce actual binaries and never run in Miri.
454+
match rustc_driver::main() {
455+
// Empty match proves this function will never return.
456+
}
457+
} else if crate_kind != "target" {
458+
panic!("invalid `MIRI_BE_RUSTC` value: {crate_kind:?}")
459+
};
460+
460461
// Earliest rustc setup.
461462
rustc_driver::install_ice_hook(rustc_driver::DEFAULT_BUG_REPORT_URL, |_| ());
462463
rustc_driver::init_rustc_env_logger(&early_dcx);
463464

464-
let target_crate = if crate_kind == "target" {
465-
true
466-
} else if crate_kind == "host" {
467-
false
468-
} else {
469-
panic!("invalid `MIRI_BE_RUSTC` value: {crate_kind:?}")
470-
};
471-
472465
let mut args = args;
473-
// Don't insert `MIRI_DEFAULT_ARGS`, in particular, `--cfg=miri`, if we are building
474-
// a "host" crate. That may cause procedural macros (and probably build scripts) to
475-
// depend on Miri-only symbols, such as `miri_resolve_frame`:
476-
// https://github.com/rust-lang/miri/issues/1760
477-
if target_crate {
478-
// Splice in the default arguments after the program name.
479-
// Some options have different defaults in Miri than in plain rustc; apply those by making
480-
// them the first arguments after the binary name (but later arguments can overwrite them).
481-
args.splice(1..1, miri::MIRI_DEFAULT_ARGS.iter().map(ToString::to_string));
482-
}
466+
// Splice in the default arguments after the program name.
467+
// Some options have different defaults in Miri than in plain rustc; apply those by making
468+
// them the first arguments after the binary name (but later arguments can overwrite them).
469+
args.splice(1..1, miri::MIRI_DEFAULT_ARGS.iter().map(ToString::to_string));
483470

484471
// We cannot use `rustc_driver::main` as we want it to use `args` as the CLI arguments.
485-
run_compiler_and_exit(&args, &mut MiriBeRustCompilerCalls { target_crate })
472+
run_compiler_and_exit(&args, &mut MiriDepCompilerCalls)
486473
}
487474

488475
// Add an ICE bug report hook.

0 commit comments

Comments
 (0)