diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 94639bf8e1ee4..2e678cd54fd87 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -725,6 +725,10 @@ fn print_crate_info( | TargetFeatures => { codegen_backend.print(*req, sess); } + RustcPath => match env::current_exe() { + Ok(exe) => println!("{}", exe.display()), + Err(_) => early_error(ErrorOutputType::default(), "failed to get rustc path"), + }, // Any output here interferes with Cargo's parsing of other printed output NativeStaticLibs => {} LinkArgs => {} diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 68519c8fa828e..3ff6c368e1ef4 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -545,6 +545,7 @@ pub enum PrintRequest { NativeStaticLibs, StackProtectorStrategies, LinkArgs, + RustcPath, } pub enum Input { @@ -1777,6 +1778,20 @@ fn collect_print_requests( cg.target_feature = String::new(); } + let gate = |req, opt| { + if unstable_opts.unstable_options { + req + } else { + early_error( + error_format, + &format!( + "the `-Z unstable-options` flag must also be passed to \ + enable the {opt} print option", + ), + ); + } + }; + prints.extend(matches.opt_strs("print").into_iter().map(|s| match &*s { "crate-name" => PrintRequest::CrateName, "file-names" => PrintRequest::FileNames, @@ -1791,18 +1806,9 @@ fn collect_print_requests( "tls-models" => PrintRequest::TlsModels, "native-static-libs" => PrintRequest::NativeStaticLibs, "stack-protector-strategies" => PrintRequest::StackProtectorStrategies, - "target-spec-json" => { - if unstable_opts.unstable_options { - PrintRequest::TargetSpec - } else { - early_error( - error_format, - "the `-Z unstable-options` flag must also be passed to \ - enable the target-spec-json print option", - ); - } - } + "target-spec-json" => gate(PrintRequest::TargetSpec, "target-spec-json"), "link-args" => PrintRequest::LinkArgs, + "rustc-path" => gate(PrintRequest::RustcPath, "rustc-path"), req => early_error(error_format, &format!("unknown print request `{req}`")), })); diff --git a/src/test/run-make/print-rustc-path/Makefile b/src/test/run-make/print-rustc-path/Makefile new file mode 100644 index 0000000000000..b008578082962 --- /dev/null +++ b/src/test/run-make/print-rustc-path/Makefile @@ -0,0 +1,9 @@ +-include ../../run-make-fulldeps/tools.mk + +ifdef IS_WINDOWS +all: + $(RUSTC) -Zunstable-options --print rustc-path | $(CGREP) bin\rustc +else +all: + $(RUSTC) -Zunstable-options --print rustc-path | $(CGREP) bin/rustc +endif