From 4da6eb2ec6141eb7b4172d62e48c277ea8a191fb Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 7 Sep 2023 05:11:41 +0000 Subject: [PATCH 1/2] Test showing it doesnt work --- ...suggest-import-without-clobbering-attrs.fixed | 16 ++++++++++++++++ .../suggest-import-without-clobbering-attrs.rs | 15 +++++++++++++++ ...uggest-import-without-clobbering-attrs.stderr | 14 ++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed create mode 100644 tests/ui/resolve/suggest-import-without-clobbering-attrs.rs create mode 100644 tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed b/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed new file mode 100644 index 0000000000000..881b973e3ef1e --- /dev/null +++ b/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed @@ -0,0 +1,16 @@ +// run-rustfix +// compile-flags: --cfg=whatever -Aunused + +#[cfg(whatever)] +use y::z; +use y::Whatever; + +mod y { + pub(crate) fn z() {} + pub(crate) struct Whatever; +} + +fn main() { + z(); + //~^ ERROR cannot find function `z` in this scope +} diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs b/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs new file mode 100644 index 0000000000000..38a1095703b69 --- /dev/null +++ b/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs @@ -0,0 +1,15 @@ +// run-rustfix +// compile-flags: --cfg=whatever -Aunused + +#[cfg(whatever)] +use y::Whatever; + +mod y { + pub(crate) fn z() {} + pub(crate) struct Whatever; +} + +fn main() { + z(); + //~^ ERROR cannot find function `z` in this scope +} diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr b/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr new file mode 100644 index 0000000000000..d3574851d5c11 --- /dev/null +++ b/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr @@ -0,0 +1,14 @@ +error[E0425]: cannot find function `z` in this scope + --> $DIR/suggest-import-without-clobbering-attrs.rs:13:5 + | +LL | z(); + | ^ not found in this scope + | +help: consider importing this function + | +LL + use y::z; + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0425`. From 03bee1f53dbf28c582d52ced9b165812514303f9 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 7 Sep 2023 05:13:52 +0000 Subject: [PATCH 2/2] Find lowest span out of use + attrs --- compiler/rustc_resolve/src/diagnostics.rs | 8 +++++++- .../resolve/suggest-import-without-clobbering-attrs.fixed | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index d99fc07a7cd28..e9edfb53b4450 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -2753,7 +2753,13 @@ fn search_for_any_use_in_items(items: &[P]) -> Option { for item in items { if let ItemKind::Use(..) = item.kind { if is_span_suitable_for_use_injection(item.span) { - return Some(item.span.shrink_to_lo()); + let mut lo = item.span.lo(); + for attr in &item.attrs { + if attr.span.eq_ctxt(item.span) { + lo = std::cmp::min(lo, attr.span.lo()); + } + } + return Some(Span::new(lo, lo, item.span.ctxt(), item.span.parent())); } } } diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed b/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed index 881b973e3ef1e..fc68884fe9c6f 100644 --- a/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed +++ b/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed @@ -1,8 +1,8 @@ // run-rustfix // compile-flags: --cfg=whatever -Aunused -#[cfg(whatever)] use y::z; +#[cfg(whatever)] use y::Whatever; mod y {