Skip to content

Port #[rustc_as_ptr] to the new attribute system #142498

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

Merged
merged 1 commit into from
Jun 16, 2025

Conversation

GrigorenkoPV
Copy link
Contributor

@GrigorenkoPV GrigorenkoPV commented Jun 14, 2025

It might make sense to introduce some new parser analogous to Single, but even more simple: for parsing attributes that take no arguments and may appear only once (such as #[rustc_as_ptr] or #[rustc_const_stable_indirect]). Not sure if this should be a single impl parsing all such attributes, or one impl per attribute. Or how it will play along with the upcoming rework of attribute validation. Or how these argumentless attributes should be called (I've loosely referred to them as markers in the name of the new module in this PR, but not sure how good it is).

This is a part of #131229, so
r? @jdonszelmann


For reference, the #[rustc_as_ptr] attribute was created back in #132732 as a followup to #128985.

@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 Jun 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 14, 2025

Some changes occurred in compiler/rustc_attr_data_structures

cc @jdonszelmann

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

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

cc @jdonszelmann

Copy link
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

Or how these argumentless attributes should be called (I've loosely referred to them as markers in the name of the new module in this PR, but not sure how good it is).

"markers" already means quite a lot, see https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html for example.

Anyway, your questions seem to be about code organization - I think it would be better to organize attributes based on what they do rather than how they are parsed.

a single impl parsing all such attributes

The downside of this approach is that updating an attribute to take an argument would get annoying. Say if we had just #[blah] and then changed it to #[blah(alot|maybe| <nothing>)] then that would require moving all the code related to parsing blah, rather than just making changes in it.

@@ -147,6 +147,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::ConstStabilityIndirect
| AttributeKind::MacroTransparency(_),
) => { /* do nothing */ }
Attribute::Parsed(AttributeKind::AsPtr) => {
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
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 an ICE, check_applied_to_fn_or_method runs attr.span() which aren't supported on parsed attributes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Copy link
Contributor

Choose a reason for hiding this comment

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

no it isn't, look

attr_span: attr.span(),

It still calls attr.span()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

ah right

Copy link
Contributor

Choose a reason for hiding this comment

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

I kind of want to get rid of that function, the few that are in that list are there because it was too painful to migrate otherwise

Copy link
Contributor

Choose a reason for hiding this comment

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

could you solve it properly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could you solve it properly?

Fully getting rid of that function is going to be quite an effort, because it is in impl AttributeExt for Attribute, which is relied on by many parts of the compiler.

I've started the process (#142552) of migrating from Attribute to either AttrItem or AttributeKind in other parts of the compiler, which should help both with getting rid of that impl specifically and with other #131229-related changes in general.

While this PR can technically be fixed independently of #142552, it will cause merge conflicts, so I'm going to wait for that one first instead.

Copy link
Contributor

@jdonszelmann jdonszelmann Jun 16, 2025

Choose a reason for hiding this comment

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

no, I mean that check_attr doesn't rely on .span() for rustc_as_ptr

@jdonszelmann
Copy link
Contributor

a single impl parsing all such attributes

I agree with @mejrs here, all attributes should get their own parser. Only if a parsed hir attribute's diagnostics depends on more than one syntactic attribute is it sensible to combine accept functions (see stability, and for example I intend to do this with allow/deny)

@jdonszelmann
Copy link
Contributor

jdonszelmann commented Jun 14, 2025

I do intend to make a simpler parser for single word attribute parsers just like there's already SingleAttributeParser. That transformation is trivial later, let's keep it as is for now. This change is at least conceptually good @GrigorenkoPV , just needs some minor work

@jdonszelmann
Copy link
Contributor

@rustbot author

@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 Jun 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 14, 2025

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

@GrigorenkoPV
Copy link
Contributor Author

@rustbot ready

@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 Jun 14, 2025
@jdonszelmann
Copy link
Contributor

@rustbot author

@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 Jun 15, 2025
@GrigorenkoPV
Copy link
Contributor Author

@rustbot ready

@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 Jun 15, 2025
@jdonszelmann
Copy link
Contributor

@rustbot author

@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 Jun 15, 2025
@rustbot rustbot added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Jun 15, 2025
@GrigorenkoPV GrigorenkoPV force-pushed the as-ptr-refactor branch 2 times, most recently from aec31cf to ff6ed54 Compare June 16, 2025 14:19

fn convert(_cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
// FIXME: check that there's no args
Some(AttributeKind::AsPtr(_cx.attr_span))
Copy link
Contributor

Choose a reason for hiding this comment

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

_cx --> cx

@jdonszelmann
Copy link
Contributor

jdonszelmann commented Jun 16, 2025

looks good to me after that, r=me after CI passes and that's fixed

@jdonszelmann
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 16, 2025

📌 Commit da8d6bb has been approved by jdonszelmann

It is now in the queue for this repository.

@bors bors 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. S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Jun 16, 2025
bors added a commit that referenced this pull request Jun 16, 2025
Rollup of 8 pull requests

Successful merges:

 - #139340 (Fix RISC-V C function ABI when passing/returning structs containing floats)
 - #142341 (Don't suggest converting `///` to `//` when expecting `,`)
 - #142414 (ignore `run-make` tests that need `std` on targets without `std`)
 - #142498 (Port `#[rustc_as_ptr]` to the new attribute system)
 - #142554 (Fix `PathSource` lifetimes.)
 - #142562 (Update the `backtrace` submodule)
 - #142565 (Test naked asm for wasm32-unknown-unknown)
 - #142573 (`fn candidate_is_applicable` to method)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1e950ef into rust-lang:master Jun 16, 2025
10 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 16, 2025
@GrigorenkoPV GrigorenkoPV deleted the as-ptr-refactor branch June 16, 2025 21:27
rust-timer added a commit that referenced this pull request Jun 16, 2025
Rollup merge of #142498 - GrigorenkoPV:as-ptr-refactor, r=jdonszelmann

Port `#[rustc_as_ptr]` to the new attribute system

It might make sense to introduce some new parser analogous to `Single`, but even more simple: for parsing attributes that take no arguments and may appear only once (such as `#[rustc_as_ptr]` or `#[rustc_const_stable_indirect]`). Not sure if this should be a single `impl` parsing all such attributes, or one impl per attribute. Or how it will play along with the upcoming rework of attribute validation. Or how these argumentless attributes should be called (I've loosely referred to them as `markers` in the name of the new module in this PR, but not sure how good it is).

This is a part of #131229, so
r? `@jdonszelmann`

---

For reference, the `#[rustc_as_ptr]` attribute was created back in #132732 as a followup to #128985.
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.

5 participants