Skip to content

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

Description

@durka

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.A-new-lintArea: new lintC-enhancementCategory: enhancementS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions