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

warn on mod lib; in main.rs, etc #4273

Open
durka opened this issue Jul 12, 2017 · 2 comments
Open

warn on mod lib; in main.rs, etc #4273

durka opened this issue Jul 12, 2017 · 2 comments
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug S-triage Status: This issue is waiting on initial triage.

Comments

@durka
Copy link
Contributor

durka commented Jul 12, 2017

When a crate imports a module that cargo is also compiling as its own crate root, things can get hairy. The UX can be pretty terrible as in this case:

// src/lib.rs
lazy_static! { static ref FOO: i32 = 42; }
// src/main.rs
#[macro_use] extern crate lazy_static;

mod lib;

fn main() {}
error: cannot find macro `lazy_static!` in this scope
 --> src/lib.rs:1:1
  |
1 | lazy_static! { static ref FOO: i32 = 42; }
  | ^^^^^^^^^^^

OK, the error says I need to import lazy_static into lib.rs. Let's do that:

// src/lib.rs
#[macro_use] extern crate lazy_static;
lazy_static! { static ref FOO: i32 = 42; }
warning: static item is never used: `FOO`
 --> src/lib.rs:2:1
  |
2 | lazy_static! { static ref FOO: i32 = 42; }
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default
  = note: this error originates in a macro outside of the current crate

error[E0468]: an `extern crate` loading macros must be at the crate root
 --> src/lib.rs:1:14
  |
1 | #[macro_use] extern crate lazy_static;
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^

WTF! I left the crate out, got an error, I put it in, got an error saying to take it back out. Rust is such a crazy language, OMG!

Of course the second error comes from a totally separate rustc run, but there's next to no indication of that. But it seems like we should be able to detect this situation and tell the user what's going on.

@matklad
Copy link
Member

matklad commented Jul 12, 2017

Off-hand, I don't know how we can warn about this: looks like this needs information from both Cargo and the compiler, and as such lies in the realm of IDEs / RLS (We certainly can add this warning to IntelliJ Rust, because we have all the data).

However, I think this is better be fixed somehow: just the other day, the student I am mentoring faced this same issue (they were a novice rust programmer, so they've written mod lib; instead of extern crate miniz_oxide), and it took me like half an hour to understand what's going on here :(

@carols10cents carols10cents added A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug labels Aug 28, 2017
@dwijnand dwijnand reopened this Dec 22, 2018
@dwijnand dwijnand added the E-hard Experience: Hard label Dec 22, 2018
@dwijnand
Copy link
Member

Looks like it's unknown how to fix this and might require cross Cargo and compiler changes, so labelling it "E-hard". (I also accidentally closed it.)

@epage epage added S-triage Status: This issue is waiting on initial triage. and removed E-hard Experience: Hard labels Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

5 participants