Skip to content

Commit 3c111c1

Browse files
committed
Accurate use rename suggestion span
1 parent ae40a3a commit 3c111c1

17 files changed

+70
-98
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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use foo::Bar;
99
= note: `Bar` must be defined only once in the type namespace of this block
1010
help: you can use `as` to change the binding name of the import
1111
|
12-
LL - use foo::Bar;
13-
LL + use foo::Bar as OtherBar;
14-
|
12+
LL | use foo::Bar as OtherBar;
13+
| +++++++++++
1514

1615
error: aborting due to 1 previous error
1716

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ LL | use foo::foo;
1010
= note: `foo` must be defined only once in the type namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL - use foo::foo;
14-
LL + use foo::foo as other_foo;
15-
|
13+
LL | use foo::foo as other_foo;
14+
| ++++++++++++
1615

1716
error: aborting due to 1 previous error
1817

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ LL | macro_rules! panic { () => {} }
1010
= note: `panic` must be defined only once in the macro namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL - pub use std::panic;
14-
LL + pub use std::panic as other_panic;
15-
|
13+
LL | pub use std::panic as other_panic;
14+
| ++++++++++++++
1615

1716
error: aborting due to 1 previous error
1817

tests/ui/error-codes/E0252.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use bar::baz;
99
= note: `baz` must be defined only once in the type namespace of this module
1010
help: you can use `as` to change the binding name of the import
1111
|
12-
LL - use bar::baz;
13-
LL + use bar::baz as other_baz;
14-
|
12+
LL | use bar::baz as other_baz;
13+
| ++++++++++++
1514

1615
error: aborting due to 1 previous error
1716

tests/ui/error-codes/E0254.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ LL | use foo::alloc;
1010
= note: `alloc` must be defined only once in the type namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL - use foo::alloc;
14-
LL + use foo::alloc as other_alloc;
15-
|
13+
LL | use foo::alloc as other_alloc;
14+
| ++++++++++++++
1615

1716
error: aborting due to 1 previous error
1817

tests/ui/error-codes/E0255.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ LL | fn foo() {}
1010
= note: `foo` must be defined only once in the value namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL - use bar::foo;
14-
LL + use bar::foo as other_foo;
15-
|
13+
LL | use bar::foo as other_foo;
14+
| ++++++++++++
1615

1716
error: aborting due to 1 previous error
1817

tests/ui/imports/double-import.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use sub2::foo;
99
= note: `foo` must be defined only once in the value namespace of this module
1010
help: you can use `as` to change the binding name of the import
1111
|
12-
LL - use sub2::foo;
13-
LL + use sub2::foo as other_foo;
14-
|
12+
LL | use sub2::foo as other_foo;
13+
| ++++++++++++
1514

1615
error: aborting due to 1 previous error
1716

tests/ui/imports/issue-19498.stderr

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ LL | mod A {}
1010
= note: `A` must be defined only once in the type namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL - use self::A;
14-
LL + use self::A as OtherA;
15-
|
13+
LL | use self::A as OtherA;
14+
| +++++++++
1615

1716
error[E0255]: the name `B` is defined multiple times
1817
--> $DIR/issue-19498.rs:5:1
@@ -26,9 +25,8 @@ LL | pub mod B {}
2625
= note: `B` must be defined only once in the type namespace of this module
2726
help: you can use `as` to change the binding name of the import
2827
|
29-
LL - use self::B;
30-
LL + use self::B as OtherB;
31-
|
28+
LL | use self::B as OtherB;
29+
| +++++++++
3230

3331
error[E0255]: the name `D` is defined multiple times
3432
--> $DIR/issue-19498.rs:9:5
@@ -41,9 +39,8 @@ LL | mod D {}
4139
= note: `D` must be defined only once in the type namespace of this module
4240
help: you can use `as` to change the binding name of the import
4341
|
44-
LL - use C::D;
45-
LL + use C::D as OtherD;
46-
|
42+
LL | use C::D as OtherD;
43+
| +++++++++
4744

4845
error: aborting due to 3 previous errors
4946

tests/ui/imports/issue-24081.stderr

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ LL | type Add = bool;
1010
= note: `Add` must be defined only once in the type namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL - use std::ops::Add;
14-
LL + use std::ops::Add as OtherAdd;
15-
|
13+
LL | use std::ops::Add as OtherAdd;
14+
| +++++++++++
1615

1716
error[E0255]: the name `Sub` is defined multiple times
1817
--> $DIR/issue-24081.rs:9:1
@@ -26,9 +25,8 @@ LL | struct Sub { x: f32 }
2625
= note: `Sub` must be defined only once in the type namespace of this module
2726
help: you can use `as` to change the binding name of the import
2827
|
29-
LL - use std::ops::Sub;
30-
LL + use std::ops::Sub as OtherSub;
31-
|
28+
LL | use std::ops::Sub as OtherSub;
29+
| +++++++++++
3230

