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

Ambiguous terminology "crate root" #82876

Closed
CryZe opened this issue Mar 7, 2021 · 6 comments · Fixed by #82881
Closed

Ambiguous terminology "crate root" #82876

CryZe opened this issue Mar 7, 2021 · 6 comments · Fixed by #82881
Assignees

Comments

@CryZe
Copy link
Contributor

CryZe commented Mar 7, 2021

In Rust 2018 the "crate namespace" now lives outside of the "crate root", yet the reference and the compiler still refer to it as the "crate root". So if you write ::foo::bar this looks up foo in the "crate namespace", but the compiler still calls this the "crate root" as can be seen here:

error[E0412]: cannot find type `Bar` in the crate root
 --> src/lib.rs:4:16
  |
4 |     fn ok(_: ::Bar) {}
  |                ^^^ not found in the crate root
  |
help: consider importing this struct
  |
4 |     use crate::Bar;
  |

The help even implies that Bar is indeed in the "crate root".

The reference seems to have a similar problem: https://doc.rust-lang.org/reference/paths.html#path-qualifiers

Paths starting with :: are considered to be global paths where the segments of the path start being resolved from the crate root.

I'm not sure if a proper term for the crate namespace got defined, but it's ambiguous at the moment and causes confusion for beginners: https://twitter.com/mcclure111/status/1368658525891747845

@Manishearth
Copy link
Member

The reference is referring to Rust 2015 and needs to be updated.

I don't think we need a name for it, but rather we can specify that when you use :: then the thing after it must be a crate name.

@Manishearth
Copy link
Member

Manishearth commented Mar 7, 2021

Actually, it's called the "extern prelude" but nobody uses that term. The reference mentions this.

@mcclure
Copy link

mcclure commented Mar 7, 2021

Twitter kind of makes it unclear exactly what Cryze is linking there so to be clear: the thing that confused me, as a newbie, reading the reference is in 2.7 of the reference, it says:

  • for ::, "the segments of the path start being resolved from the crate root"
  • whereas crate "resolves the path relative to the current crate"

Until this was explained to me, it sounded to me like these two quotes were designing the same concept. The point of confusion for me was that "the crate root" could ambiguously be read as "the root of all crates" or "the root of the crate". I was reading the reference and maybe it is expected a reader of the reference realizes "the crate root" means something specific and special, but it did trip me up.

@Manishearth
Copy link
Member

Yeah the "crate root" means a specific thing, whereas :: refers to some notion of "global path" which differs based on edition -- "crate root" in 2015, and "extern prelude" in 2018+. We should stop saying :: is the crate root. I already have a PR for the reference in rust-lang/reference#974 , fixing the Rust diagnostic might be trickier but I'm looking at it

@Manishearth Manishearth self-assigned this Mar 7, 2021
@Manishearth
Copy link
Member

I have patches for this, just need to update test expectations

@Manishearth
Copy link
Member

#82881

@bors bors closed this as completed in 3908eec Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants