diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index b7c273e626378..2496200ab298a 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -935,12 +935,13 @@ fn link_natively<'a>( let mut output = prog.stderr.clone(); output.extend_from_slice(&prog.stdout); let escaped_output = escape_linker_output(&output, flavor); - // FIXME: Add UI tests for this error. + debug!("{}", sess.opts.verbose); let err = errors::LinkingFailed { linker_path: &linker_path, exit_status: prog.status, command: &cmd, escaped_output, + verbose: sess.opts.verbose, }; sess.dcx().emit_err(err); // If MSVC's `link.exe` was expected but the return code diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 668d39afbda74..4064ec270d724 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -408,6 +408,7 @@ pub struct LinkingFailed<'a> { pub exit_status: ExitStatus, pub command: &'a Command, pub escaped_output: String, + pub verbose: bool, } impl IntoDiagnostic<'_> for LinkingFailed<'_> { @@ -418,7 +419,13 @@ impl IntoDiagnostic<'_> for LinkingFailed<'_> { let contains_undefined_ref = self.escaped_output.contains("undefined reference to"); - diag.note(format!("{:?}", self.command)).note(self.escaped_output); + if self.verbose { + diag.note(format!("{:?}", self.command)); + } else { + diag.note("use `--verbose` to show all linker arguments"); + } + + diag.note(self.escaped_output); // Trying to match an error from OS linkers // which by now we have no way to translate. diff --git a/tests/run-make/linker-warning/Makefile b/tests/run-make/linker-warning/Makefile index 8b9e98f4ec54e..ecb36aee21440 100644 --- a/tests/run-make/linker-warning/Makefile +++ b/tests/run-make/linker-warning/Makefile @@ -2,7 +2,9 @@ include ../tools.mk RUN_RUSTC := $(RUSTC_ORIGINAL) main.rs -o $(TMPDIR)/main -C linker=./fake-linker.sh -all: +all: succeeds_with_warnings errors linker_args + +succeeds_with_warnings: # Run rustc with our fake linker, and make sure it shows warnings $(RUN_RUSTC) -C link-arg=run_make_warn 2>&1 | $(CGREP) "warning: linker stderr: bar" @@ -10,8 +12,14 @@ all: $(RUN_RUSTC) -C link-arg=run_make_info --verbose 2>&1 | $(CGREP) "warning: linker stdout: foo" $(RUN_RUSTC) -C link-arg=run_make_info 2>&1 | $(CGREP) -v "warning: linker stdout: foo" +errors: # Make sure we short-circuit this new path if the linker exits with an error (so the diagnostic is less verbose) rm -f $(TMPDIR)/main $(RUN_RUSTC) -C link-arg=run_make_error 2>&1 | $(CGREP) "note: error: baz" ! [ -e $(TMPDIR)/main ] +linker_args: + # Make sure we don't show the linker args unless `--verbose` is passed + $(RUN_RUSTC) --verbose -C link-arg=run_make_error 2>&1 | $(CGREP) -e "PATH=.*fake-linker.sh.*run_make_error" + $(RUN_RUSTC) -C link-arg=run_make_error 2>&1 | $(CGREP) -v -e "PATH=.*fake-linker.sh.*run_make_error" +