Skip to content

Modules loaded using #[path] are seemingly treated like mod.rs files even when they're not #104705

Open

Description

Assume the following file structure:

src
  bin
    foo.rs
  bar
    baz.rs
  bar.rs
  main.rs

where bar.rs has a mod baz; in it.

When doing

mod bar;

in main.rs, everything works like expected. The bar module is loaded from bar.rs and its baz module gets loaded from bar/baz.rs. Smooth sailing.

Now try adding

#[path = "../bar.rs"]
mod bar;

to bin/foo.rs. What I expected was that this would behave exactly identical to mod bar in main.rs; after all, I've just changed the implicit location of bar.rs to an explicit one.

Instead, this happens:

error[E0583]: file not found for module `baz`
 --> src\bin\..\bar.rs:1:1
  |
1 | mod baz;
  | ^^^^^^^^
  |
  = help: to create the module `baz`, create file "src\bin\..\baz.rs" or "src\bin\..\baz\mod.rs"

For more information about this error, try `rustc --explain E0583`.

Why is it looking for src\bin\..\baz.rs? It should be looking for src\bin\..\bar\baz.rs, since bar.rs is not a root module like lib.rs or main.rs, nor is it a mod.rs file.

If this behavior is expected, it's not very well documented, I certainly couldn't find anything explaining this behavior in the documentation.

Meta

rustc --version --verbose:

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-pc-windows-msvc
release: 1.65.0
LLVM version: 15.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.This change is large or controversial enough that it should have an RFC accepted before doing it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions