Skip to content

Ambiguous terminology "crate root" #82876

Closed

Description

@CryZe

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

Activity

  1. Manishearth commented on Mar 7, 2021

    @Manishearth
    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.

  2. Manishearth commented on Mar 7, 2021

    @Manishearth
    Member

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

  3. mcclure commented on Mar 7, 2021

    @mcclure

    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.

  4. Manishearth commented on Mar 7, 2021

    @Manishearth
    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

  5. self-assigned this
    on Mar 7, 2021
  6. Manishearth commented on Mar 7, 2021

    @Manishearth
    Member

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

  7. Manishearth commented on Mar 7, 2021

    @Manishearth
    Member
  8. added a commit that references this issue on Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions