Skip to content

Commit 8eb5185

Browse files
committed
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; | ++++++++++++ ```
1 parent 032be6f commit 8eb5185

25 files changed

+58
-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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0432]: unresolved import `inner`
2+
--> $DIR/ice-unresolved-import-100241.rs:9:13
3+
|
4+
LL | pub use inner::S;
5+
| ^^^^^ maybe a missing crate `inner`?
6+
|
7+
= help: consider adding `extern crate inner` to use the `inner` crate
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0432`.

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

0 commit comments

Comments
 (0)