Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: Suggest use self when import resolves #36289

Merged
merged 2 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

@jseyfried jseyfried Sep 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be let self_module = self.current_module.

However, #34191 (comment) isn't entirely correct (sorry). If we are in a block that has items in it, self.current_module is the anonymous module associated with the block, but self is the "normal" (mod) module that contains the block.

To get the self module, you can use self.module_map[&self.current_module.normal_ancestor_id.unwrap()] instead of just self.current_module (c.f. fn resolve_module_prefix in librustc_resolve/lib.rs).


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),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional nit: I think this match would look better as an if let (i.e. if let Success(_) = self_result { ... }).

},
};

let (name, value_result, type_result) = match directive.subclass {
Expand Down
9 changes: 9 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,12 @@ mod food {
}
}
}

mod m {
enum MyEnum {
MyVariant
}

use MyEnum::*; //~ ERROR unresolved import `MyEnum::*` [E0432]
//~^ Did you mean `self::MyEnum`?
}