Skip to content

Commit b52883d

Browse files
authored
Rollup merge of #127886 - estebank:as-rename-suggestion, r=compiler-errors
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; | ++++++++++++ ```
2 parents a13d7db + 8eb5185 commit b52883d

24 files changed

+47
-45
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
371371
};
372372

373373
let mut suggestion = None;
374+
let mut span = binding_span;
374375
match import.kind {
375376
ImportKind::Single { type_ns_only: true, .. } => {
376377
suggestion = Some(format!("self as {suggested_name}"))
@@ -381,12 +382,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
381382
{
382383
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(binding_span) {
383384
if pos <= snippet.len() {
384-
suggestion = Some(format!(
385-
"{} as {}{}",
386-
&snippet[..pos],
387-
suggested_name,
388-
if snippet.ends_with(';') { ";" } else { "" }
389-
))
385+
span = binding_span
386+
.with_lo(binding_span.lo() + BytePos(pos as u32))
387+
.with_hi(
388+
binding_span.hi()
389+
- BytePos(if snippet.ends_with(';') { 1 } else { 0 }),
390+
);
391+
suggestion = Some(format!(" as {suggested_name}"));
390392
}
391393
}
392394
}
@@ -402,9 +404,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
402404
}
403405

404406
if let Some(suggestion) = suggestion {
405-
err.subdiagnostic(ChangeImportBindingSuggestion { span: binding_span, suggestion });
407+
err.subdiagnostic(ChangeImportBindingSuggestion { span, suggestion });
406408
} else {
407-
err.subdiagnostic(ChangeImportBinding { span: binding_span });
409+
err.subdiagnostic(ChangeImportBinding { span });
408410
}
409411
}
410412

tests/ui/blind/blind-item-block-item-shadow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use foo::Bar;
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use foo::Bar as OtherBar;
13-
| ~~~~~~~~~~~~~~~~~~~~
13+
| +++++++++++
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/blind/blind-item-item-shadow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | use foo::foo;
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use foo::foo as other_foo;
14-
| ~~~~~~~~~~~~~~~~~~~~~
14+
| ++++++++++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/duplicate/duplicate-check-macro-exports.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | macro_rules! panic { () => {} }
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | pub use std::panic as other_panic;
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ++++++++++++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/error-codes/E0252.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use bar::baz;
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use bar::baz as other_baz;
13-
| ~~~~~~~~~~~~~~~~~~~~~
13+
| ++++++++++++
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/error-codes/E0254.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | use foo::alloc;
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use foo::alloc as other_alloc;
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ++++++++++++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/error-codes/E0255.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | fn foo() {}
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use bar::foo as other_foo;
14-
| ~~~~~~~~~~~~~~~~~~~~~
14+
| ++++++++++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/imports/double-import.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use sub2::foo;
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use sub2::foo as other_foo;
13-
| ~~~~~~~~~~~~~~~~~~~~~~
13+
| ++++++++++++
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/imports/issue-19498.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | mod A {}
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use self::A as OtherA;
14-
| ~~~~~~~~~~~~~~~~~
14+
| +++++++++
1515

1616
error[E0255]: the name `B` is defined multiple times
1717
--> $DIR/issue-19498.rs:5:1
@@ -26,7 +26,7 @@ LL | pub mod B {}
2626
help: you can use `as` to change the binding name of the import
2727
|
2828
LL | use self::B as OtherB;
29-
| ~~~~~~~~~~~~~~~~~
29+
| +++++++++
3030

3131
error[E0255]: the name `D` is defined multiple times
3232
--> $DIR/issue-19498.rs:9:5
@@ -40,7 +40,7 @@ LL | mod D {}
4040
help: you can use `as` to change the binding name of the import
4141
|
4242
LL | use C::D as OtherD;
43-
| ~~~~~~~~~~~~~~
43+
| +++++++++
4444

4545
error: aborting due to 3 previous errors
4646

tests/ui/imports/issue-24081.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | type Add = bool;
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use std::ops::Add as OtherAdd;
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| +++++++++++
1515

1616
error[E0255]: the name `Sub` is defined multiple times
1717
--> $DIR/issue-24081.rs:9:1
@@ -26,7 +26,7 @@ LL | struct Sub { x: f32 }
2626
help: you can use `as` to change the binding name of the import
2727
|
2828
LL | use std::ops::Sub as OtherSub;
29-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
29+
| +++++++++++
3030

3131
error[E0255]: the name `Mul` is defined multiple times
3232
--> $DIR/issue-24081.rs:11:1
@@ -41,7 +41,7 @@ LL | enum Mul { A, B }
4141
help: you can use `as` to change the binding name of the import
4242
|
4343
LL | use std::ops::Mul as OtherMul;
44-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
44+
| +++++++++++
4545

4646
error[E0255]: the name `Div` is defined multiple times
4747
--> $DIR/issue-24081.rs:13:1
@@ -56,7 +56,7 @@ LL | mod Div { }
5656
help: you can use `as` to change the binding name of the import
5757
|
5858
LL | use std::ops::Div as OtherDiv;
59-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
59+
| +++++++++++
6060

6161
error[E0255]: the name `Rem` is defined multiple times
6262
--> $DIR/issue-24081.rs:15:1
@@ -71,7 +71,7 @@ LL | trait Rem { }
7171
help: you can use `as` to change the binding name of the import
7272
|
7373
LL | use std::ops::Rem as OtherRem;
74-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
74+
| +++++++++++
7575

7676
error: aborting due to 5 previous errors
7777

tests/ui/imports/issue-25396.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use bar::baz;
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use bar::baz as other_baz;
13-
| ~~~~~~~~~~~~~~~~~~~~~
13+
| ++++++++++++
1414

1515
error[E0252]: the name `Quux` is defined multiple times
1616
--> $DIR/issue-25396.rs:7:5
@@ -24,7 +24,7 @@ LL | use bar::Quux;
2424
help: you can use `as` to change the binding name of the import
2525
|
2626
LL | use bar::Quux as OtherQuux;
27-
| ~~~~~~~~~~~~~~~~~~~~~~
27+
| ++++++++++++
2828

2929
error[E0252]: the name `blah` is defined multiple times
3030
--> $DIR/issue-25396.rs:10:5
@@ -38,7 +38,7 @@ LL | use bar::blah;
3838
help: you can use `as` to change the binding name of the import
3939
|
4040
LL | use bar::blah as other_blah;
41-
| ~~~~~~~~~~~~~~~~~~~~~~~
41+
| +++++++++++++
4242

4343
error[E0252]: the name `WOMP` is defined multiple times
4444
--> $DIR/issue-25396.rs:13:5
@@ -52,7 +52,7 @@ LL | use bar::WOMP;
5252
help: you can use `as` to change the binding name of the import
5353
|
5454
LL | use bar::WOMP as OtherWOMP;
55-
| ~~~~~~~~~~~~~~~~~~~~~~
55+
| ++++++++++++
5656

5757
error: aborting due to 4 previous errors
5858

tests/ui/imports/issue-32354-suggest-import-rename.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use extension2::ConstructorExtension;
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use extension2::ConstructorExtension as OtherConstructorExtension;
13-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13+
| ++++++++++++++++++++++++++++
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/imports/issue-45829/import-self.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ LL | use foo::self;
4848
help: you can use `as` to change the binding name of the import
4949
|
5050
LL | use foo as other_foo;
51-
| ~~~~~~~~~~~~~~~~
51+
| ~~~~~~~~~~~~
5252

5353
error[E0252]: the name `A` is defined multiple times
5454
--> $DIR/import-self.rs:16:11

tests/ui/imports/issue-45829/issue-45829.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use foo::{A, B as A};
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use foo::{A, B as OtherA};
13-
| ~~~~~~~~~~~
13+
| ~~~~~~~~~
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/imports/issue-45829/rename-use-vs-extern.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use std as issue_45829_b;
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use std as other_issue_45829_b;
13-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
13+
| ~~~~~~~~~~~~~~~~~~~~~~
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/imports/issue-45829/rename-use-with-tabs.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use foo::{A, bar::B as A};
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use foo::{A, bar::B as OtherA};
13-
| ~~~~~~~~~~~~~~~~
13+
| ~~~~~~~~~
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/imports/issue-45829/rename-with-path.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use std::{collections::HashMap as A, sync::Arc as A};
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use std::{collections::HashMap as A, sync::Arc as OtherA};
13-
| ~~~~~~~~~~~~~~~~~~~
13+
| ~~~~~~~~~
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/imports/issue-45829/rename.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | use std as core;
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use std as other_core;
13-
| ~~~~~~~~~~~~~~~~~
13+
| ~~~~~~~~~~~~~
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/imports/issue-52891.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ LL | use issue_52891::b::inner;
9898
help: you can use `as` to change the binding name of the import
9999
|
100100
LL | use issue_52891::b::inner as other_inner;
101-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101+
| ++++++++++++++
102102

103103
error[E0254]: the name `issue_52891` is defined multiple times
104104
--> $DIR/issue-52891.rs:31:19

tests/ui/imports/issue-8640.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | mod bar {}
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use baz::bar as other_bar;
13-
| ~~~~~~~~~~~~~~~~~~~~~
13+
| ++++++++++++
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | use std::slice as std;
88
help: you can use `as` to change the binding name of the import
99
|
1010
LL | use std::slice as other_std;
11-
| ~~~~~~~~~~~~~~~~~~~~~~~
11+
| ~~~~~~~~~~~~
1212

1313
error: aborting due to 1 previous error
1414

tests/ui/resolve/resolve-conflict-item-vs-import.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | fn transmute() {}
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use std::mem::transmute as other_transmute;
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ++++++++++++++++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/resolve/resolve-conflict-type-vs-import.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | struct Iter;
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use std::slice::Iter as OtherIter;
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ++++++++++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/variants/variant-namespacing.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit};
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | pub use variant_namespacing::XE::{XStruct as OtherXStruct, XTuple, XUnit};
14-
| ~~~~~~~~~~~~~~~~~~~~~~~
14+
| +++++++++++++++
1515

1616
error[E0255]: the name `XTuple` is defined multiple times
1717
--> $DIR/variant-namespacing.rs:24:44
@@ -26,7 +26,7 @@ LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit};
2626
help: you can use `as` to change the binding name of the import
2727
|
2828
LL | pub use variant_namespacing::XE::{XStruct, XTuple as OtherXTuple, XUnit};
29-
| ~~~~~~~~~~~~~~~~~~~~~
29+
| ++++++++++++++
3030

3131
error[E0255]: the name `XUnit` is defined multiple times
3232
--> $DIR/variant-namespacing.rs:24:52
@@ -41,7 +41,7 @@ LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit};
4141
help: you can use `as` to change the binding name of the import
4242
|
4343
LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit as OtherXUnit};
44-
| ~~~~~~~~~~~~~~~~~~~
44+
| +++++++++++++
4545

4646
error[E0255]: the name `Struct` is defined multiple times
4747
--> $DIR/variant-namespacing.rs:28:13
@@ -56,7 +56,7 @@ LL | pub use E::{Struct, Tuple, Unit};
5656
help: you can use `as` to change the binding name of the import
5757
|
5858
LL | pub use E::{Struct as OtherStruct, Tuple, Unit};
59-
| ~~~~~~~~~~~~~~~~~~~~~
59+
| ++++++++++++++
6060

6161
error[E0255]: the name `Tuple` is defined multiple times
6262
--> $DIR/variant-namespacing.rs:28:21
@@ -71,7 +71,7 @@ LL | pub use E::{Struct, Tuple, Unit};
7171
help: you can use `as` to change the binding name of the import
7272
|
7373
LL | pub use E::{Struct, Tuple as OtherTuple, Unit};
74-
| ~~~~~~~~~~~~~~~~~~~
74+
| +++++++++++++
7575

7676
error[E0255]: the name `Unit` is defined multiple times
7777
--> $DIR/variant-namespacing.rs:28:28
@@ -86,7 +86,7 @@ LL | pub use E::{Struct, Tuple, Unit};
8686
help: you can use `as` to change the binding name of the import
8787
|
8888
LL | pub use E::{Struct, Tuple, Unit as OtherUnit};
89-
| ~~~~~~~~~~~~~~~~~
89+
| ++++++++++++
9090

9191
error: aborting due to 6 previous errors
9292

0 commit comments

Comments
 (0)