Skip to content

Commit

Permalink
don't show the full linker args unless --verbose is passed
Browse files Browse the repository at this point in the history
the linker arguments can be *very* long, especially for crates with many dependencies. often they are not useful. omit them unless the user specifically requests them.
  • Loading branch information
jyn514 committed Dec 25, 2023
1 parent 5fa554e commit 64cb47a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_codegen_ssa/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<'_> {
Expand All @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion tests/run-make/linker-warning/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ 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"

# Make sure it shows stdout, but only when --verbose is passed
$(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"

0 comments on commit 64cb47a

Please sign in to comment.