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

Document the order preludes are resolved #1765

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jyn514
Copy link
Member

@jyn514 jyn514 commented Mar 22, 2025

Also document the derive macro sub-namespace; apparently it was missing before.

cc rust-lang/rust#66079 (comment), rust-lang/rust#138840

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Mar 22, 2025
Names of items are resolved in the following order:
1. Explicit definitions (including imports)
2. Language and macro prelude. If an item is present in both preludes, an error is emitted.
Copy link
Contributor

@petrochenkov petrochenkov Mar 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look correct, language prelude should be considered last.

In the compiler the order can be found in fn visit_scopes (it also describes the motivation behind the scope ordering).

  • Type namespace: module items ("explicit definitions") -> extern prelude -> tool prelude -> stdlib prelude -> language prelude (built-in types).
  • Value namespace: module items -> stdlib prelude.
  • Macro namespace: derive helpers -> legacy derive helpers -> macro_rules -> module items -> macro_use prelude -> stdlib prelude -> language prelude (built-in attributes).

Language and macro prelude. If an item is present in both preludes, an error is emitted.

Built-in attributes conflict with all other scopes (in attr sub-namespace), not just with preludes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This is not relevant to preludes, but the "macro_rules -> module items" should ideally be intermixed, but it's under-implemented and stubbed by errors, so probably no need to specify it for now.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok; clearly my testing was wrong and i need to think about exactly why. i have fixed the first commit (about sub-namespaces) in the meantime.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i have a question for you: visit_scopes says that in the type namespace, the extern prelude takes precedence over tool modules. this makes sense by the reasoning there since tool modules are closed but the extern prelude is not (i guess in source this correponds to the fact that extern crate can be anywhere but register_tool can only be at the crate root). but for tools it's not ideal because it means that extern crate rustfmt; will break rustfmt::skip.

that doesn't matter so much today because the tool namespace is controlled. but if we want to stabilize register tool, it becomes more of a problem. if the precedence went the other way, with tools resolved before the extern prelude, there would be a workaround: use ::rustfmt to refer to the crate. but there is no such workaround to refer to the tool module.

Copy link
Member Author

@jyn514 jyn514 Mar 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh lol i didn't ask a question. my question is: do you agree that's a problem? and if so, do you have a preferred solution?

my preferred solution is to have tool modules take precedence over the extern prelude, but i'm curious to hear your thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: The marked PR is awaiting review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants