Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
Riateche opened this issue Aug 30, 2016 · 4 comments
Open

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

Riateche opened this issue Aug 30, 2016 · 4 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Riateche
Copy link

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;
  |     ^^^
@durka
Copy link
Contributor

durka commented Aug 30, 2016

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!

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 30, 2016
@jseyfried jseyfried self-assigned this Dec 23, 2016
@jseyfried jseyfried removed their assignment Feb 8, 2017
@jseyfried jseyfried added the A-parser Area: The parsing of Rust source code to an AST label Feb 8, 2017
@jseyfried jseyfried self-assigned this Feb 8, 2017
@jseyfried
Copy link
Contributor

cc #34157

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 26, 2017
@durka
Copy link
Contributor

durka commented Jun 29, 2018

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.)

@durka
Copy link
Contributor

durka commented Jun 29, 2018

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

@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants