Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Warn against mod lib; in main.rs #56314

Open
Kimundi opened this issue Nov 28, 2018 · 6 comments
Open

Warn against mod lib; in main.rs #56314

Kimundi opened this issue Nov 28, 2018 · 6 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Kimundi
Copy link
Member

Kimundi commented Nov 28, 2018

Right now this is possible and compiles without warning:

Given a cargo crate with name "foo":

// src/lib.rs
// src/main.rs

mod lib;
fn main() {}

... even though we now have duplicated code in the library "foo" and the submodule "lib"

This can be confusing for a beginner because the code can still compile or just breaks "late" depending on what you do:

// src/lib.rs

pub struct Foo;
// src/main.rs

mod lib;

// either of those work (though not at the same time):
use foo::Foo;
use crate::lib::Foo;

fn main() {}

cargo or rustc should detect if we use lib.rs as a submodule, and warn or error against it.


This issue assumes 2018 edition without uniform paths syntax.

@eddyb
Copy link
Member

eddyb commented Nov 28, 2018

cc @Centril @aturon @joshtriplett

@Centril Centril added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Nov 28, 2018
@Centril
Copy link
Contributor

Centril commented Nov 28, 2018

This issue assumes 2018 edition without uniform paths syntax.

Does it not become moot then since that's what we are going to do?

@steveklabnik
Copy link
Member

This would have to be done in Cargo, because only cargo even knows if there's another project being built.

additionally, it's a bit more general than this; while lib.rs is the default, you can name it anything.

@Centril Centril added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. labels Nov 28, 2018
@Kimundi
Copy link
Member Author

Kimundi commented Nov 28, 2018

@Centril: Just wanted to document what I based this issue on, since I was not sure if something influences the behavior here.


@steveklabnik: Right, it could be named anything, hm...

How about this: rustc gets a new flag used by cargo to inform it about "possible extern crate roots" or more general "possible extern source files". Cargo then looks at whether there are crates in the dependency chain that share source directories, and passes the module file paths as needed.

Whenever rustc sees a modulefile, it checks it against the list from the flag and emits an warning.

@neilyoung
Copy link

I'm astonished, that this simple problem is still not solved. See discussion here:

https://users.rust-lang.org/t/confusion-about-module-name-ambiguity/50512/9

@kadiwa4
Copy link
Contributor

kadiwa4 commented Jan 25, 2023

Since #94467, Rust warns against this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants