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

Better "unresolved import" error message and "self::" #13577

Closed
bachm opened this issue Apr 17, 2014 · 4 comments
Closed

Better "unresolved import" error message and "self::" #13577

bachm opened this issue Apr 17, 2014 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name/path resolution done by `rustc_resolve` specifically

Comments

@bachm
Copy link

bachm commented Apr 17, 2014

Importing names sometimes requires use self:: as shown here:

main.rs

use foo::Foo;
mod foo;

fn main() {}

foo.rs

use bar::Bar; //looks fine at first glance but is actually an error
mod bar;

pub struct Foo {
    bar: Bar
}

bar.rs

pub struct Bar;

Someone new to Rust will almost certainly make this mistake, and then see the following error message:

foo.rs:1:5: 1:8 error: unresolved import. maybe a missing `extern crate bar`?
foo.rs:1 use bar::Bar;
             ^~~
foo.rs:1:5: 1:13 error: failed to resolve import `bar::Bar`
foo.rs:1 use bar::Bar;
             ^~~~~~~~

Personally I found this was the main source of frustration while learning Rust's module system because of my ingrained thinking in absolute paths.

I think two changes would be helpful here:

The maybe a missing extern crate bar hint should only be given when appropriate (ie. when mod bar; cannot be resolved).

There should be a hint to maybe use self:: when use bar::Bar; cannot be resolved and the current module is not the crate root.

@steveklabnik
Copy link
Member

Triage: no change, these errors are the same today

@ketsuban
Copy link
Contributor

ketsuban commented Apr 9, 2016

Triage: I get a different error now, which is a little more helpful.

src/foo.rs:2:5: 2:8 error: cannot declare a new module at this location
src/foo.rs:2 mod bar;
                 ^~~
src/foo.rs:2:5: 2:8 note: maybe move this module `foo` to its own directory via `foo/mod.rs`
src/foo.rs:2 mod bar;
                 ^~~
src/foo.rs:2:5: 2:8 note: ... or maybe `use` the module `bar` instead of possibly redeclaring it
src/foo.rs:2 mod bar;
                 ^~~

However, taking the first suggested course of action and moving src/{foo, bar}.rs to src/foo/{mod, bar}.rs gets me the unhelpful extern crate suggestion again, so the report stands.

@birkenfeld
Copy link
Contributor

At least the long diagostics for E0432 now reference self:: and super::: #33320

@jseyfried
Copy link
Contributor

Fixed in #36289.

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 A-resolve Area: Name/path resolution done by `rustc_resolve` specifically
Projects
None yet
Development

No branches or pull requests

7 participants