From 84a44218ad9dd0e278d934a72975dbad4f88c235 Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Sun, 3 May 2020 18:54:21 +0200 Subject: [PATCH 1/3] Suggest fixes for `use foo::self` --- src/librustc_resolve/build_reduced_graph.rs | 19 ++++++++- src/librustc_resolve/diagnostics.rs | 41 +++++++++++++++---- src/librustc_resolve/lib.rs | 2 +- src/test/ui/error-codes/E0429.stderr | 13 +++++- .../ui/issues/issue-45829/import-self.stderr | 13 +++++- src/test/ui/use/use-keyword.stderr | 2 +- src/test/ui/use/use-mod/use-mod-4.stderr | 26 ++++++++++-- 7 files changed, 97 insertions(+), 19 deletions(-) diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index d0eb1cfc222..35bba2e8b08 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -435,9 +435,24 @@ fn build_reduced_graph_for_use_tree( } else { // Disallow `self` if source.ident.name == kw::SelfLower { + let parent = module_path.last(); + + let span = match parent { + // only `::self` from `use foo::self as bar` + Some(seg) => seg.ident.span.shrink_to_hi().to(source.ident.span), + None => source.ident.span, + }; + let span_with_rename = match rename { + // only `self as bar` from `use foo::self as bar` + Some(rename) => source.ident.span.to(rename.span), + None => source.ident.span, + }; self.r.report_error( - use_tree.span, - ResolutionError::SelfImportsOnlyAllowedWithin, + span, + ResolutionError::SelfImportsOnlyAllowedWithin { + root: parent.is_none(), + span_with_rename, + }, ); } diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index c66e9a60406..e4f1d7a123d 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -301,13 +301,40 @@ impl<'a> Resolver<'a> { } err } - ResolutionError::SelfImportsOnlyAllowedWithin => struct_span_err!( - self.session, - span, - E0429, - "{}", - "`self` imports are only allowed within a { } list" - ), + ResolutionError::SelfImportsOnlyAllowedWithin { root, span_with_rename } => { + let mut err = struct_span_err!( + self.session, + span, + E0429, + "{}", + "`self` imports are only allowed within a { } list" + ); + + // None of the suggestions below would help with a case like `use self`. + if !root { + // use foo::bar::self -> foo::bar + // use foo::bar::self as abc -> foo::bar as abc + err.span_suggestion( + span, + "Remove `::self`..", + "".to_string(), + Applicability::MachineApplicable, + ); + + // use foo::bar::self -> foo::bar::{self} + // use foo::bar::self as abc -> foo::bar::{self as abc} + let braces = vec![ + (span_with_rename.shrink_to_lo(), "{".to_string()), + (span_with_rename.shrink_to_hi(), "}".to_string()), + ]; + err.multipart_suggestion( + "..or add braces around `self`", + braces, + Applicability::MachineApplicable, + ); + } + err + } ResolutionError::SelfImportCanOnlyAppearOnceInTheList => { let mut err = struct_span_err!( self.session, diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 2031b7868c0..bfb7f081fc3 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -194,7 +194,7 @@ enum ResolutionError<'a> { /// Error E0426: use of undeclared label. UndeclaredLabel(&'a str, Option), /// Error E0429: `self` imports are only allowed within a `{ }` list. - SelfImportsOnlyAllowedWithin, + SelfImportsOnlyAllowedWithin { root: bool, span_with_rename: Span }, /// Error E0430: `self` import can only appear once in the list. SelfImportCanOnlyAppearOnceInTheList, /// Error E0431: `self` import can only appear in an import list with a non-empty prefix. diff --git a/src/test/ui/error-codes/E0429.stderr b/src/test/ui/error-codes/E0429.stderr index b5f76a1fcd8..457782583a5 100644 --- a/src/test/ui/error-codes/E0429.stderr +++ b/src/test/ui/error-codes/E0429.stderr @@ -1,8 +1,17 @@ error[E0429]: `self` imports are only allowed within a { } list - --> $DIR/E0429.rs:1:5 + --> $DIR/E0429.rs:1:13 | LL | use std::fmt::self; - | ^^^^^^^^^^^^^^ + | ^^^^^^ + | +help: Remove `::self`.. + | +LL | use std::fmt; + | -- +help: ..or add braces around `self` + | +LL | use std::fmt::{self}; + | ^ ^ error: aborting due to previous error diff --git a/src/test/ui/issues/issue-45829/import-self.stderr b/src/test/ui/issues/issue-45829/import-self.stderr index 39522cd8183..9d9a785387f 100644 --- a/src/test/ui/issues/issue-45829/import-self.stderr +++ b/src/test/ui/issues/issue-45829/import-self.stderr @@ -5,10 +5,19 @@ LL | use foo as self; | ^^^^ expected identifier, found keyword error[E0429]: `self` imports are only allowed within a { } list - --> $DIR/import-self.rs:12:5 + --> $DIR/import-self.rs:12:8 | LL | use foo::self; - | ^^^^^^^^^ + | ^^^^^^ + | +help: Remove `::self`.. + | +LL | use foo; + | -- +help: ..or add braces around `self` + | +LL | use foo::{self}; + | ^ ^ error[E0255]: the name `foo` is defined multiple times --> $DIR/import-self.rs:6:11 diff --git a/src/test/ui/use/use-keyword.stderr b/src/test/ui/use/use-keyword.stderr index 62b6a77fbfb..501d14be521 100644 --- a/src/test/ui/use/use-keyword.stderr +++ b/src/test/ui/use/use-keyword.stderr @@ -2,7 +2,7 @@ error[E0429]: `self` imports are only allowed within a { } list --> $DIR/use-keyword.rs:6:13 | LL | use self as A; - | ^^^^^^^^^ + | ^^^^ error[E0432]: unresolved import `super` --> $DIR/use-keyword.rs:8:13 diff --git a/src/test/ui/use/use-mod/use-mod-4.stderr b/src/test/ui/use/use-mod/use-mod-4.stderr index e30e5c3ceb1..9d853296798 100644 --- a/src/test/ui/use/use-mod/use-mod-4.stderr +++ b/src/test/ui/use/use-mod/use-mod-4.stderr @@ -1,14 +1,32 @@ error[E0429]: `self` imports are only allowed within a { } list - --> $DIR/use-mod-4.rs:1:5 + --> $DIR/use-mod-4.rs:1:8 | LL | use foo::self; - | ^^^^^^^^^ + | ^^^^^^ + | +help: Remove `::self`.. + | +LL | use foo; + | -- +help: ..or add braces around `self` + | +LL | use foo::{self}; + | ^ ^ error[E0429]: `self` imports are only allowed within a { } list - --> $DIR/use-mod-4.rs:4:5 + --> $DIR/use-mod-4.rs:4:13 | LL | use std::mem::self; - | ^^^^^^^^^^^^^^ + | ^^^^^^ + | +help: Remove `::self`.. + | +LL | use std::mem; + | -- +help: ..or add braces around `self` + | +LL | use std::mem::{self}; + | ^ ^ error[E0432]: unresolved import `foo` --> $DIR/use-mod-4.rs:1:5 From d190e10f74afbe7edb5755bb0e0825450ea6c3bb Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Sun, 3 May 2020 18:56:52 +0200 Subject: [PATCH 2/3] Add error recovery for `use foo::self` --- src/librustc_resolve/build_reduced_graph.rs | 10 +++++++++- src/test/ui/issues/issue-45829/import-self.rs | 2 +- .../ui/issues/issue-45829/import-self.stderr | 17 ++++++++++++++++- src/test/ui/use/use-mod/use-mod-4.stderr | 2 +- src/test/ui/use/use-mod/use-mod-5.rs | 13 +++++++++++++ src/test/ui/use/use-mod/use-mod-5.stderr | 18 ++++++++++++++++++ src/test/ui/use/use-mod/use-mod-6.rs | 13 +++++++++++++ src/test/ui/use/use-mod/use-mod-6.stderr | 18 ++++++++++++++++++ 8 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/use/use-mod/use-mod-5.rs create mode 100644 src/test/ui/use/use-mod/use-mod-5.stderr create mode 100644 src/test/ui/use/use-mod/use-mod-6.rs create mode 100644 src/test/ui/use/use-mod/use-mod-6.stderr diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 35bba2e8b08..988ec3d4374 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -426,7 +426,7 @@ fn build_reduced_graph_for_use_tree( 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 @@ fn build_reduced_graph_for_use_tree( 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;` diff --git a/src/test/ui/issues/issue-45829/import-self.rs b/src/test/ui/issues/issue-45829/import-self.rs index 6cb18e1cdb7..2dc4331ced7 100644 --- a/src/test/ui/issues/issue-45829/import-self.rs +++ b/src/test/ui/issues/issue-45829/import-self.rs @@ -9,7 +9,7 @@ mod foo { 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; diff --git a/src/test/ui/issues/issue-45829/import-self.stderr b/src/test/ui/issues/issue-45829/import-self.stderr index 9d9a785387f..2ae2713b004 100644 --- a/src/test/ui/issues/issue-45829/import-self.stderr +++ b/src/test/ui/issues/issue-45829/import-self.stderr @@ -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`. diff --git a/src/test/ui/use/use-mod/use-mod-4.stderr b/src/test/ui/use/use-mod/use-mod-4.stderr index 9d853296798..e01c2cbf732 100644 --- a/src/test/ui/use/use-mod/use-mod-4.stderr +++ b/src/test/ui/use/use-mod/use-mod-4.stderr @@ -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 diff --git a/src/test/ui/use/use-mod/use-mod-5.rs b/src/test/ui/use/use-mod/use-mod-5.rs new file mode 100644 index 00000000000..df5b423ec57 --- /dev/null +++ b/src/test/ui/use/use-mod/use-mod-5.rs @@ -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(); +} diff --git a/src/test/ui/use/use-mod/use-mod-5.stderr b/src/test/ui/use/use-mod/use-mod-5.stderr new file mode 100644 index 00000000000..859547fbc4d --- /dev/null +++ b/src/test/ui/use/use-mod/use-mod-5.stderr @@ -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`. diff --git a/src/test/ui/use/use-mod/use-mod-6.rs b/src/test/ui/use/use-mod/use-mod-6.rs new file mode 100644 index 00000000000..1f8777daca4 --- /dev/null +++ b/src/test/ui/use/use-mod/use-mod-6.rs @@ -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(); +} diff --git a/src/test/ui/use/use-mod/use-mod-6.stderr b/src/test/ui/use/use-mod/use-mod-6.stderr new file mode 100644 index 00000000000..0042325229a --- /dev/null +++ b/src/test/ui/use/use-mod/use-mod-6.stderr @@ -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`. From aaeea7ffc36cd705116bf45f0479e51788ae33c5 Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Tue, 19 May 2020 22:12:41 +0200 Subject: [PATCH 3/3] Alter wording for `use foo::self` help --- src/librustc_resolve/diagnostics.rs | 4 ++-- src/test/ui/error-codes/E0429.stderr | 4 ++-- src/test/ui/issues/issue-45829/import-self.stderr | 4 ++-- src/test/ui/use/use-mod/use-mod-4.stderr | 8 ++++---- src/test/ui/use/use-mod/use-mod-5.stderr | 4 ++-- src/test/ui/use/use-mod/use-mod-6.stderr | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index e4f1d7a123d..ea237f1a04f 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -316,7 +316,7 @@ impl<'a> Resolver<'a> { // use foo::bar::self as abc -> foo::bar as abc err.span_suggestion( span, - "Remove `::self`..", + "consider importing the module directly", "".to_string(), Applicability::MachineApplicable, ); @@ -328,7 +328,7 @@ impl<'a> Resolver<'a> { (span_with_rename.shrink_to_hi(), "}".to_string()), ]; err.multipart_suggestion( - "..or add braces around `self`", + "alternatively, use the multi-path `use` syntax to import `self`", braces, Applicability::MachineApplicable, ); diff --git a/src/test/ui/error-codes/E0429.stderr b/src/test/ui/error-codes/E0429.stderr index 457782583a5..c598803fa6c 100644 --- a/src/test/ui/error-codes/E0429.stderr +++ b/src/test/ui/error-codes/E0429.stderr @@ -4,11 +4,11 @@ error[E0429]: `self` imports are only allowed within a { } list LL | use std::fmt::self; | ^^^^^^ | -help: Remove `::self`.. +help: consider importing the module directly | LL | use std::fmt; | -- -help: ..or add braces around `self` +help: alternatively, use the multi-path `use` syntax to import `self` | LL | use std::fmt::{self}; | ^ ^ diff --git a/src/test/ui/issues/issue-45829/import-self.stderr b/src/test/ui/issues/issue-45829/import-self.stderr index 2ae2713b004..158e81cdd96 100644 --- a/src/test/ui/issues/issue-45829/import-self.stderr +++ b/src/test/ui/issues/issue-45829/import-self.stderr @@ -10,11 +10,11 @@ error[E0429]: `self` imports are only allowed within a { } list LL | use foo::self; | ^^^^^^ | -help: Remove `::self`.. +help: consider importing the module directly | LL | use foo; | -- -help: ..or add braces around `self` +help: alternatively, use the multi-path `use` syntax to import `self` | LL | use foo::{self}; | ^ ^ diff --git a/src/test/ui/use/use-mod/use-mod-4.stderr b/src/test/ui/use/use-mod/use-mod-4.stderr index e01c2cbf732..a29bd07ac44 100644 --- a/src/test/ui/use/use-mod/use-mod-4.stderr +++ b/src/test/ui/use/use-mod/use-mod-4.stderr @@ -4,11 +4,11 @@ error[E0429]: `self` imports are only allowed within a { } list LL | use foo::self; | ^^^^^^ | -help: Remove `::self`.. +help: consider importing the module directly | LL | use foo; | -- -help: ..or add braces around `self` +help: alternatively, use the multi-path `use` syntax to import `self` | LL | use foo::{self}; | ^ ^ @@ -19,11 +19,11 @@ error[E0429]: `self` imports are only allowed within a { } list LL | use std::mem::self; | ^^^^^^ | -help: Remove `::self`.. +help: consider importing the module directly | LL | use std::mem; | -- -help: ..or add braces around `self` +help: alternatively, use the multi-path `use` syntax to import `self` | LL | use std::mem::{self}; | ^ ^ diff --git a/src/test/ui/use/use-mod/use-mod-5.stderr b/src/test/ui/use/use-mod/use-mod-5.stderr index 859547fbc4d..ebb71c51293 100644 --- a/src/test/ui/use/use-mod/use-mod-5.stderr +++ b/src/test/ui/use/use-mod/use-mod-5.stderr @@ -4,11 +4,11 @@ error[E0429]: `self` imports are only allowed within a { } list LL | use foo::bar::self; | ^^^^^^ | -help: Remove `::self`.. +help: consider importing the module directly | LL | use foo::bar; | -- -help: ..or add braces around `self` +help: alternatively, use the multi-path `use` syntax to import `self` | LL | use foo::bar::{self}; | ^ ^ diff --git a/src/test/ui/use/use-mod/use-mod-6.stderr b/src/test/ui/use/use-mod/use-mod-6.stderr index 0042325229a..36fdf9c75c7 100644 --- a/src/test/ui/use/use-mod/use-mod-6.stderr +++ b/src/test/ui/use/use-mod/use-mod-6.stderr @@ -4,11 +4,11 @@ error[E0429]: `self` imports are only allowed within a { } list LL | use foo::bar::self as abc; | ^^^^^^ | -help: Remove `::self`.. +help: consider importing the module directly | LL | use foo::bar as abc; | -- -help: ..or add braces around `self` +help: alternatively, use the multi-path `use` syntax to import `self` | LL | use foo::bar::{self as abc}; | ^ ^