Skip to content

raise ambiguity error on attribute macro that could be tool attribute - #162597

Open
mejrs wants to merge 1 commit into
rust-lang:mainfrom
mejrs:ambiguity
Open

mejrs wants to merge 1 commit into
rust-lang:mainfrom
mejrs:ambiguity

Conversation

@mejrs

@mejrs mejrs commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

View all comments

See #162597 (comment) for clean crater run; the errors are unrelated/spurious.

We need some sort of ambiguity error in other to implement the register tool rfc:

Fixing name resolution errors

Note that register_tool changes name resolution, and may give errors if you have a crate named some_tool.
The compiler will suggest ways to fix the new errors.

If a tool name conflicts with a crate name, you can disambiguate the crate with ::some_tool:

#![register_tool(some_tool)]
extern crate some_tool;

#[some_tool::attribute] //~ ERROR: is this the tool or a proc-macro?
fn bar() {
   // ...
}

#[::some_tool::attribute] // OK: This is the proc-macro defined in the crate.
fn foo() {
  // ...
}

However, if you want to go on to use a tool attribute,
you must rename the crate so it doesn't conflict:

#![register_tool(some_tool)]
extern crate some_tool as my_library;

#[some_tool::attribute] // OK: This is the attribute specified by the tool.
fn bar() {
   // ...
}

Alternatively, if you only want to use lints, you can use register_lint_tool instead of register_tool, which will avoid resolution errors.

Overlaps like this are expected to be rare in practice.

This PR implements the ambiguity error (not the "leading :: to disambiguate" part).

This is a breaking change if:

  • someone has a crate or module named diagnostic|miri|rust_analyzer|clippy|rustfmt and exports an attribute macro from it
  • it is then used it as #[(diagnostic|miri|rust_analyzer|clippy|rustfmt)::attr_macro] (rather than use module_or_crate::attr_macro; #[attr_macro])
  • the same applies for tools registered with register_tool, but that's unstable.

See #158146 for context. This is a simpler version of that PR.

This does not implement unconditionally resolving these as tool attributes but does reserve the ability to do so. This would allow things like this to compile:

mod rustfmt {}

#[rustfmt::skip]
fn main() {}

and see also #98291 for a case where it would be nice to assume e.g. #[rustfmt::skip] is actually a tool attribute and thus inert.

@mejrs mejrs added the needs-crater This change needs a crater run to check for possible breakage in the ecosystem. label Sep 10, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 10, 2026
@mejrs

mejrs commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
raise ambiguity error on attribute macro that could be tool attribute
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 8c2ec58 (8c2ec5867b7cd0db3869fc1c128b867c2e8c0129)
Base parent: c4c4a57 (c4c4a576936e9e67717d0deb8e74e02dd5dd10de)

@mejrs

mejrs commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
raise ambiguity error on attribute macro that could be tool attribute
@rust-bors

rust-bors Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: be3da1e (be3da1e8c043bb6f1f335463d3b1335cdf50b03c)
Base parent: 018018e (018018e881e2db0956f229dbb543e21f058d1ce7)

@mejrs

mejrs commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

@craterbot check

@craterbot

Copy link
Copy Markdown
Collaborator

👌 Experiment pr-162597 created and queued.
🤖 Automatically detected try build be3da1e
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 10, 2026
@craterbot

Copy link
Copy Markdown
Collaborator

🚧 Experiment pr-162597 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot

Copy link
Copy Markdown
Collaborator

🎉 Experiment pr-162597 is completed!
📊 3 regressed and 1 fixed (1108363 total)
📊 6349 spurious results on the retry-regressed-list.txt, consider a retry1 if this is a significant amount.
📰 Open the summary report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Footnotes

  1. re-run the experiment with crates=https://crater-reports.s3.amazonaws.com/pr-162597/retry-regressed-list.txt ↩

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Sep 18, 2026
@mejrs

mejrs commented Sep 18, 2026

Copy link
Copy Markdown
Member Author

Looks like the crater run is clean, so I guess we can do this (If the lang team agrees, of course)

r? @petrochenkov

@mejrs
mejrs marked this pull request as ready for review September 18, 2026 22:31
Comment thread compiler/rustc_resolve/src/macros.rs Outdated
Comment thread compiler/rustc_resolve/src/macros.rs Outdated
Comment thread compiler/rustc_resolve/src/macros.rs Outdated
Comment thread compiler/rustc_resolve/src/macros.rs Outdated
@petrochenkov

Copy link
Copy Markdown
Contributor

So the main drawback here is the same as with built-in attributes - with these rules you cannot add a new built-in tool module without a breakage (maybe theoretical).

But if register_tool is stabilized, then new built-in tools are hopefully never added, and most of existing built-in tools are migrated to explicit registration in the next edition, so this is not going to be a big problem.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 25, 2026
@rustbot

rustbot commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@mejrs mejrs removed the needs-crater This change needs a crater run to check for possible breakage in the ecosystem. label Sep 27, 2026
@mejrs

mejrs commented Sep 27, 2026 •

Copy link
Copy Markdown
Member Author

@rustbot ready

So the main drawback here is the same as with built-in attributes - with these rules you cannot add a new built-in tool module without a breakage (maybe theoretical).

But if register_tool is stabilized, then new built-in tools are hopefully never added, and most of existing built-in tools are migrated to explicit registration in the next edition, so this is not going to be a big problem.

As far as new builtin tools go, the only idea I've seen is a lint tool for lint helpers in #t-lang > Extend the diagnostics attr for a lint helper @ 💬. Interestingly, an attribute macro is never used in this way (at least nowhere on github) so that breakage likely is only theoretical.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 27, 2026
@mejrs

mejrs commented Sep 27, 2026

Copy link
Copy Markdown
Member Author

By the way what's the process for merging something like this, where it's part of a rfc and technically a breaking change but has a clean crater run? Does any team need to be involved?

@petrochenkov petrochenkov added S-waiting-on-t-lang Status: Awaiting decision from T-lang T-lang Relevant to the language team I-lang-nominated Nominated for discussion during a lang team meeting. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 27, 2026
@traviscross traviscross added P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. labels Sep 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang S-waiting-on-t-lang Status: Awaiting decision from T-lang T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants