Skip to content

Commit

Permalink
Add error recovery for use foo::self
Browse files Browse the repository at this point in the history
  • Loading branch information
mibac138 committed May 19, 2020
1 parent 84a4421 commit d190e10
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
return;
}

// Replace `use foo::self;` with `use foo;`
// Replace `use foo::{ self };` with `use foo;`
source = module_path.pop().unwrap();
if rename.is_none() {
ident = source.ident;
Expand Down Expand Up @@ -454,6 +454,14 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
span_with_rename,
},
);

// Error recovery: replace `use foo::self;` with `use foo;`
if let Some(parent) = module_path.pop() {
source = parent;
if rename.is_none() {
ident = source.ident;
}
}
}

// Disallow `use $crate;`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-45829/import-self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use foo::{self};
use foo as self;
//~^ ERROR expected identifier

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

use foo::A;
Expand Down
17 changes: 16 additions & 1 deletion src/test/ui/issues/issue-45829/import-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ help: you can use `as` to change the binding name of the import
LL | use foo::{self as other_foo};
| ^^^^^^^^^^^^^^^^^

error[E0255]: the name `foo` is defined multiple times
--> $DIR/import-self.rs:12:5
|
LL | mod foo {
| ------- previous definition of the module `foo` here
...
LL | use foo::self;
| ^^^^^^^^^ `foo` reimported here
|
= note: `foo` must be defined only once in the type namespace of this module
help: you can use `as` to change the binding name of the import
|
LL | use foo as other_foo;
| ^^^^^^^^^^^^^^^^

error[E0252]: the name `A` is defined multiple times
--> $DIR/import-self.rs:16:11
|
Expand All @@ -48,7 +63,7 @@ help: you can use `as` to change the binding name of the import
LL | use foo::{self as OtherA};
| ^^^^^^^^^^^^^^

error: aborting due to 4 previous errors
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0252, E0255, E0429.
For more information about an error, try `rustc --explain E0252`.
2 changes: 1 addition & 1 deletion src/test/ui/use/use-mod/use-mod-4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ error[E0432]: unresolved import `foo`
--> $DIR/use-mod-4.rs:1:5
|
LL | use foo::self;
| ^^^ maybe a missing crate `foo`?
| ^^^^^^^^^ no `foo` in the root

error: aborting due to 3 previous errors

Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/use/use-mod/use-mod-5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod foo {
pub mod bar {
pub fn drop() {}
}
}

use foo::bar::self;
//~^ ERROR `self` imports are only allowed within a { } list

fn main() {
// Because of error recovery this shouldn't error
bar::drop();
}
18 changes: 18 additions & 0 deletions src/test/ui/use/use-mod/use-mod-5.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0429]: `self` imports are only allowed within a { } list
--> $DIR/use-mod-5.rs:7:13
|
LL | use foo::bar::self;
| ^^^^^^
|
help: Remove `::self`..
|
LL | use foo::bar;
| --
help: ..or add braces around `self`
|
LL | use foo::bar::{self};
| ^ ^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0429`.
13 changes: 13 additions & 0 deletions src/test/ui/use/use-mod/use-mod-6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod foo {
pub mod bar {
pub fn drop() {}
}
}

use foo::bar::self as abc;
//~^ ERROR `self` imports are only allowed within a { } list

fn main() {
// Because of error recovery this shouldn't error
abc::drop();
}
18 changes: 18 additions & 0 deletions src/test/ui/use/use-mod/use-mod-6.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0429]: `self` imports are only allowed within a { } list
--> $DIR/use-mod-6.rs:7:13
|
LL | use foo::bar::self as abc;
| ^^^^^^
|
help: Remove `::self`..
|
LL | use foo::bar as abc;
| --
help: ..or add braces around `self`
|
LL | use foo::bar::{self as abc};
| ^ ^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0429`.

0 comments on commit d190e10

Please sign in to comment.