"mod lib" in lib.rs produces incorrect error message
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;
| ^^^
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
libwhile 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
- Parse lib.rs (in "crate-root mode")
cc #34157
Jeffrey Seyfried at 2017-02-08 07:30:08
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.rsis being parsed.)Alex Burka at 2018-06-29 19:52:33
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.21sAlex Burka at 2018-06-29 19:57:29