"mod lib" in lib.rs produces incorrect error message

0943a09
Opened by Pavel Strakhov at 2020-06-11 18:02:19

lib.rs:

mod a1;
mod lib;

a1.rs - empty file.

Error:

error: cannot declare a new module at this location
 --> src/lib.rs:1:5
  |
1 | mod a1;
  |     ^^
  |
note: maybe move this module `lib` to its own directory via `lib/mod.rs`
 --> src/lib.rs:1:5
  |
1 | mod a1;
  |     ^^
note: ... or maybe `use` the module `a1` instead of possibly redeclaring it
 --> src/lib.rs:1:5
  |
1 | mod a1;
  |     ^^

Error location and some of the messages incorrectly refer to a1 module instead of lib. Module a1 itself is declared correctly. If I write lib before a1, the error starts making more sense:

lib.rs:

mod lib;
mod a1;

Error:

error: cannot declare a new module at this location
 --> src/lib.rs:1:5
  |
1 | mod lib;
  |     ^^^
  |
note: maybe move this module `lib` to its own directory via `lib/mod.rs`
 --> src/lib.rs:1:5
  |
1 | mod lib;
  |     ^^^
note: ... or maybe `use` the module `lib` instead of possibly redeclaring it
 --> src/lib.rs:1:5
  |
1 | mod lib;
  |     ^^^
  1. I think what happens in the first case is:

    • Parse lib.rs (in "crate-root mode")
      • mod a1;
        • ✓ can declare modules at crate root
        • parse a1.rs (in "non-crate-root mode")
          • ✓ a1.rs contains a valid module
      • mod lib;
        • ✓ can declare modules at crate root
        • parse lib.rs (in "non-crate-root mode")
          • mod a1;
            • ✗ cannot declare module

    The fix should be to disallow a module named lib while parsing lib.rs (viz. main).

    As an aside, there are way too many weasel words in those notes!

    Alex Burka at 2016-08-30 21:34:53

  2. cc #34157

    Jeffrey Seyfried at 2017-02-08 07:30:08

  3. Since the "directory ownership" requirement was removed, the new error message is:

    error[E0583]: file not found for module `a1`
     --> src/lib.rs:1:5
      |
    1 | mod a1;
      |     ^^
      |
      = help: name the file either lib/a1.rs or lib/a1/mod.rs inside the directory "src"
    

    (This bug remains relevant because the error message comes from the second time lib.rs is being parsed.)

    Alex Burka at 2018-06-29 19:52:33

  4. Indeed, adding #![feature(non_modrs_mods)] allows this to successfully compile on nightly:

    src/lib.rs (all other files are empty)

    #![feature(non_modrs_mods)]
    mod a1;
    mod lib;
    
    $ tree src
    src
    ├── a1.rs
    ├── lib
    │   ├── a1.rs
    │   └── lib.rs
    └── lib.rs
    $ cargo +nightly build
       Compiling asdf v0.1.0 (file:///Users/alex/Programming/rust/asdf)
    warning: unused attribute
     --> src/lib.rs:1:1
      |
    1 | #![feature(non_modrs_mods)]
      | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |
      = note: #[warn(unused_attributes)] on by default
    
    warning: crate-level attribute should be in the root module
     --> src/lib.rs:1:1
      |
    1 | #![feature(non_modrs_mods)]
      | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
        Finished dev [unoptimized + debuginfo] target(s) in 0.21s
    

    Alex Burka at 2018-06-29 19:57:29