Skip to content

Commit

Permalink
Auto merge of #36289 - euclio:self-suggestion, r=jseyfried
Browse files Browse the repository at this point in the history
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.

Fixes #34191.

Thank you for the help @jseyfried!
  • Loading branch information
bors authored Sep 7, 2016
2 parents 2819eca + 288e7ca commit f707582
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,19 @@ 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.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 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)
};
},
};

let (name, value_result, type_result) = match directive.subclass {
Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/unresolved-import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,23 @@ mod food {
}
}
}

mod m {
enum MyEnum {
MyVariant
}

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() {}
}

0 comments on commit f707582

Please sign in to comment.