From 8eb51852a87dd2640fbd95019ff1124b38ae8b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 10 Jul 2024 01:49:11 +0000 Subject: [PATCH] Accurate `use` rename suggestion span When suggesting to rename an import with `as`, use a smaller span to render the suggestion with a better format: ``` error[E0252]: the name `baz` is defined multiple times --> $DIR/issue-25396.rs:4:5 | LL | use foo::baz; | -------- previous import of the module `baz` here LL | use bar::baz; | ^^^^^^^^ `baz` reimported here | = note: `baz` must be defined only once in the type namespace of this module help: you can use `as` to change the binding name of the import | LL | use bar::baz as other_baz; | ++++++++++++ ``` --- compiler/rustc_resolve/src/diagnostics.rs | 18 ++++++++++-------- .../ice-unresolved-import-100241.stderr | 11 +++++++++++ .../blind/blind-item-block-item-shadow.stderr | 2 +- tests/ui/blind/blind-item-item-shadow.stderr | 2 +- .../duplicate-check-macro-exports.stderr | 2 +- tests/ui/error-codes/E0252.stderr | 2 +- tests/ui/error-codes/E0254.stderr | 2 +- tests/ui/error-codes/E0255.stderr | 2 +- tests/ui/imports/double-import.stderr | 2 +- tests/ui/imports/issue-19498.stderr | 6 +++--- tests/ui/imports/issue-24081.stderr | 10 +++++----- tests/ui/imports/issue-25396.stderr | 8 ++++---- .../issue-32354-suggest-import-rename.stderr | 2 +- .../ui/imports/issue-45829/import-self.stderr | 2 +- .../ui/imports/issue-45829/issue-45829.stderr | 2 +- .../issue-45829/rename-use-vs-extern.stderr | 2 +- .../issue-45829/rename-use-with-tabs.stderr | 2 +- .../issue-45829/rename-with-path.stderr | 2 +- tests/ui/imports/issue-45829/rename.stderr | 2 +- tests/ui/imports/issue-52891.stderr | 2 +- tests/ui/imports/issue-8640.stderr | 2 +- ...olve-conflict-import-vs-extern-crate.stderr | 2 +- .../resolve-conflict-item-vs-import.stderr | 2 +- .../resolve-conflict-type-vs-import.stderr | 2 +- tests/ui/variants/variant-namespacing.stderr | 12 ++++++------ 25 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 tests/rustdoc-ui/ice-unresolved-import-100241.stderr diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index e3917acce65b5..566223f98bfdb 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -371,6 +371,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { }; let mut suggestion = None; + let mut span = binding_span; match import.kind { ImportKind::Single { type_ns_only: true, .. } => { suggestion = Some(format!("self as {suggested_name}")) @@ -381,12 +382,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { { if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(binding_span) { if pos <= snippet.len() { - suggestion = Some(format!( - "{} as {}{}", - &snippet[..pos], - suggested_name, - if snippet.ends_with(';') { ";" } else { "" } - )) + span = binding_span + .with_lo(binding_span.lo() + BytePos(pos as u32)) + .with_hi( + binding_span.hi() + - BytePos(if snippet.ends_with(';') { 1 } else { 0 }), + ); + suggestion = Some(format!(" as {suggested_name}")); } } } @@ -402,9 +404,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } if let Some(suggestion) = suggestion { - err.subdiagnostic(ChangeImportBindingSuggestion { span: binding_span, suggestion }); + err.subdiagnostic(ChangeImportBindingSuggestion { span, suggestion }); } else { - err.subdiagnostic(ChangeImportBinding { span: binding_span }); + err.subdiagnostic(ChangeImportBinding { span }); } } diff --git a/tests/rustdoc-ui/ice-unresolved-import-100241.stderr b/tests/rustdoc-ui/ice-unresolved-import-100241.stderr new file mode 100644 index 0000000000000..57fbbb59c8d41 --- /dev/null +++ b/tests/rustdoc-ui/ice-unresolved-import-100241.stderr @@ -0,0 +1,11 @@ +error[E0432]: unresolved import `inner` + --> $DIR/ice-unresolved-import-100241.rs:9:13 + | +LL | pub use inner::S; + | ^^^^^ maybe a missing crate `inner`? + | + = help: consider adding `extern crate inner` to use the `inner` crate + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0432`. diff --git a/tests/ui/blind/blind-item-block-item-shadow.stderr b/tests/ui/blind/blind-item-block-item-shadow.stderr index 2e24ef453c2e4..38e3087ca3461 100644 --- a/tests/ui/blind/blind-item-block-item-shadow.stderr +++ b/tests/ui/blind/blind-item-block-item-shadow.stderr @@ -10,7 +10,7 @@ LL | use foo::Bar; help: you can use `as` to change the binding name of the import | LL | use foo::Bar as OtherBar; - | ~~~~~~~~~~~~~~~~~~~~ + | +++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/blind/blind-item-item-shadow.stderr b/tests/ui/blind/blind-item-item-shadow.stderr index 84b273c7338b1..08f5f5b47ed49 100644 --- a/tests/ui/blind/blind-item-item-shadow.stderr +++ b/tests/ui/blind/blind-item-item-shadow.stderr @@ -11,7 +11,7 @@ LL | use foo::foo; help: you can use `as` to change the binding name of the import | LL | use foo::foo as other_foo; - | ~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/duplicate/duplicate-check-macro-exports.stderr b/tests/ui/duplicate/duplicate-check-macro-exports.stderr index eff19c09062f0..470a516ecbc52 100644 --- a/tests/ui/duplicate/duplicate-check-macro-exports.stderr +++ b/tests/ui/duplicate/duplicate-check-macro-exports.stderr @@ -11,7 +11,7 @@ LL | macro_rules! panic { () => {} } help: you can use `as` to change the binding name of the import | LL | pub use std::panic as other_panic; - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/error-codes/E0252.stderr b/tests/ui/error-codes/E0252.stderr index efbb9ec96dee6..16574964eb33e 100644 --- a/tests/ui/error-codes/E0252.stderr +++ b/tests/ui/error-codes/E0252.stderr @@ -10,7 +10,7 @@ LL | use bar::baz; help: you can use `as` to change the binding name of the import | LL | use bar::baz as other_baz; - | ~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/error-codes/E0254.stderr b/tests/ui/error-codes/E0254.stderr index d89497b2804bf..592b8766d6bd8 100644 --- a/tests/ui/error-codes/E0254.stderr +++ b/tests/ui/error-codes/E0254.stderr @@ -11,7 +11,7 @@ LL | use foo::alloc; help: you can use `as` to change the binding name of the import | LL | use foo::alloc as other_alloc; - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/error-codes/E0255.stderr b/tests/ui/error-codes/E0255.stderr index e5f908217e138..b67a334f02c4c 100644 --- a/tests/ui/error-codes/E0255.stderr +++ b/tests/ui/error-codes/E0255.stderr @@ -11,7 +11,7 @@ LL | fn foo() {} help: you can use `as` to change the binding name of the import | LL | use bar::foo as other_foo; - | ~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/imports/double-import.stderr b/tests/ui/imports/double-import.stderr index 73bb73e349066..15b8620909f82 100644 --- a/tests/ui/imports/double-import.stderr +++ b/tests/ui/imports/double-import.stderr @@ -10,7 +10,7 @@ LL | use sub2::foo; help: you can use `as` to change the binding name of the import | LL | use sub2::foo as other_foo; - | ~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/imports/issue-19498.stderr b/tests/ui/imports/issue-19498.stderr index 9d26022098fdd..69bdb67d3898b 100644 --- a/tests/ui/imports/issue-19498.stderr +++ b/tests/ui/imports/issue-19498.stderr @@ -11,7 +11,7 @@ LL | mod A {} help: you can use `as` to change the binding name of the import | LL | use self::A as OtherA; - | ~~~~~~~~~~~~~~~~~ + | +++++++++ error[E0255]: the name `B` is defined multiple times --> $DIR/issue-19498.rs:5:1 @@ -26,7 +26,7 @@ LL | pub mod B {} help: you can use `as` to change the binding name of the import | LL | use self::B as OtherB; - | ~~~~~~~~~~~~~~~~~ + | +++++++++ error[E0255]: the name `D` is defined multiple times --> $DIR/issue-19498.rs:9:5 @@ -40,7 +40,7 @@ LL | mod D {} help: you can use `as` to change the binding name of the import | LL | use C::D as OtherD; - | ~~~~~~~~~~~~~~ + | +++++++++ error: aborting due to 3 previous errors diff --git a/tests/ui/imports/issue-24081.stderr b/tests/ui/imports/issue-24081.stderr index e5ed6b10a5479..6ceba6c7013ac 100644 --- a/tests/ui/imports/issue-24081.stderr +++ b/tests/ui/imports/issue-24081.stderr @@ -11,7 +11,7 @@ LL | type Add = bool; help: you can use `as` to change the binding name of the import | LL | use std::ops::Add as OtherAdd; - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++ error[E0255]: the name `Sub` is defined multiple times --> $DIR/issue-24081.rs:9:1 @@ -26,7 +26,7 @@ LL | struct Sub { x: f32 } help: you can use `as` to change the binding name of the import | LL | use std::ops::Sub as OtherSub; - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++ error[E0255]: the name `Mul` is defined multiple times --> $DIR/issue-24081.rs:11:1 @@ -41,7 +41,7 @@ LL | enum Mul { A, B } help: you can use `as` to change the binding name of the import | LL | use std::ops::Mul as OtherMul; - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++ error[E0255]: the name `Div` is defined multiple times --> $DIR/issue-24081.rs:13:1 @@ -56,7 +56,7 @@ LL | mod Div { } help: you can use `as` to change the binding name of the import | LL | use std::ops::Div as OtherDiv; - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++ error[E0255]: the name `Rem` is defined multiple times --> $DIR/issue-24081.rs:15:1 @@ -71,7 +71,7 @@ LL | trait Rem { } help: you can use `as` to change the binding name of the import | LL | use std::ops::Rem as OtherRem; - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++ error: aborting due to 5 previous errors diff --git a/tests/ui/imports/issue-25396.stderr b/tests/ui/imports/issue-25396.stderr index 518d2be784111..6f9b080f3c509 100644 --- a/tests/ui/imports/issue-25396.stderr +++ b/tests/ui/imports/issue-25396.stderr @@ -10,7 +10,7 @@ LL | use bar::baz; help: you can use `as` to change the binding name of the import | LL | use bar::baz as other_baz; - | ~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++ error[E0252]: the name `Quux` is defined multiple times --> $DIR/issue-25396.rs:7:5 @@ -24,7 +24,7 @@ LL | use bar::Quux; help: you can use `as` to change the binding name of the import | LL | use bar::Quux as OtherQuux; - | ~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++ error[E0252]: the name `blah` is defined multiple times --> $DIR/issue-25396.rs:10:5 @@ -38,7 +38,7 @@ LL | use bar::blah; help: you can use `as` to change the binding name of the import | LL | use bar::blah as other_blah; - | ~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++ error[E0252]: the name `WOMP` is defined multiple times --> $DIR/issue-25396.rs:13:5 @@ -52,7 +52,7 @@ LL | use bar::WOMP; help: you can use `as` to change the binding name of the import | LL | use bar::WOMP as OtherWOMP; - | ~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++ error: aborting due to 4 previous errors diff --git a/tests/ui/imports/issue-32354-suggest-import-rename.stderr b/tests/ui/imports/issue-32354-suggest-import-rename.stderr index de9bdc4f2cc85..753fa434ffba9 100644 --- a/tests/ui/imports/issue-32354-suggest-import-rename.stderr +++ b/tests/ui/imports/issue-32354-suggest-import-rename.stderr @@ -10,7 +10,7 @@ LL | use extension2::ConstructorExtension; help: you can use `as` to change the binding name of the import | LL | use extension2::ConstructorExtension as OtherConstructorExtension; - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++++++++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/imports/issue-45829/import-self.stderr b/tests/ui/imports/issue-45829/import-self.stderr index 0c9424f308351..3c9d4fe6ba6fb 100644 --- a/tests/ui/imports/issue-45829/import-self.stderr +++ b/tests/ui/imports/issue-45829/import-self.stderr @@ -48,7 +48,7 @@ LL | use foo::self; help: you can use `as` to change the binding name of the import | LL | use foo as other_foo; - | ~~~~~~~~~~~~~~~~ + | ~~~~~~~~~~~~ error[E0252]: the name `A` is defined multiple times --> $DIR/import-self.rs:16:11 diff --git a/tests/ui/imports/issue-45829/issue-45829.stderr b/tests/ui/imports/issue-45829/issue-45829.stderr index 627a09a07b860..c6835c3bd7aaa 100644 --- a/tests/ui/imports/issue-45829/issue-45829.stderr +++ b/tests/ui/imports/issue-45829/issue-45829.stderr @@ -10,7 +10,7 @@ LL | use foo::{A, B as A}; help: you can use `as` to change the binding name of the import | LL | use foo::{A, B as OtherA}; - | ~~~~~~~~~~~ + | ~~~~~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/imports/issue-45829/rename-use-vs-extern.stderr b/tests/ui/imports/issue-45829/rename-use-vs-extern.stderr index 9b0a2534a1d1f..fd4fb9db0b6d7 100644 --- a/tests/ui/imports/issue-45829/rename-use-vs-extern.stderr +++ b/tests/ui/imports/issue-45829/rename-use-vs-extern.stderr @@ -10,7 +10,7 @@ LL | use std as issue_45829_b; help: you can use `as` to change the binding name of the import | LL | use std as other_issue_45829_b; - | ~~~~~~~~~~~~~~~~~~~~~~~~~~ + | ~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/imports/issue-45829/rename-use-with-tabs.stderr b/tests/ui/imports/issue-45829/rename-use-with-tabs.stderr index 5751f41ae9d12..178303bbc1d16 100644 --- a/tests/ui/imports/issue-45829/rename-use-with-tabs.stderr +++ b/tests/ui/imports/issue-45829/rename-use-with-tabs.stderr @@ -10,7 +10,7 @@ LL | use foo::{A, bar::B as A}; help: you can use `as` to change the binding name of the import | LL | use foo::{A, bar::B as OtherA}; - | ~~~~~~~~~~~~~~~~ + | ~~~~~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/imports/issue-45829/rename-with-path.stderr b/tests/ui/imports/issue-45829/rename-with-path.stderr index 69e084db6ffc1..a83fdb3732436 100644 --- a/tests/ui/imports/issue-45829/rename-with-path.stderr +++ b/tests/ui/imports/issue-45829/rename-with-path.stderr @@ -10,7 +10,7 @@ LL | use std::{collections::HashMap as A, sync::Arc as A}; help: you can use `as` to change the binding name of the import | LL | use std::{collections::HashMap as A, sync::Arc as OtherA}; - | ~~~~~~~~~~~~~~~~~~~ + | ~~~~~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/imports/issue-45829/rename.stderr b/tests/ui/imports/issue-45829/rename.stderr index f1ee5112dc164..4977909487cfb 100644 --- a/tests/ui/imports/issue-45829/rename.stderr +++ b/tests/ui/imports/issue-45829/rename.stderr @@ -10,7 +10,7 @@ LL | use std as core; help: you can use `as` to change the binding name of the import | LL | use std as other_core; - | ~~~~~~~~~~~~~~~~~ + | ~~~~~~~~~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/imports/issue-52891.stderr b/tests/ui/imports/issue-52891.stderr index 7bb1301edf264..730962d702a06 100644 --- a/tests/ui/imports/issue-52891.stderr +++ b/tests/ui/imports/issue-52891.stderr @@ -98,7 +98,7 @@ LL | use issue_52891::b::inner; help: you can use `as` to change the binding name of the import | LL | use issue_52891::b::inner as other_inner; - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++++ error[E0254]: the name `issue_52891` is defined multiple times --> $DIR/issue-52891.rs:31:19 diff --git a/tests/ui/imports/issue-8640.stderr b/tests/ui/imports/issue-8640.stderr index ea350e97e6420..d22fddb1a69e5 100644 --- a/tests/ui/imports/issue-8640.stderr +++ b/tests/ui/imports/issue-8640.stderr @@ -10,7 +10,7 @@ LL | mod bar {} help: you can use `as` to change the binding name of the import | LL | use baz::bar as other_bar; - | ~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr b/tests/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr index e22e636adb6b5..a8d0efedb6c42 100644 --- a/tests/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr +++ b/tests/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr @@ -8,7 +8,7 @@ LL | use std::slice as std; help: you can use `as` to change the binding name of the import | LL | use std::slice as other_std; - | ~~~~~~~~~~~~~~~~~~~~~~~ + | ~~~~~~~~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/resolve/resolve-conflict-item-vs-import.stderr b/tests/ui/resolve/resolve-conflict-item-vs-import.stderr index 3b1b5f1ad001e..a8b16009c5542 100644 --- a/tests/ui/resolve/resolve-conflict-item-vs-import.stderr +++ b/tests/ui/resolve/resolve-conflict-item-vs-import.stderr @@ -11,7 +11,7 @@ LL | fn transmute() {} help: you can use `as` to change the binding name of the import | LL | use std::mem::transmute as other_transmute; - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/resolve/resolve-conflict-type-vs-import.stderr b/tests/ui/resolve/resolve-conflict-type-vs-import.stderr index c5cb4e078620a..241e48d0cd562 100644 --- a/tests/ui/resolve/resolve-conflict-type-vs-import.stderr +++ b/tests/ui/resolve/resolve-conflict-type-vs-import.stderr @@ -11,7 +11,7 @@ LL | struct Iter; help: you can use `as` to change the binding name of the import | LL | use std::slice::Iter as OtherIter; - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/variants/variant-namespacing.stderr b/tests/ui/variants/variant-namespacing.stderr index 9e91ff7178d98..2a2491b81f940 100644 --- a/tests/ui/variants/variant-namespacing.stderr +++ b/tests/ui/variants/variant-namespacing.stderr @@ -11,7 +11,7 @@ LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit}; help: you can use `as` to change the binding name of the import | LL | pub use variant_namespacing::XE::{XStruct as OtherXStruct, XTuple, XUnit}; - | ~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++ error[E0255]: the name `XTuple` is defined multiple times --> $DIR/variant-namespacing.rs:24:44 @@ -26,7 +26,7 @@ LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit}; help: you can use `as` to change the binding name of the import | LL | pub use variant_namespacing::XE::{XStruct, XTuple as OtherXTuple, XUnit}; - | ~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++++ error[E0255]: the name `XUnit` is defined multiple times --> $DIR/variant-namespacing.rs:24:52 @@ -41,7 +41,7 @@ LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit}; help: you can use `as` to change the binding name of the import | LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit as OtherXUnit}; - | ~~~~~~~~~~~~~~~~~~~ + | +++++++++++++ error[E0255]: the name `Struct` is defined multiple times --> $DIR/variant-namespacing.rs:28:13 @@ -56,7 +56,7 @@ LL | pub use E::{Struct, Tuple, Unit}; help: you can use `as` to change the binding name of the import | LL | pub use E::{Struct as OtherStruct, Tuple, Unit}; - | ~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++++ error[E0255]: the name `Tuple` is defined multiple times --> $DIR/variant-namespacing.rs:28:21 @@ -71,7 +71,7 @@ LL | pub use E::{Struct, Tuple, Unit}; help: you can use `as` to change the binding name of the import | LL | pub use E::{Struct, Tuple as OtherTuple, Unit}; - | ~~~~~~~~~~~~~~~~~~~ + | +++++++++++++ error[E0255]: the name `Unit` is defined multiple times --> $DIR/variant-namespacing.rs:28:28 @@ -86,7 +86,7 @@ LL | pub use E::{Struct, Tuple, Unit}; help: you can use `as` to change the binding name of the import | LL | pub use E::{Struct, Tuple, Unit as OtherUnit}; - | ~~~~~~~~~~~~~~~~~ + | ++++++++++++ error: aborting due to 6 previous errors