Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b52883d

Browse files
authored
Rollup merge of rust-lang#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

Lines changed: 10 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
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

0 commit comments

Comments
 (0)