3331
error[E0255]: the name `Mul` is defined multiple times
3432
--> $DIR/issue-24081.rs:11:1
@@ -42,9 +40,8 @@ LL | enum Mul { A, B }
4240
= note: `Mul` must be defined only once in the type namespace of this module
4341
help: you can use `as` to change the binding name of the import
4442
|
45-
LL - use std::ops::Mul;
46-
LL + use std::ops::Mul as OtherMul;
47-
|
43+
LL | use std::ops::Mul as OtherMul;
44+
| +++++++++++
4845

4946
error[E0255]: the name `Div` is defined multiple times
5047
--> $DIR/issue-24081.rs:13:1
@@ -58,9 +55,8 @@ LL | mod Div { }
5855
= note: `Div` must be defined only once in the type namespace of this module
5956
help: you can use `as` to change the binding name of the import
6057
|
61-
LL - use std::ops::Div;
62-
LL + use std::ops::Div as OtherDiv;
63-
|
58+
LL | use std::ops::Div as OtherDiv;
59+
| +++++++++++
6460

6561
error[E0255]: the name `Rem` is defined multiple times
6662
--> $DIR/issue-24081.rs:15:1
@@ -74,9 +70,8 @@ LL | trait Rem { }
7470
= note: `Rem` must be defined only once in the type namespace of this module
7571
help: you can use `as` to change the binding name of the import
7672
|
77-
LL - use std::ops::Rem;
78-
LL + use std::ops::Rem as OtherRem;
79-
|
73+
LL | use std::ops::Rem as OtherRem;
74+
| +++++++++++
8075

8176
error: aborting due to 5 previous errors
8277

tests/ui/imports/issue-25396.stderr

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use bar::baz;
99
= note: `baz` must be defined only once in the type namespace of this module
1010
help: you can use `as` to change the binding name of the import
1111
|
12-
LL - use bar::baz;
13-
LL + use bar::baz as other_baz;
14-
|
12+
LL | use bar::baz as other_baz;
13+
| ++++++++++++
1514

1615
error[E0252]: the name `Quux` is defined multiple times
1716
--> $DIR/issue-25396.rs:7:5
@@ -24,9 +23,8 @@ LL | use bar::Quux;
2423
= note: `Quux` must be defined only once in the type namespace of this module
2524
help: you can use `as` to change the binding name of the import
2625
|
27-
LL - use bar::Quux;
28-
LL + use bar::Quux as OtherQuux;
29-
|
26+
LL | use bar::Quux as OtherQuux;
27+
| ++++++++++++
3028

3129
error[E0252]: the name `blah` is defined multiple times
3230
--> $DIR/issue-25396.rs:10:5
@@ -39,9 +37,8 @@ LL | use bar::blah;
3937
= note: `blah` must be defined only once in the type namespace of this module
4038
help: you can use `as` to change the binding name of the import
4139
|
42-
LL - use bar::blah;
43-
LL + use bar::blah as other_blah;
44-
|
40+
LL | use bar::blah as other_blah;
41+
| +++++++++++++
4542

4643
error[E0252]: the name `WOMP` is defined multiple times
4744
--> $DIR/issue-25396.rs:13:5
@@ -54,9 +51,8 @@ LL | use bar::WOMP;
5451
= note: `WOMP` must be defined only once in the value namespace of this module
5552
help: you can use `as` to change the binding name of the import
5653
|
57-
LL - use bar::WOMP;
58-
LL + use bar::WOMP as OtherWOMP;
59-
|
54+
LL | use bar::WOMP as OtherWOMP;
55+
| ++++++++++++
6056

6157
error: aborting due to 4 previous errors
6258

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use extension2::ConstructorExtension;
99
= note: `ConstructorExtension` must be defined only once in the type namespace of this module
1010
help: you can use `as` to change the binding name of the import
1111
|
12-
LL - use extension2::ConstructorExtension;
13-
LL + use extension2::ConstructorExtension as OtherConstructorExtension;
14-
|
12+
LL | use extension2::ConstructorExtension as OtherConstructorExtension;
13+
| ++++++++++++++++++++++++++++
1514

1615
error: aborting due to 1 previous error
1716

tests/ui/imports/issue-52891.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ LL | use issue_52891::b::inner;
9797
= note: `inner` must be defined only once in the type namespace of this module
9898
help: you can use `as` to change the binding name of the import
9999
|
100-
LL - use issue_52891::b::inner;
101-
LL + use issue_52891::b::inner as other_inner;
102-
|
100+
LL | use issue_52891::b::inner as other_inner;
101+
| ++++++++++++++
103102

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

