Skip to content

Don't emit diagnostics during early lint attribute parsing - #164898

Draft
JonathanBrouwer wants to merge 5 commits into
rust-lang:mainfrom
JonathanBrouwer:silence-early-lint
Draft

JonathanBrouwer wants to merge 5 commits into
rust-lang:mainfrom
JonathanBrouwer:silence-early-lint

Conversation

@JonathanBrouwer

@JonathanBrouwer JonathanBrouwer commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

This PR makes the compiler no longer emit any errors on pre-expansion lint attributes.
That is, the following code currently doesn't compile, but compiles after this PR:

#[deny(dead_code(arguments_not_allowed_here))]
#[cfg(false)]
fn test() { }

This was historically hard to fix, but since we ported the lint attributes to the attribute parsing system, this is now relatively easy.

Blocked on #164985 and #164895. This PR is stacked on top of those two changes.

Reviewable commit by commit, every commit passes tests. The first 3 commits are from the stacked PRs.

This PR introduces the AttrErrorGuaranteed type, which is similar to ErrorGuaranteed. AttrErrorGuaranteed represents that ShouldEmit::emit_err was called, which may or may not represent an actual diagnostic being emitted, because attributes parsed pre-expansion should not emit diagnostics.

r? @mejrs

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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 Oct 25, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 25, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 25, 2026
Don't emit diagnostics during early lint attribute parsing
@rust-log-analyzer

This comment has been minimized.

@JonathanBrouwer JonathanBrouwer added the needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. label Oct 25, 2026
@rust-bors

rust-bors Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 9509e76 (9509e763f32f4738ee0877b99b28a3f18d47d7e7)
Base parent: dc44e7f (dc44e7f2c436b6041742e138b9c2176f093196d5)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (9509e76): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@bors rollup=iffy rustc-perf
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-0.2%, -0.2%] 3

Max RSS (memory usage)

Results (primary 0.3%, secondary -0.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.2% [0.5%, 1.9%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.5% [-0.6%, -0.4%] 5
Improvements ✅
(secondary)
-0.8% [-0.8%, -0.8%] 1
All ❌✅ (primary) 0.3% [-0.6%, 1.9%] 9

Cycles

Results (primary 0.4%, secondary 0.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.7% [0.4%, 1.5%] 11
Regressions ❌
(secondary)
0.7% [0.4%, 1.5%] 6
Improvements ✅
(primary)
-0.5% [-0.5%, -0.4%] 5
Improvements ✅
(secondary)
-0.7% [-0.7%, -0.7%] 1
All ❌✅ (primary) 0.4% [-0.5%, 1.5%] 16

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 487.351s -> 487.992s (0.13%)
Artifact size: 405.87 MiB -> 405.97 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 25, 2026
@rust-bors

This comment has been minimized.

`AttrErrorGuaranteed` represents that `ShouldEmit::emit_err` was called, which may or may not represent an actual diagnostic being emitted.
This new type is needed to remove the unchecked `ErrorGuaranteed` when emitting `ShouldEmit::NothingNoBug`.
@JonathanBrouwer JonathanBrouwer added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Oct 26, 2026
@JonathanBrouwer
JonathanBrouwer marked this pull request as ready for review October 26, 2026 14:43
@rustbot

rustbot commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to diagnostic attributes.

cc @mejrs

Some changes occurred in compiler/rustc_attr_ir

cc @jdonszelmann

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@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 Oct 26, 2026
@JonathanBrouwer JonathanBrouwer added I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-t-lang Status: Awaiting decision from T-lang labels Oct 26, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Member Author

This needs a @rust-lang/lang FCP.

@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 26, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 26, 2026
Don't emit diagnostics during early lint attribute parsing
@rust-bors

rust-bors Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: bf3b3ba (bf3b3ba03ef9e7aeb49f7f0f21f46e6fc2a15b72)
Base parent: 019b59d (019b59d8101bb3e141f1b880684747b4217beeb6)

@rust-timer

This comment has been minimized.


#[deny("literal")] //~ ERROR expected a lint, found literal
//[duplicate]~| ERROR expected a lint, found literal
//[duplicate]~| ERROR expected a lint, found literal

@JonathanBrouwer JonathanBrouwer Oct 26, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This test needs to be changed

View changes since the review

@mejrs

mejrs commented Oct 26, 2026

Copy link
Copy Markdown
Member

Personally I don't like assigning semantics to these attributes pre-expansion, perhaps we could get rid of that so we don't need to parse them at all.

Perhaps this could be done by getting rid of pre expansion lints entirely (for rustc, it's just the keywords in new edition lint). Or we could make these lints crate level only, so we need not think about lint attributes on such items.

See also #162813 (comment) (cc @petrochenkov)

@JonathanBrouwer

JonathanBrouwer commented Oct 26, 2026 •

Copy link
Copy Markdown
Member Author

I agree, I think the easiest thing to do is to make them crate-level only, as that's trivial to implement and preserves the behaviour of the lint which is slightly better pre expansion (tho debatable if that's even worth the implementation complexity)

If lang decides to do that I can change this PR to implement that instead (or do it as a follow-up). It would also make the code a lot nicer, as the entire last commit is not needed then :)

@rust-bors

rust-bors Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #165002) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

@Bryntet Bryntet mentioned this pull request Oct 27, 2026
@mejrs mejrs removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Oct 27, 2026
@JonathanBrouwer
JonathanBrouwer marked this pull request as draft October 27, 2026 20:59
@rustbot rustbot 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 Oct 27, 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

A-attributes Area: Attributes (`#[…]`, `#![…]`) needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-blocked Status: Blocked on something else such as an RFC or other implementation work. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-perf Status: Waiting on a perf run to be completed. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants