Skip to content

Produce an error when #[inline] and #[rust_force_inline] are used together - #158814

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
tahadostifam:taha_refactors_rustc_attr_parsing
Aug 1, 2026
Merged

Produce an error when #[inline] and #[rust_force_inline] are used together#158814
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
tahadostifam:taha_refactors_rustc_attr_parsing

Conversation

@tahadostifam

@tahadostifam tahadostifam commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

View all comments

Merged RustcForceInline & Inline Attribute Parsers

I've been reading #153101, #131229 and I came across with this comment in compiler/rustc_attr_parsing/src/attributes/inline.rs:

// FIXME(jdonszelmann): merge these two parsers and error when both attributes are present here.
//                      note: need to model better how duplicate attr errors work when not using
//                      SingleAttributeParser which is what we have two of here.

Having separate SingleAttributeParser implementations for #[rustc_force_inline] and #[inline(...)] meant the compiler wasn't able to recognize cases where both of them were used together on the same item, for example:

#![feature(rustc_attrs)]

#[rustc_force_inline]
#[inline]
fn foo() {}

fn main() {}
image

This case was previously considered allowed, which is not correct.

My changes replace the distinct attribute parsers with a single unified AttributeParser implementation for InlineParser, handling both attributes together in a single pass. By consolidating the logic, the parser now tracks the state of both attributes side-by-side using an AcceptMapping and introduces a new session diagnostic, InlineForceInlineConflict, which is explicitly triggered in the finalize step if a user attempts to combine them.

An added benefit of catching this conflict early during the parsing phase is that it prevents downstream validation passes (like check_attr) from triggering redundant errors on malformed or incorrectly placed attributes, which naturally cleans up and streamlines our compiler stderr output as reflected in the updated UI test baselines.

This is my very first PR on rustc, and I am incredibly grateful to @hkalbasi, who heavily guided me through the codebase and helped make this change possible!

Sincerely looking for your feedback and thoughts on this, let me know if there is something need to be changed.

@rustbot

rustbot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 5, 2026
@rustbot

rustbot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @folkertdev (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 22 candidates

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 5, 2026
@tahadostifam
tahadostifam force-pushed the taha_refactors_rustc_attr_parsing branch from bd990d8 to 15b16bb Compare July 5, 2026 13:42
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 5, 2026
@tahadostifam
tahadostifam marked this pull request as draft July 5, 2026 14:25
@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 Jul 5, 2026
@tahadostifam
tahadostifam force-pushed the taha_refactors_rustc_attr_parsing branch from 15b16bb to b2e4430 Compare July 5, 2026 14:43
@tahadostifam
tahadostifam marked this pull request as ready for review July 5, 2026 17:58
@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 Jul 5, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Contributor

(would like to take a look at this, this might take me a few days to get to, should have time to take a look on friday)

@JonathanBrouwer JonathanBrouwer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Haven't reviewed the full diff yet, will do so after these two comments are fixed, thanks :3

View changes since this review

Comment thread tests/ui/attributes/args-checked.stderr Outdated
| ^^^^^^^^^^^^^^^^---^^
| |
| didn't expect any arguments here
| valid arguments are `always` or `never`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change is not correct, always does not take any arguments.
This error would suggest always(always) would be valid.
Please drop that commit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You made changes to tests that are not related to this PR, could you at the very least split these changes into individual commits, and preferably into separate PRs? It makes the diff hard to review

@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 Jul 10, 2026
@rustbot

rustbot commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@tahadostifam
tahadostifam force-pushed the taha_refactors_rustc_attr_parsing branch from 6df20c4 to 0f6e739 Compare July 10, 2026 17:54
@rust-log-analyzer

This comment has been minimized.

@tahadostifam
tahadostifam force-pushed the taha_refactors_rustc_attr_parsing branch from 0f6e739 to 9e86aa9 Compare July 10, 2026 18:59
@tahadostifam

Copy link
Copy Markdown
Contributor Author

Thanks for your feedback, I've applied the new changes and removed irrelevant ones.

#[inline(always = 5)]
fn foo() {}

This now emits unexpected_args as you said:

error[E0565]: malformed `inline` attribute input
  --> /tmp/main.rs:14:1
   |
14 | #[inline(always = 5)]
   | ^^^^^^^^^----------^^
   |          |
   |          didn't expect any arguments here

Currently with rustc 1.98-nightly this compiles with no error:

#[inline(always = 1)]
#[inline(always = "a")]
fn foo() {}

#[inline(always = 1)]
fn bar() {}

#[inline(always = "b")]
fn baz() {}

#[inline(always(always))]
fn buz() {}

fn main() {}

(Only generates warning but compiles successfully)

All of those malformed cases are covered now, with my changes.

Also reviewed spans of diagnostics several times, but if there is still something that must be changed, let me know.

@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 Jul 31, 2026
@tahadostifam

Copy link
Copy Markdown
Contributor Author

Hi dear Jonathan.

I truly appreciate your time and consideration of reviewing this PR.
I think you're right, we better to keep them separate and add a finalize_check;
But I tried to merge them because of @jdonszelmann comment in the inline.rs file.

Now I'm working to separate them and add check for it.

@JonathanBrouwer

Copy link
Copy Markdown
Contributor

Yeah that comment is very old from a time when finalize_check didn't exist yet :)

@tahadostifam
tahadostifam force-pushed the taha_refactors_rustc_attr_parsing branch from 1c8f102 to b2e3b4d Compare July 31, 2026 22:13
@rustbot

rustbot commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

@rustbot

This comment has been minimized.

Comment thread compiler/rustc_hir/src/attrs/data_structures.rs Outdated
Comment thread compiler/rustc_attr_parsing/src/attributes/inline.rs Outdated
Comment thread compiler/rustc_attr_parsing/src/attributes/inline.rs Outdated
@tahadostifam
tahadostifam force-pushed the taha_refactors_rustc_attr_parsing branch from b2e3b4d to b1ad224 Compare August 1, 2026 13:27
@rustbot

This comment has been minimized.

}

