Skip to content

Commit 119e5d5

Browse files
committed
Accurate use rename suggestion span
1 parent aa97280 commit 119e5d5

24 files changed

+81
-119
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+7-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,10 @@ 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_hi(binding_span.lo() + BytePos(pos as u32))
387+
.shrink_to_hi();
388+
suggestion = Some(format!(" as {}", suggested_name,))
390389
}
391390
}
392391
}
@@ -402,9 +401,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
402401
}
403402

404403
if let Some(suggestion) = suggestion {
405-
err.subdiagnostic(ChangeImportBindingSuggestion { span: binding_span, suggestion });
404+
err.subdiagnostic(ChangeImportBindingSuggestion { span, suggestion });
406405
} else {
407-
err.subdiagnostic(ChangeImportBinding { span: binding_span });
406+
err.subdiagnostic(ChangeImportBinding { span });
408407
}
409408
}
410409

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

+2-3
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

+2-3
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

+2-3
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

+2-3
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

+2-3
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

+2-3
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

+2-3
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

+6-9
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

+10-15
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

+8-12
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

+2-3
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-45829/import-self.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ LL | use foo::self;
4848
= note: `foo` must be defined only once in the type namespace of this module
4949
help: you can use `as` to change the binding name of the import
5050
|
51-
LL - use foo::self;
52-
LL + use foo as other_foo;
53-
|
51+
LL | use foo as other_foo::self;
52+
| ++++++++++++
5453

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

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use foo::{A, B as A};
99
= note: `A` 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 foo::{A, B as A};
13-
LL + use foo::{A, B as OtherA};
14-
|
12+
LL | use foo::{A, B as OtherA as A};
13+
| +++++++++
1514

1615
error: aborting due to 1 previous error
1716

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use std as issue_45829_b;
99
= note: `issue_45829_b` 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 std as issue_45829_b;
13-
LL + use std as other_issue_45829_b;
14-
|
12+
LL | use std as other_issue_45829_b as issue_45829_b;
13+
| ++++++++++++++++++++++
1514

1615
error: aborting due to 1 previous error
1716

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use foo::{A, bar::B as A};
99
= note: `A` 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 foo::{A, bar::B as A};
13-
LL + use foo::{A, bar::B as OtherA};
14-
|
12+
LL | use foo::{A, bar::B as OtherA as A};
13+
| +++++++++
1514

1615
error: aborting due to 1 previous error
1716

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use std::{collections::HashMap as A, sync::Arc as A};
99
= note: `A` 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 std::{collections::HashMap as A, sync::Arc as A};
13-
LL + use std::{collections::HashMap as A, sync::Arc as OtherA};
14-
|
12+
LL | use std::{collections::HashMap as A, sync::Arc as OtherA as A};
13+
| +++++++++
1514

1615
error: aborting due to 1 previous error
1716

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | use std as core;
99
= note: `core` 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 std as core;
13-
LL + use std as other_core;
14-
|
12+
LL | use std as other_core as core;
13+
| +++++++++++++
1514

1615
error: aborting due to 1 previous error
1716

tests/ui/imports/issue-52891.stderr

+2-3
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

+2-3
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-import-vs-extern-crate.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ LL | use std::slice as std;
77
= note: `std` must be defined only once in the type namespace of this module
88
help: you can use `as` to change the binding name of the import
99
|
10-
LL - use std::slice as std;
11-
LL + use std::slice as other_std;
12-
|
10+
LL | use std::slice as other_std as std;
11+
| ++++++++++++
1312

1413
error: aborting due to 1 previous error
1514

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

+2-3
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

0 commit comments

Comments
 (0)