Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accurate use rename suggestion span #127886

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"))
Expand All @@ -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}"));
}
}
}
Expand All @@ -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 });
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/rustdoc-ui/ice-unresolved-import-100241.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion tests/ui/blind/blind-item-block-item-shadow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/blind/blind-item-item-shadow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/duplicate/duplicate-check-macro-exports.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0252.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0254.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0255.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/double-import.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/imports/issue-19498.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
10 changes: 5 additions & 5 deletions tests/ui/imports/issue-24081.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/imports/issue-25396.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-32354-suggest-import-rename.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-45829/import-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-45829/issue-45829.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-45829/rename-use-vs-extern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-45829/rename-use-with-tabs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-45829/rename-with-path.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-45829/rename.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-52891.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-8640.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/resolve/resolve-conflict-item-vs-import.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/resolve/resolve-conflict-type-vs-import.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/variants/variant-namespacing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
Loading