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

Suggest use self::Thing::OtherThing when using use Thing::OtherThing #34191

Closed
jonas-schievink opened this issue Jun 9, 2016 · 3 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@jonas-schievink
Copy link
Contributor

This frequently happens for code like this:

mod m {
    enum MyEnum {
        MyVariant
    }

    // Import all variants:
    use MyEnum::*;
}

fn main() {}

Since this will compile when putting ms contents into the main module, this can be quite confusing (for example when refactoring some code into it's own module).

Currently, this yields error: unresolved import MyEnum::*. Maybe a missing extern crate MyEnum?. When this case is detected, the Maybe a missing extern crate MyEnum? probably shouldn't be printed.

@jonas-schievink jonas-schievink changed the title Suggest use self::Thing when using use Thing::OtherThing Suggest use self::Thing::OtherThing when using use Thing::OtherThing Jun 9, 2016
@jseyfried
Copy link
Contributor

cc me

@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 10, 2016
@euclio
Copy link
Contributor

euclio commented Aug 24, 2016

I'd like to work on this.

I'm thinking: if we detect that adding "self" to the import resolves it, then a help message suggesting that is added. Otherwise, we add a help message that suggests extern crate. Does that make sense?

@jseyfried
Copy link
Contributor

jseyfried commented Aug 24, 2016

@euclio Yeah, that makes sense -- I think that's a good idea.

I would check whether prefixing self fixes the import in finalize_import (resolve_imports.rs).
If resolve_module_path fails, try resolve_module_path_from_root with a root of self.current_module (EDIT: nevermind).
If that succeeds, prefixing self would fix it and you can return Failed(Some(span, "Try prepending self::")).

bors added a commit that referenced this issue Sep 7, 2016
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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

4 participants