From 41ee2e9124245071dbc890b8617fd6c75391477f Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Mon, 5 Sep 2016 21:48:02 -0400 Subject: [PATCH 1/2] resolve: Suggest `use self` when import resolves Improves errors messages by replacing "Maybe a missing `extern crate`" messages with "Did you mean `self::...`" when the `self` import would succeed. --- src/librustc_resolve/resolve_imports.rs | 16 +++++++++++++++- src/test/compile-fail/unresolved-import.rs | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 875d6745f6b2e..622d705485efd 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -587,7 +587,21 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { let module = match module_result { Success(module) => module, Indeterminate => return Indeterminate, - Failed(err) => return Failed(err), + Failed(err) => { + let self_module = self.current_module.clone(); + + let resolve_from_self_result = self.resolve_module_path_from_root( + &self_module, &module_path, 0, Some(span)); + + return match resolve_from_self_result { + Success(_) => { + let msg = format!("Did you mean `self::{}`?", + &names_to_string(module_path)); + Failed(Some((span, msg))) + }, + _ => Failed(err), + }; + }, }; let (name, value_result, type_result) = match directive.subclass { diff --git a/src/test/compile-fail/unresolved-import.rs b/src/test/compile-fail/unresolved-import.rs index d1254f3f5241b..6421599cacb24 100644 --- a/src/test/compile-fail/unresolved-import.rs +++ b/src/test/compile-fail/unresolved-import.rs @@ -35,3 +35,12 @@ mod food { } } } + +mod m { + enum MyEnum { + MyVariant + } + + use MyEnum::*; //~ ERROR unresolved import `MyEnum::*` [E0432] + //~^ Did you mean `self::MyEnum`? +} From 288e7caf19b415007787f47424c9e00913cb7803 Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Mon, 5 Sep 2016 23:08:21 -0400 Subject: [PATCH 2/2] show `self` suggestion when items are in the block --- src/librustc_resolve/resolve_imports.rs | 14 ++++++-------- src/test/compile-fail/unresolved-import.rs | 11 +++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 622d705485efd..90c17866583a7 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -588,18 +588,16 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { Success(module) => module, Indeterminate => return Indeterminate, Failed(err) => { - let self_module = self.current_module.clone(); + let self_module = self.module_map[&self.current_module.normal_ancestor_id.unwrap()]; let resolve_from_self_result = self.resolve_module_path_from_root( &self_module, &module_path, 0, Some(span)); - return match resolve_from_self_result { - Success(_) => { - let msg = format!("Did you mean `self::{}`?", - &names_to_string(module_path)); - Failed(Some((span, msg))) - }, - _ => Failed(err), + return if let Success(_) = resolve_from_self_result { + let msg = format!("Did you mean `self::{}`?", &names_to_string(module_path)); + Failed(Some((span, msg))) + } else { + Failed(err) }; }, }; diff --git a/src/test/compile-fail/unresolved-import.rs b/src/test/compile-fail/unresolved-import.rs index 6421599cacb24..0a9a437569730 100644 --- a/src/test/compile-fail/unresolved-import.rs +++ b/src/test/compile-fail/unresolved-import.rs @@ -44,3 +44,14 @@ mod m { use MyEnum::*; //~ ERROR unresolved import `MyEnum::*` [E0432] //~^ Did you mean `self::MyEnum`? } + +mod items { + enum Enum { + Variant + } + + use Enum::*; //~ ERROR unresolved import `Enum::*` [E0432] + //~^ Did you mean `self::Enum`? + + fn item() {} +}