From cedd4cad21298ca8b825ab3c56a84753176dc8e8 Mon Sep 17 00:00:00 2001 From: jyn Date: Fri, 29 Nov 2024 10:28:54 -0500 Subject: ignore linker errors on all platforms --- src/bootstrap/src/core/build_steps/test.rs | 2 ++ src/bootstrap/src/core/builder/cargo.rs | 7 +++++++ 2 files changed, 9 insertions(+) (limited to 'src') diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 9f3e4d9cc89..38e056cbc14 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -3577,6 +3577,8 @@ impl Step for CodegenGCC { let mut cargo = build_cargo(); cargo + // cg_gcc's build system ignores RUSTFLAGS. pass some flags through CG_RUSTFLAGS instead. + .env("CG_RUSTFLAGS", "-Alinker-messages") .arg("--") .arg("test") .arg("--use-system-gcc") diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs index f9fb19ddb09..031cbb7dcdb 100644 --- a/src/bootstrap/src/core/builder/cargo.rs +++ b/src/bootstrap/src/core/builder/cargo.rs @@ -269,6 +269,13 @@ impl Cargo { self.rustflags.arg("-Clink-arg=-gz"); } + // Ignore linker warnings for now. These are complicated to fix and don't affect the build. + // FIXME: we should really investigate these... + // cfg(bootstrap) + if compiler.stage != 0 { + self.rustflags.arg("-Alinker-messages"); + } + // Throughout the build Cargo can execute a number of build scripts // compiling C/C++ code and we need to pass compilers, archivers, flags, etc // obtained previously to those build scripts. -- cgit 1.4.1-3-g733a5 From b757663a00260e22799a1bbdc5d6ba603e4bb30d Mon Sep 17 00:00:00 2001 From: jyn Date: Sat, 21 Dec 2024 10:06:07 -0500 Subject: don't ICE when emitting linker errors during `-Z link-only` note that this still ICEs when passed `-Z link-only --error-format json` because i can't be bothered to fix it right now --- compiler/rustc_errors/src/json.rs | 47 +++++++++++++++++++++------------ compiler/rustc_errors/src/json/tests.rs | 2 +- compiler/rustc_session/src/session.rs | 9 ++++--- src/librustdoc/core.rs | 2 +- tests/run-make/linker-warning/rmake.rs | 29 ++++++++++++++++++++ 5 files changed, 67 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index c1188665a05..95c81fc5f44 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -21,7 +21,7 @@ use rustc_error_messages::FluentArgs; use rustc_lint_defs::Applicability; use rustc_span::Span; use rustc_span::hygiene::ExpnData; -use rustc_span::source_map::SourceMap; +use rustc_span::source_map::{FilePathMapping, SourceMap}; use serde::Serialize; use termcolor::{ColorSpec, WriteColor}; @@ -45,7 +45,7 @@ pub struct JsonEmitter { #[setters(skip)] dst: IntoDynSyncSend>, #[setters(skip)] - sm: Lrc, + sm: Option>, fluent_bundle: Option>, #[setters(skip)] fallback_bundle: LazyFallbackBundle, @@ -65,7 +65,7 @@ pub struct JsonEmitter { impl JsonEmitter { pub fn new( dst: Box, - sm: Lrc, + sm: Option>, fallback_bundle: LazyFallbackBundle, pretty: bool, json_rendered: HumanReadableErrorType, @@ -171,7 +171,7 @@ impl Emitter for JsonEmitter { } fn source_map(&self) -> Option<&SourceMap> { - Some(&self.sm) + self.sm.as_deref() } fn should_show_explain(&self) -> bool { @@ -371,7 +371,7 @@ impl Diagnostic { } HumanEmitter::new(dst, Lrc::clone(&je.fallback_bundle)) .short_message(short) - .sm(Some(Lrc::clone(&je.sm))) + .sm(je.sm.clone()) .fluent_bundle(je.fluent_bundle.clone()) .diagnostic_width(je.diagnostic_width) .macro_backtrace(je.macro_backtrace) @@ -458,23 +458,34 @@ impl DiagnosticSpan { mut backtrace: impl Iterator, je: &JsonEmitter, ) -> DiagnosticSpan { - let start = je.sm.lookup_char_pos(span.lo()); + let empty_source_map; + let sm = match &je.sm { + Some(s) => s, + None => { + span = rustc_span::DUMMY_SP; + empty_source_map = Arc::new(SourceMap::new(FilePathMapping::empty())); + empty_source_map + .new_source_file(std::path::PathBuf::from("empty.rs").into(), String::new()); + &empty_source_map + } + }; + let start = sm.lookup_char_pos(span.lo()); // If this goes from the start of a line to the end and the replacement // is an empty string, increase the length to include the newline so we don't // leave an empty line if start.col.0 == 0 && let Some((suggestion, _)) = suggestion && suggestion.is_empty() - && let Ok(after) = je.sm.span_to_next_source(span) + && let Ok(after) = sm.span_to_next_source(span) && after.starts_with('\n') { span = span.with_hi(span.hi() + rustc_span::BytePos(1)); } - let end = je.sm.lookup_char_pos(span.hi()); + let end = sm.lookup_char_pos(span.hi()); let backtrace_step = backtrace.next().map(|bt| { let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je); let def_site_span = Self::from_span_full( - je.sm.guess_head_span(bt.def_site), + sm.guess_head_span(bt.def_site), false, None, None, @@ -489,7 +500,7 @@ impl DiagnosticSpan { }); DiagnosticSpan { - file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(), + file_name: sm.filename_for_diagnostics(&start.file.name).to_string(), byte_start: start.file.original_relative_byte_pos(span.lo()).0, byte_end: start.file.original_relative_byte_pos(span.hi()).0, line_start: start.line, @@ -559,19 +570,20 @@ impl DiagnosticSpanLine { /// `span` within the line. fn from_span(span: Span, je: &JsonEmitter) -> Vec { je.sm - .span_to_lines(span) - .map(|lines| { + .as_ref() + .and_then(|sm| { + let lines = sm.span_to_lines(span).ok()?; // We can't get any lines if the source is unavailable. if !should_show_source_code( &je.ignored_directories_in_source_blocks, - &je.sm, + &sm, &lines.file, ) { - return vec![]; + return None; } let sf = &*lines.file; - lines + let span_lines = lines .lines .iter() .map(|line| { @@ -582,8 +594,9 @@ impl DiagnosticSpanLine { line.end_col.0 + 1, ) }) - .collect() + .collect(); + Some(span_lines) }) - .unwrap_or_else(|_| vec![]) + .unwrap_or_default() } } diff --git a/compiler/rustc_errors/src/json/tests.rs b/compiler/rustc_errors/src/json/tests.rs index 0de555b83d3..cebaf7c1cfe 100644 --- a/compiler/rustc_errors/src/json/tests.rs +++ b/compiler/rustc_errors/src/json/tests.rs @@ -47,7 +47,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) { let output = Arc::new(Mutex::new(Vec::new())); let je = JsonEmitter::new( Box::new(Shared { data: output.clone() }), - sm, + Some(sm), fallback_bundle, true, // pretty HumanReadableErrorType::Short, diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 60f1154dc6d..022c2aa880e 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -895,13 +895,16 @@ fn default_emitter( } t => t, }; + + let source_map = if sopts.unstable_opts.link_only { None } else { Some(source_map) }; + match sopts.error_format { config::ErrorOutputType::HumanReadable(kind, color_config) => { let short = kind.short(); if let HumanReadableErrorType::AnnotateSnippet = kind { let emitter = AnnotateSnippetEmitter::new( - Some(source_map), + source_map, bundle, fallback_bundle, short, @@ -911,7 +914,7 @@ fn default_emitter( } else { let emitter = HumanEmitter::new(stderr_destination(color_config), fallback_bundle) .fluent_bundle(bundle) - .sm(Some(source_map)) + .sm(source_map) .short_message(short) .teach(sopts.unstable_opts.teach) .diagnostic_width(sopts.diagnostic_width) @@ -1442,7 +1445,7 @@ fn mk_emitter(output: ErrorOutputType) -> Box { config::ErrorOutputType::Json { pretty, json_rendered, color_config } => { Box::new(JsonEmitter::new( Box::new(io::BufWriter::new(io::stderr())), - Lrc::new(SourceMap::new(FilePathMapping::empty())), + Some(Lrc::new(SourceMap::new(FilePathMapping::empty()))), fallback_bundle, pretty, json_rendered, diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 0dda3466a71..78fee20aee3 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -178,7 +178,7 @@ pub(crate) fn new_dcx( Box::new( JsonEmitter::new( Box::new(io::BufWriter::new(io::stderr())), - source_map, + Some(source_map), fallback_bundle, pretty, json_rendered, diff --git a/tests/run-make/linker-warning/rmake.rs b/tests/run-make/linker-warning/rmake.rs index d2bb12aafcb..5967f5b32e9 100644 --- a/tests/run-make/linker-warning/rmake.rs +++ b/tests/run-make/linker-warning/rmake.rs @@ -44,4 +44,33 @@ fn main() { .assert_stderr_contains("object files omitted") .assert_stderr_contains_regex(r"\{") .assert_stderr_not_contains_regex(r"lib(/|\\\\)libstd"); + + // Make sure we show linker warnings even across `-Z no-link` + rustc() + .arg("-Zno-link") + .input("-") + .stdin_buf("#![deny(linker_messages)] \n fn main() {}") + .run() + .assert_stderr_equals(""); + rustc() + .arg("-Zlink-only") + .arg("rust_out.rlink") + .linker("./fake-linker") + .link_arg("run_make_warn") + .run_fail() + // NOTE: the error message here is quite bad (we don't have a source + // span, but still try to print the lint source). But `-Z link-only` is + // unstable and this still shows the linker warning itself so this is + // probably good enough. + .assert_stderr_contains("linker stderr: bar"); + + // Same thing, but with json output. + rustc() + .error_format("json") + .arg("-Zlink-only") + .arg("rust_out.rlink") + .linker("./fake-linker") + .link_arg("run_make_warn") + .run_fail() + .assert_stderr_contains(r#""$message_type":"diagnostic""#); } -- cgit 1.4.1-3-g733a5 From 7407c1268334a01142d1770dd5080ffeaa8eaa1a Mon Sep 17 00:00:00 2001 From: jyn Date: Mon, 20 Jan 2025 16:44:24 -0500 Subject: Ignore linker warnings on macOS for ui-fulldeps ld is showing things like this: ``` ld: ignoring duplicate libraries: '-lm' ``` I don't have time or a macbook that lets me investigate these. Just silence them for now. --- src/bootstrap/src/core/build_steps/test.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 38e056cbc14..95fbf76b79b 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1862,12 +1862,19 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the let mut hostflags = flags.clone(); hostflags.push(format!("-Lnative={}", builder.test_helpers_out(compiler.host).display())); hostflags.extend(linker_flags(builder, compiler.host, LldThreads::No)); - for flag in hostflags { - cmd.arg("--host-rustcflags").arg(flag); - } let mut targetflags = flags; targetflags.push(format!("-Lnative={}", builder.test_helpers_out(target).display())); + + // FIXME: on macOS, we get linker warnings about duplicate `-lm` flags. We should investigate why this happens. + if suite == "ui-fulldeps" && target.ends_with("darwin") { + hostflags.push("-Alinker_messages".into()); + targetflags.push("-Alinker_messages".into()); + } + + for flag in hostflags { + cmd.arg("--host-rustcflags").arg(flag); + } for flag in targetflags { cmd.arg("--target-rustcflags").arg(flag); } -- cgit 1.4.1-3-g733a5