@@ -244,6 +244,16 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
244244 /// Cargo does not give us this information directly, so we need to check
245245 /// various command-line flags.
246246 fn is_runnable_crate ( ) -> bool {
247+ // Determine whether this is cargo invoking rustc to get some infos. Ideally we'd check "is
248+ // there a filename passed to rustc", but that's very hard as we would have to know whether
249+ // e.g. `--print foo` is a booolean flag `--print` followed by filename `foo` or equivalent
250+ // to `--print=foo`. So instead we use this more fragile approach of detecting the presence
251+ // of a "query" flag rather than the absence of a filename.
252+ let info_query = get_arg_flag_value ( "--print" ) . is_some ( ) || has_arg_flag ( "-vV" ) ;
253+ if info_query {
254+ // Nothing to run.
255+ return false ;
256+ }
247257 let is_bin = get_arg_flag_value ( "--crate-type" ) . as_deref ( ) . unwrap_or ( "bin" ) == "bin" ;
248258 let is_test = has_arg_flag ( "--test" ) ;
249259 is_bin || is_test
@@ -290,8 +300,6 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
290300 let verbose = std:: env:: var ( "MIRI_VERBOSE" )
291301 . map_or ( 0 , |verbose| verbose. parse ( ) . expect ( "verbosity flag must be an integer" ) ) ;
292302 let target_crate = is_target_crate ( ) ;
293- // Determine whether this is cargo invoking rustc to get some infos.
294- let info_query = get_arg_flag_value ( "--print" ) . is_some ( ) || has_arg_flag ( "-vV" ) ;
295303
296304 let store_json = |info : CrateRunInfo | {
297305 if get_arg_flag_value ( "--emit" ) . unwrap_or_default ( ) . split ( ',' ) . any ( |e| e == "dep-info" ) {
@@ -318,7 +326,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
318326 }
319327 } ;
320328
321- let runnable_crate = !info_query && is_runnable_crate ( ) ;
329+ let runnable_crate = is_runnable_crate ( ) ;
322330
323331 if runnable_crate && target_crate {
324332 assert ! (
@@ -392,7 +400,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
392400 let mut emit_link_hack = false ;
393401 // Arguments are treated very differently depending on whether this crate is
394402 // for interpretation by Miri, or for use by a build script / proc macro.
395- if !info_query && target_crate {
403+ if target_crate {
396404 // Set the sysroot.
397405 cmd. arg ( "--sysroot" ) . arg ( env:: var_os ( "MIRI_SYSROOT" ) . unwrap ( ) ) ;
398406 // Forward arguments, but remove "link" from "--emit" to make this a check-only build.
@@ -428,17 +436,14 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
428436 cmd. arg ( "-C" ) . arg ( "panic=abort" ) ;
429437 }
430438 } else {
431- // For host crates (but not when we are just printing some info),
432- // we might still have to set the sysroot.
433- if !info_query {
434- // When we're running `cargo-miri` from `x.py` we need to pass the sysroot explicitly
435- // due to bootstrap complications.
436- if let Some ( sysroot) = std:: env:: var_os ( "MIRI_HOST_SYSROOT" ) {
437- cmd. arg ( "--sysroot" ) . arg ( sysroot) ;
438- }
439+ // This is a host crate.
440+ // When we're running `cargo-miri` from `x.py` we need to pass the sysroot explicitly
441+ // due to bootstrap complications.
442+ if let Some ( sysroot) = std:: env:: var_os ( "MIRI_HOST_SYSROOT" ) {
443+ cmd. arg ( "--sysroot" ) . arg ( sysroot) ;
439444 }
440445
441- // For host crates or when we are printing, just forward everything.
446+ // Forward everything.
442447 cmd. args ( args) ;
443448 }
444449
@@ -450,9 +455,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
450455
451456 // Run it.
452457 if verbose > 0 {
453- eprintln ! (
454- "[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} info_query={info_query}"
455- ) ;
458+ eprintln ! ( "[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate}" ) ;
456459 }
457460
458461 // Create a stub .rlib file if "link" was requested by cargo.
@@ -547,15 +550,13 @@ pub fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: Runner
547550 // but when we run here, cargo does not interpret the JSON any more. `--json`
548551 // then also needs to be dropped.
549552 let mut args = info. args . into_iter ( ) ;
550- let error_format_flag = "--error-format" ;
551- let json_flag = "--json" ;
552553 while let Some ( arg) = args. next ( ) {
553554 if arg == "--extern" {
554555 forward_patched_extern_arg ( & mut args, & mut cmd) ;
555- } else if let Some ( suffix) = arg. strip_prefix ( error_format_flag ) {
556+ } else if let Some ( suffix) = arg. strip_prefix ( "--error-format" ) {
556557 assert ! ( suffix. starts_with( '=' ) ) ;
557558 // Drop this argument.
558- } else if let Some ( suffix) = arg. strip_prefix ( json_flag ) {
559+ } else if let Some ( suffix) = arg. strip_prefix ( "--json" ) {
559560 assert ! ( suffix. starts_with( '=' ) ) ;
560561 // Drop this argument.
561562 } else {
@@ -593,13 +594,11 @@ pub fn phase_rustdoc(mut args: impl Iterator<Item = String>) {
593594 // just default to a straight-forward invocation for now:
594595 let mut cmd = Command :: new ( "rustdoc" ) ;
595596
596- let extern_flag = "--extern" ;
597- let runtool_flag = "--runtool" ;
598597 while let Some ( arg) = args. next ( ) {
599- if arg == extern_flag {
598+ if arg == "--extern" {
600599 // Patch --extern arguments to use *.rmeta files, since phase_cargo_rustc only creates stub *.rlib files.
601600 forward_patched_extern_arg ( & mut args, & mut cmd) ;
602- } else if arg == runtool_flag {
601+ } else if arg == "--runtool" {
603602 // An existing --runtool flag indicates cargo is running in cross-target mode, which we don't support.
604603 // Note that this is only passed when cargo is run with the unstable -Zdoctest-xcompile flag;
605604 // otherwise, we won't be called as rustdoc at all.
0 commit comments