Skip to content

Commit d190e10

Browse files
committed
Add error recovery for use foo::self
1 parent 84a4421 commit d190e10

File tree

8 files changed

+89
-4
lines changed

8 files changed

+89
-4
lines changed

src/librustc_resolve/build_reduced_graph.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
426426
return;
427427
}
428428

429-
// Replace `use foo::self;` with `use foo;`
429+
// Replace `use foo::{ self };` with `use foo;`
430430
source = module_path.pop().unwrap();
431431
if rename.is_none() {
432432
ident = source.ident;
@@ -454,6 +454,14 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
454454
span_with_rename,
455455
},
456456
);
457+
458+
// Error recovery: replace `use foo::self;` with `use foo;`
459+
if let Some(parent) = module_path.pop() {
460+
source = parent;
461+
if rename.is_none() {
462+
ident = source.ident;
463+
}
464+
}
457465
}
458466

459467
// Disallow `use $crate;`

src/test/ui/issues/issue-45829/import-self.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use foo::{self};
99
use foo as self;
1010
//~^ ERROR expected identifier
1111

12-
use foo::self;
12+
use foo::self; //~ ERROR is defined multiple times
1313
//~^ ERROR `self` imports are only allowed within a { } list
1414

1515
use foo::A;

src/test/ui/issues/issue-45829/import-self.stderr

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ help: you can use `as` to change the binding name of the import
3434
LL | use foo::{self as other_foo};
3535
| ^^^^^^^^^^^^^^^^^
3636

37+
error[E0255]: the name `foo` is defined multiple times
38+
--> $DIR/import-self.rs:12:5
39+
|
40+
LL | mod foo {
41+
| ------- previous definition of the module `foo` here
42+
...
43+
LL | use foo::self;
44+
| ^^^^^^^^^ `foo` reimported here
45+
|
46+
= note: `foo` must be defined only once in the type namespace of this module
47+
help: you can use `as` to change the binding name of the import
48+
|
49+
LL | use foo as other_foo;
50+
| ^^^^^^^^^^^^^^^^
51+
3752
error[E0252]: the name `A` is defined multiple times
3853
--> $DIR/import-self.rs:16:11
3954
|
@@ -48,7 +63,7 @@ help: you can use `as` to change the binding name of the import
4863
LL | use foo::{self as OtherA};
4964
| ^^^^^^^^^^^^^^
5065

51-
error: aborting due to 4 previous errors
66+
error: aborting due to 5 previous errors
5267

5368
Some errors have detailed explanations: E0252, E0255, E0429.
5469
For more information about an error, try `rustc --explain E0252`.

src/test/ui/use/use-mod/use-mod-4.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ error[E0432]: unresolved import `foo`
3232
--> $DIR/use-mod-4.rs:1:5
3333
|
3434
LL | use foo::self;
35-
| ^^^ maybe a missing crate `foo`?
35+
| ^^^^^^^^^ no `foo` in the root
3636

3737
error: aborting due to 3 previous errors
3838

src/test/ui/use/use-mod/use-mod-5.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mod foo {
2+
pub mod bar {
3+
pub fn drop() {}
4+
}
5+
}
6+
7+
use foo::bar::self;
8+
//~^ ERROR `self` imports are only allowed within a { } list
9+
10+
fn main() {
11+
// Because of error recovery this shouldn't error
12+
bar::drop();
13+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0429]: `self` imports are only allowed within a { } list
2+
--> $DIR/use-mod-5.rs:7:13
3+
|
4+
LL | use foo::bar::self;
5+
| ^^^^^^
6+
|
7+
help: Remove `::self`..
8+
|
9+
LL | use foo::bar;
10+
| --
11+
help: ..or add braces around `self`
12+
|
13+
LL | use foo::bar::{self};
14+
| ^ ^
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0429`.

src/test/ui/use/use-mod/use-mod-6.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mod foo {
2+
pub mod bar {
3+
pub fn drop() {}
4+
}
5+
}
6+
7+
use foo::bar::self as abc;
8+
//~^ ERROR `self` imports are only allowed within a { } list
9+
10+
fn main() {
11+
// Because of error recovery this shouldn't error
12+
abc::drop();
13+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0429]: `self` imports are only allowed within a { } list
2+
--> $DIR/use-mod-6.rs:7:13
3+
|
4+
LL | use foo::bar::self as abc;
5+
| ^^^^^^
6+
|
7+
help: Remove `::self`..
8+
|
9+
LL | use foo::bar as abc;
10+
| --
11+
help: ..or add braces around `self`
12+
|
13+
LL | use foo::bar::{self as abc};
14+
| ^ ^
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0429`.

0 commit comments

Comments
 (0)