fn finalize_check(cx: &FinalizeCheckContext<'_, '_>, attr_span: Span) {
if let Some((attr, span)) = find_attr!(cx.parsed_attrs, Inline(attr, span) => (attr, span))

@JonathanBrouwer JonathanBrouwer Aug 1, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This will match the first inline attribute, whether it is force or not.
I think it should work if you do

find_attr!(cx.parsed_attrs, Inline(attr, span) if !matches!(attr, InlineAttr::Force { .. }) => (attr, span))
`

*[View changes since the review](https://triagebot.infra.rust-lang.org/gh-changes-since/rust-lang/rust/158814/c47bb2f39658c00b109693df9611d7238bedf6fa..b1ad22425d157eb8968bb11ffda53dc422dd91a5)*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add a uitest for both orders of the attributes? (So inline first and rustc_force_inline first)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought it's cleaner to have if !matches!(attr, InlineAttr::Force { .. }) separate. Shall I change it?

@JonathanBrouwer JonathanBrouwer Aug 1, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The meaning is different, this will select the first Inline and incorrectly if it is a Force attribute it does nothing. That means it will not output an error for:

#[rustc_force_inline]
#[inline]

@JonathanBrouwer

Copy link
Copy Markdown
Contributor

I think we're almost there now, thanks for the effort!

@tahadostifam
tahadostifam force-pushed the taha_refactors_rustc_attr_parsing branch from b1ad224 to 98cc5dc Compare August 1, 2026 15:56
@rustbot

rustbot commented Aug 1, 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.

@JonathanBrouwer JonathanBrouwer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@bors r+ rollup
Thanks a lot!

View changes since this review

@rust-bors

rust-bors Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 98cc5dc has been approved by JonathanBrouwer

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 1, 2026
@tahadostifam

Copy link
Copy Markdown
Contributor Author

Thanks a lot, I truly appreciate your kindness and patience!❤️
Hope to have more contributions in the future.

@JonathanBrouwer

Copy link
Copy Markdown
Contributor

Hope to see you around more as well, was awesome working with you <3

@JonathanBrouwer JonathanBrouwer changed the title Merged RustcForceInline & Inline Attribute Parsers Produce an error when #[inline] and #[rust_force_inline] are used together Aug 1, 2026
rust-bors Bot pushed a commit that referenced this pull request Aug 1, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #160262 (Library lock file maintenance)
 - #158548 (Move `std::io::copy` to `alloc::io`)
 - #158814 (Produce an error when `#[inline]` and `#[rust_force_inline]` are used together)
 - #160025 (Fix an edge case with `StepBy::nth` on non-fused iterators)
 - #160271 (Resolver: Introduce `CmRef` which has a speclative borrow variant for `CmRefCell`)
 - #160281 (Fix(lib/fs/tests): Avoid permission denials when cleaning up TempDirs in `set_get_permissions_nofollows*`)
 - #160325 (tidy: Check `proc_macro_deps.rs` by reading it, not by including it)
 - #160334 (Add regression test for unused_allocation on boxed comparison)
@rust-bors
rust-bors Bot merged commit 0489fb0 into rust-lang:main Aug 1, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Aug 1, 2026
rust-timer added a commit that referenced this pull request Aug 1, 2026
Rollup merge of #158814 - tahadostifam:taha_refactors_rustc_attr_parsing, r=JonathanBrouwer

Produce an error when `#[inline]` and `#[rust_force_inline]` are used together

### Merged RustcForceInline & Inline Attribute Parsers

I've been reading [https://github.com/rust-lang/rust/issues/153101](https://github.com/rust-lang/rust/issues/153101), #131229 and I came across with this comment in `compiler/rustc_attr_parsing/src/attributes/inline.rs`:

```rust
// FIXME(jdonszelmann): merge these two parsers and error when both attributes are present here.
//                      note: need to model better how duplicate attr errors work when not using
//                      SingleAttributeParser which is what we have two of here.

```

Having separate `SingleAttributeParser` implementations for `#[rustc_force_inline]` and `#[inline(...)]` meant the compiler wasn't able to recognize cases where both of them were used together on the same item, for example:

```rust
#![feature(rustc_attrs)]

#[rustc_force_inline]
#[inline]
fn foo() {}

fn main() {}
```

<img width="1222" height="915" alt="image" src="https://github.com/user-attachments/assets/69d4e3b0-bc21-4679-8f3d-017f9fe152fb" />

This case was previously considered allowed, which is not correct.

My changes replace the distinct attribute parsers with a single unified `AttributeParser` implementation for `InlineParser`, handling both attributes together in a single pass. By consolidating the logic, the parser now tracks the state of both attributes side-by-side using an `AcceptMapping` and introduces a new session diagnostic, `InlineForceInlineConflict`, which is explicitly triggered in the `finalize` step if a user attempts to combine them.

An added benefit of catching this conflict early during the parsing phase is that it prevents downstream validation passes (like `check_attr`) from triggering redundant errors on malformed or incorrectly placed attributes, which naturally cleans up and streamlines our compiler stderr output as reflected in the updated UI test baselines.

**This is my very first PR on rustc**, and I am incredibly grateful to @hkalbasi, who heavily guided me through the codebase and helped make this change possible!

Sincerely looking for your feedback and thoughts on this, let me know if there is something need to be changed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants