Skip to content

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

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

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

Conversation

@JonathanBrouwer

@JonathanBrouwer JonathanBrouwer commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

View all comments

Language change

The lint-check attributes have meaning pre-expansion, for pre-expansion linting. For example:

// edition <2024
#[cfg(false)]
#[warn(keyword_idents_2024)] // has meaning
fn gen() {} //~ WARN `gen` is a keyword in the 2024 edition

In Rust, attributes that only exist pre-expansion are not required to be semantically valid though, for example:

#[non_exhaustive(nonsense)]  // OK
#[allow] // OK
#[allow = "NONSENSE"] // OK
#[allow[wut]] // OK
#[allow(expect)] // OK
#[allow(({!}))] // OK
#[allow[helix::<ub>]] // OK
#[cfg(false)] // the attributes above only exist pre-expansion
fn example2() {}

However, the implementation of this has an inconsistency where invalid lint attributes sometimes emit errors.
This specifically happens when the meta item representing a lint is given an argument list, for example:

#[deny(dead_code(arguments_not_allowed_here))] //~ ERROR malformed lint attribute input
#[cfg(false)]
fn example3() { }

This PR fixes this inconsistency, such that lint attributes that only exist pre-expansion never emit errors.
After this PR example 3 will compile, making it consistent with example 2.

This PR implements a small part of the proposal of When do attributes gain semantic meaning?.

Implementation details

This PR introduces a new variant ShouldEmit::NothingNoBug such that an attribute parser can be called without emitting diagnostics or delayed bugs. To achieve this, we use 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.

This PR interacts with #165049. If that PR is merged, the second commit which introduces ShouldEmit::NothingNoBug can be reverted.

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.

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

This comment has been minimized.

@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 23, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 23, 2026
Don't emit diagnostics during pre-expansion lint attribute parsing
@rust-bors

rust-bors Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 322e491 (322e4913d377a5998277ee3cf7bded2f8826c275)
Base parent: 0259d5e (0259d5e9a81c6af08e40f8c204f2c0c41dca1647)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (322e491): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

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

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@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)
1.6% [1.6%, 1.6%] 1
Regressions ❌
(secondary)
0.9% [0.9%, 0.9%] 6
Improvements ✅
(primary)
-0.5% [-0.8%, -0.2%] 4
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-0.8%, 1.6%] 5

Max RSS (memory usage)

Results (secondary -1.2%)

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

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.2% [-2.7%, -0.7%] 4
All ❌✅ (primary) - - 0

Cycles

Results (secondary 0.6%)

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

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.7% [0.6%, 7.2%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.8% [-2.2%, -0.6%] 4
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 460.582s -> 460.016s (-0.12%)
Artifact size: 405.69 MiB -> 405.76 MiB (0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 23, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Member Author

The changes to unicode-normalization-0.1.24 and next-solver-stress are all noise

@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue
to verify

@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 Nov 23, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 23, 2026
Don't emit diagnostics during pre-expansion lint attribute parsing
@rust-bors

rust-bors Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 2a887ad (2a887adf7abd5b5f2a5e522c2d298b39d5adf8ad)
Base parent: 960ba7c (960ba7cf7e8666ecb449402ca82b6e9e631bd227)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (2a887ad): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

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

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@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)
1.6% [1.6%, 1.6%] 1
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
-0.5% [-0.8%, -0.2%] 4
Improvements ✅
(secondary)
-0.8% [-1.2%, -0.1%] 9
All ❌✅ (primary) -0.1% [-0.8%, 1.6%] 5

Max RSS (memory usage)

Results (primary -0.0%, secondary -3.5%)

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

mean range count
Regressions ❌
(primary)
1.5% [0.7%, 2.4%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.1% [-3.1%, -3.1%] 1
Improvements ✅
(secondary)
-3.5% [-4.5%, -2.3%] 5
All ❌✅ (primary) -0.0% [-3.1%, 2.4%] 3

Cycles

Results (secondary 1.7%)

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

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.2% [0.7%, 9.1%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.2% [-4.8%, -1.1%] 3
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 460.809s -> 458.49s (-0.50%)
Artifact size: 406.40 MiB -> 405.75 MiB (-0.16%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 23, 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`.
@rustbot

rustbot commented Nov 23, 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.

@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

@nikomatsakis

Copy link
Copy Markdown
Contributor

@rfcbot reviewed

This seems right to me. Pre-expansion attributes don't have to be semantically valid, and lint attributes shouldn't be the exception. Like @tmandry, I think missing nonsense attributes on cfg'd-out items is consistent with how cfg works elsewhere.

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 (`#[…]`, `#![…]`) disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. perf-regression Performance regression. 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. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants