Skip to content

Commit 74748b1

Browse files
committed
WIP: don't suggest placing use statements into expanded code
1 parent f9a07bc commit 74748b1

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

src/librustc_resolve/lib.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
605605
ItemKind::Use(..) => {
606606
// don't suggest placing a use before the prelude
607607
// import or other generated ones
608-
if item.span == DUMMY_SP {
608+
if item.span.ctxt().outer().expn_info().is_none() {
609609
self.span = Some(item.span.with_hi(item.span.lo()));
610610
self.found_use = true;
611611
return;
@@ -615,7 +615,19 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
615615
ItemKind::ExternCrate(_) => {}
616616
// but place them before the first other item
617617
_ => if self.span.map_or(true, |span| item.span < span ) {
618-
self.span = Some(item.span.with_hi(item.span.lo()));
618+
if item.span.ctxt().outer().expn_info().is_none() {
619+
// don't insert between attributes and an item
620+
if item.attrs.is_empty() {
621+
self.span = Some(item.span.with_hi(item.span.lo()));
622+
} else {
623+
// find the first attribute on the item
624+
for attr in &item.attrs {
625+
if self.span.map_or(true, |span| attr.span < span) {
626+
self.span = Some(attr.span.with_hi(attr.span.lo()));
627+
}
628+
}
629+
}
630+
}
619631
},
620632
}
621633
}

src/test/ui/resolve/privacy-struct-ctor.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0423]: expected value, found struct `Z`
1010
|
1111
help: possible better candidate is found in another module, you can import it into scope
1212
|
13-
16 | use m::n::Z;
13+
22 | use m::n::Z;
1414
|
1515

1616
error[E0423]: expected value, found struct `S`
@@ -24,7 +24,7 @@ error[E0423]: expected value, found struct `S`
2424
|
2525
help: possible better candidate is found in another module, you can import it into scope
2626
|
27-
15 | use m::S;
27+
32 | use m::S;
2828
|
2929

3030
error[E0423]: expected value, found struct `xcrate::S`
@@ -38,7 +38,7 @@ error[E0423]: expected value, found struct `xcrate::S`
3838
|
3939
help: possible better candidate is found in another module, you can import it into scope
4040
|
41-
15 | use m::S;
41+
32 | use m::S;
4242
|
4343

4444
error[E0603]: tuple struct `Z` is private

src/test/ui/resolve/use_suggestion_placement.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ error[E0412]: cannot find type `Path` in this scope
66
|
77
help: possible candidate is found in another module, you can import it into scope
88
|
9-
20 | #[derive(use std::path::Path;
9+
21 | use std::path::Path;
1010
|
1111

1212
error[E0425]: cannot find value `A` in this scope

src/test/ui/span/issue-35987.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ error[E0404]: expected trait, found type parameter `Add`
66
|
77
help: possible better candidate is found in another module, you can import it into scope
88
|
9-
11 | use std::ops::Add;
9+
13 | use std::ops::Add;
1010
|
1111

1212
error[E0601]: main function not found

0 commit comments

Comments
 (0)