Skip to content

Commit 137ffa1

Browse files
committed
Improve robustness of nested check.
This commit removes the assumption that the start of a use statement will always be on one line with a single space - which was silly in the first place.
1 parent 7c95540 commit 137ffa1

File tree

4 files changed

+71
-28
lines changed

4 files changed

+71
-28
lines changed

src/librustc_resolve/error_reporting.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -827,28 +827,29 @@ fn find_span_immediately_after_crate_name(
827827
module_name, use_span);
828828
let source_map = sess.source_map();
829829

830-
// Get position of the first `{` character for the use statement.
831-
// ie. `use foo::{a, b::{c, d}};`
832-
// ^
833-
let pos_of_use_tree_left_bracket = source_map.span_until_char(use_span, '{').hi();
834-
debug!("find_span_immediately_after_crate_name: pos_of_use_tree_left_bracket={:?}",
835-
pos_of_use_tree_left_bracket);
836-
837-
// Calculate the expected difference between the first `{` character and the start of a
838-
// use statement.
839-
// ie. `use foo::{..};`
840-
// ^^^^
841-
// | ^^^
842-
// 4 | ^^
843-
// 3 |
844-
// 2
845-
let expected_difference = BytePos((module_name.as_str().len() + 4 + 2) as u32);
846-
debug!("find_span_immediately_after_crate_name: expected_difference={:?}",
847-
expected_difference);
848-
let actual_difference = pos_of_use_tree_left_bracket - use_span.lo();
849-
debug!("find_span_immediately_after_crate_name: actual_difference={:?}",
850-
actual_difference);
851-
852-
(expected_difference == actual_difference,
853-
use_span.with_lo(use_span.lo() + expected_difference))
830+
// Using `use issue_59764::foo::{baz, makro};` as an example throughout..
831+
let mut num_colons = 0;
832+
// Find second colon.. `use issue_59764:`
833+
let until_second_colon = source_map.span_take_while(use_span, |c| {
834+
if *c == ':' { num_colons += 1; }
835+
match c {
836+
':' if num_colons == 2 => false,
837+
_ => true,
838+
}
839+
});
840+
// Find everything after the second colon.. `foo::{baz, makro};`
841+
let from_second_colon = use_span.with_lo(until_second_colon.hi() + BytePos(1));
842+
843+
let mut found_a_non_whitespace_character = false;
844+
// Find the first non-whitespace character in `from_second_colon`.. `f`
845+
let after_second_colon = source_map.span_take_while(from_second_colon, |c| {
846+
if found_a_non_whitespace_character { return false; }
847+
if !c.is_whitespace() { found_a_non_whitespace_character = true; }
848+
true
849+
});
850+
851+
// Find the first `{` in from_second_colon.. `foo::{`
852+
let next_left_bracket = source_map.span_through_char(from_second_colon, '{');
853+
854+
(next_left_bracket == after_second_colon, from_second_colon)
854855
}

src/test/ui/issue-59764.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ mod renamed_multiple_imports {
109109
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]
110110
}
111111

112+
mod lots_of_whitespace {
113+
use issue_59764::{makro as foobar,
114+
115+
foobaz,
116+
117+
118+
foo::{baz} //~ ERROR unresolved import `issue_59764::foo::makro` [E0432]
119+
120+
};
121+
}
122+
112123
// Simple case..
113124

114125
use issue_59764::makro;

src/test/ui/issue-59764.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ mod renamed_multiple_imports {
109109
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]
110110
}
111111

112+
mod lots_of_whitespace {
113+
use
114+
issue_59764::{
115+
116+
foobaz,
117+
118+
119+
foo::{baz,
120+
121+
makro as foobar} //~ ERROR unresolved import `issue_59764::foo::makro` [E0432]
122+
123+
};
124+
}
125+
112126
// Simple case..
113127

114128
use issue_59764::foo::makro;

src/test/ui/issue-59764.stderr

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,24 @@ LL | use issue_59764::{makro as foobar, foo::{baz}};
196196
|
197197

198198
error[E0432]: unresolved import `issue_59764::foo::makro`
199-
--> $DIR/issue-59764.rs:114:5
199+
--> $DIR/issue-59764.rs:121:17
200+
|
201+
LL | makro as foobar}
202+
| ^^^^^^^^^^^^^^^ no `makro` in `foo`
203+
|
204+
= note: this could be because a macro annotated with `#[macro_export]` will be exported at the root of the crate instead of the module where it is defined
205+
help: a macro with this name exists at the root of the crate
206+
|
207+
LL | use issue_59764::{makro as foobar,
208+
LL |
209+
LL | foobaz,
210+
LL |
211+
LL |
212+
LL | foo::{baz}
213+
...
214+
215+
error[E0432]: unresolved import `issue_59764::foo::makro`
216+
--> $DIR/issue-59764.rs:128:5
200217
|
201218
LL | use issue_59764::foo::makro;
202219
| ^^^^^^^^^^^^^^^^^^^^^^^ no `makro` in `foo`
@@ -208,20 +225,20 @@ LL | use issue_59764::makro;
208225
| ^^^^^^^^^^^^^^^^^^
209226

210227
error: cannot determine resolution for the macro `makro`
211-
--> $DIR/issue-59764.rs:117:1
228+
--> $DIR/issue-59764.rs:131:1
212229
|
213230
LL | makro!(bar);
214231
| ^^^^^
215232
|
216233
= note: import resolution is stuck, try simplifying macro imports
217234

218235
error[E0425]: cannot find function `bar` in this scope
219-
--> $DIR/issue-59764.rs:121:5
236+
--> $DIR/issue-59764.rs:135:5
220237
|
221238
LL | bar();
222239
| ^^^ not found in this scope
223240

224-
error: aborting due to 17 previous errors
241+
error: aborting due to 18 previous errors
225242

226243
Some errors occurred: E0425, E0432.
227244
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)