tests/ui/imports/issue-8640.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | mod bar {}
99
= note: `bar` must be defined only once in the type namespace of this module
1010
help: you can use `as` to change the binding name of the import
1111
|
12-
LL - use baz::bar;
13-
LL + use baz::bar as other_bar;
14-
|
12+
LL | use baz::bar as other_bar;
13+
| ++++++++++++
1514

1615
error: aborting due to 1 previous error
1716

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ LL | fn transmute() {}
1010
= note: `transmute` must be defined only once in the value namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL - use std::mem::transmute;
14-
LL + use std::mem::transmute as other_transmute;
15-
|
13+
LL | use std::mem::transmute as other_transmute;
14+
| ++++++++++++++++++
1615

1716
error: aborting due to 1 previous error
1817

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ LL | struct Iter;
1010
= note: `Iter` must be defined only once in the type namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL - use std::slice::Iter;
14-
LL + use std::slice::Iter as OtherIter;
15-
|
13+
LL | use std::slice::Iter as OtherIter;
14+
| ++++++++++++
1615

1716
error: aborting due to 1 previous error
1817

tests/ui/variants/variant-namespacing.stderr

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit};
1010
= note: `XStruct` must be defined only once in the type namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL - pub use variant_namespacing::XE::{XStruct, XTuple, XUnit};
14-
LL + pub use variant_namespacing::XE::{XStruct as OtherXStruct, XTuple, XUnit};
15-
|
13+
LL | pub use variant_namespacing::XE::{XStruct as OtherXStruct, XTuple, XUnit};
14+
| +++++++++++++++
1615

1716
error[E0255]: the name `XTuple` is defined multiple times
1817
--> $DIR/variant-namespacing.rs:24:44
@@ -26,9 +25,8 @@ LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit};
2625
= note: `XTuple` must be defined only once in the type namespace of this module
2726
help: you can use `as` to change the binding name of the import
2827
|
29-
LL - pub use variant_namespacing::XE::{XStruct, XTuple, XUnit};
30-
LL + pub use variant_namespacing::XE::{XStruct, XTuple as OtherXTuple, XUnit};
31-
|
28+
LL | pub use variant_namespacing::XE::{XStruct, XTuple as OtherXTuple, XUnit};
29+
| ++++++++++++++
3230

3331
error[E0255]: the name `XUnit` is defined multiple times
3432
--> $DIR/variant-namespacing.rs:24:52
@@ -42,9 +40,8 @@ LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit};
4240
= note: `XUnit` must be defined only once in the type namespace of this module
4341
help: you can use `as` to change the binding name of the import
4442
|
45-
LL - pub use variant_namespacing::XE::{XStruct, XTuple, XUnit};
46-
LL + pub use variant_namespacing::XE::{XStruct, XTuple, XUnit as OtherXUnit};
47-
|
43+
LL | pub use variant_namespacing::XE::{XStruct, XTuple, XUnit as OtherXUnit};
44+
| +++++++++++++
4845

4946
error[E0255]: the name `Struct` is defined multiple times
5047
--> $DIR/variant-namespacing.rs:28:13
@@ -58,9 +55,8 @@ LL | pub use E::{Struct, Tuple, Unit};
5855
= note: `Struct` must be defined only once in the type namespace of this module
5956
help: you can use `as` to change the binding name of the import
6057
|
61-
LL - pub use E::{Struct, Tuple, Unit};
62-
LL + pub use E::{Struct as OtherStruct, Tuple, Unit};
63-
|
58+
LL | pub use E::{Struct as OtherStruct, Tuple, Unit};
59+
| ++++++++++++++
6460

6561
error[E0255]: the name `Tuple` is defined multiple times
6662
--> $DIR/variant-namespacing.rs:28:21
@@ -74,9 +70,8 @@ LL | pub use E::{Struct, Tuple, Unit};
7470
= note: `Tuple` must be defined only once in the type namespace of this module
7571
help: you can use `as` to change the binding name of the import
7672
|
77-
LL - pub use E::{Struct, Tuple, Unit};
78-
LL + pub use E::{Struct, Tuple as OtherTuple, Unit};
79-
|
73+
LL | pub use E::{Struct, Tuple as OtherTuple, Unit};
74+
| +++++++++++++
8075

8176
error[E0255]: the name `Unit` is defined multiple times
8277
--> $DIR/variant-namespacing.rs:28:28
@@ -90,9 +85,8 @@ LL | pub use E::{Struct, Tuple, Unit};
9085
= note: `Unit` must be defined only once in the type namespace of this module
9186
help: you can use `as` to change the binding name of the import
9287
|
93-
LL - pub use E::{Struct, Tuple, Unit};
94-
LL + pub use E::{Struct, Tuple, Unit as OtherUnit};
95-
|
88+
LL | pub use E::{Struct, Tuple, Unit as OtherUnit};
89+
| ++++++++++++
9690

9791
error: aborting due to 6 previous errors
9892

0 commit comments

Comments
 (0)