From 5a59808973fd8c059b88b9e4b5f648f2d950b7eb Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Tue, 20 Feb 2024 21:06:11 -0500 Subject: [PATCH] Don't let non-primary spans take priority over primary spans. --- src/rustc_stderr.rs | 8 +++++++- tests/integrations/basic/Cargo.stdout | 3 ++- .../basic/tests/actual_tests/mac_span.fixed | 11 +++++++++++ .../basic/tests/actual_tests/mac_span.rs | 11 +++++++++++ .../basic/tests/actual_tests/mac_span.stderr | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/integrations/basic/tests/actual_tests/mac_span.fixed create mode 100644 tests/integrations/basic/tests/actual_tests/mac_span.rs create mode 100644 tests/integrations/basic/tests/actual_tests/mac_span.stderr diff --git a/src/rustc_stderr.rs b/src/rustc_stderr.rs index 291208e6..f7218a1b 100644 --- a/src/rustc_stderr.rs +++ b/src/rustc_stderr.rs @@ -144,8 +144,14 @@ impl RustcSpan { /// Returns the most expanded line number *in the given file*, if possible. fn line(&self, file: &Path, primary: bool) -> Option { if let Some(exp) = &self.expansion { - if let Some(line) = exp.span.line(file, primary && !self.is_primary) { + if let Some(line) = exp.span.line(file, !primary || self.is_primary) { return Some(line); + } else if self.file_name != file { + return if !primary && self.is_primary { + exp.span.line(file, false) + } else { + None + }; } } ((!primary || self.is_primary) && self.file_name == file).then_some(spanned::Span { diff --git a/tests/integrations/basic/Cargo.stdout b/tests/integrations/basic/Cargo.stdout index 1dff073d..6cf6132e 100644 --- a/tests/integrations/basic/Cargo.stdout +++ b/tests/integrations/basic/Cargo.stdout @@ -20,6 +20,7 @@ tests/actual_tests/foomp.rs ... ok tests/actual_tests/joined_above.rs ... ok tests/actual_tests/joined_below.rs ... ok tests/actual_tests/joined_mixed.rs ... ok +tests/actual_tests/mac_span.rs ... ok tests/actual_tests/match_diagnostic_code.rs ... ok tests/actual_tests/no_rustfix.rs ... ok tests/actual_tests/stdin.rs ... ok @@ -27,7 +28,7 @@ tests/actual_tests/unicode.rs ... ok tests/actual_tests/windows_paths.rs ... ok tests/actual_tests/subdir/aux_proc_macro.rs ... ok -test result: ok. 15 passed; +test result: ok. 16 passed; running 0 tests diff --git a/tests/integrations/basic/tests/actual_tests/mac_span.fixed b/tests/integrations/basic/tests/actual_tests/mac_span.fixed new file mode 100644 index 00000000..dc521512 --- /dev/null +++ b/tests/integrations/basic/tests/actual_tests/mac_span.fixed @@ -0,0 +1,11 @@ +#![deny(unused_variables)] + +macro_rules! m { + () => {{ + let _x = 0; //~ unused_variables + }}; +} + +fn main() { + m!(); +} diff --git a/tests/integrations/basic/tests/actual_tests/mac_span.rs b/tests/integrations/basic/tests/actual_tests/mac_span.rs new file mode 100644 index 00000000..5ca20df6 --- /dev/null +++ b/tests/integrations/basic/tests/actual_tests/mac_span.rs @@ -0,0 +1,11 @@ +#![deny(unused_variables)] + +macro_rules! m { + () => {{ + let x = 0; //~ unused_variables + }}; +} + +fn main() { + m!(); +} diff --git a/tests/integrations/basic/tests/actual_tests/mac_span.stderr b/tests/integrations/basic/tests/actual_tests/mac_span.stderr new file mode 100644 index 00000000..02c8ebef --- /dev/null +++ b/tests/integrations/basic/tests/actual_tests/mac_span.stderr @@ -0,0 +1,18 @@ +error: unused variable: `x` + --> tests/actual_tests/mac_span.rs:5:13 + | +5 | let x = 0; + | ^ help: if this is intentional, prefix it with an underscore: `_x` +... +10 | m!(); + | ---- in this macro invocation + | +note: the lint level is defined here + --> tests/actual_tests/mac_span.rs:1:9 + | +1 | #![deny(unused_variables)] + | ^^^^^^^^^^^^^^^^ + = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error +