1
Fork 0

Add error recovery for use foo::self

This commit is contained in:
mibac138 2020-05-03 18:56:52 +02:00
parent 84a44218ad
commit d190e10f74
8 changed files with 89 additions and 4 deletions

View file

@ -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;
@ -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;`

View file

@ -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;

View file

@ -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
|
@ -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`.

View file

@ -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

View file

@ -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();
}

View file

@ -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`.

View file

@ -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();
}

View file

@ -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`.