Skip to content

rustc_marker_type built in attribute created to have certain marker types not trigger dead code warnings from clippy#154978

Open
asder8215 wants to merge 1 commit intorust-lang:mainfrom
asder8215:phantom_pin_dead_code
Open

rustc_marker_type built in attribute created to have certain marker types not trigger dead code warnings from clippy#154978
asder8215 wants to merge 1 commit intorust-lang:mainfrom
asder8215:phantom_pin_dead_code

Conversation

@asder8215
Copy link
Copy Markdown
Contributor

@asder8215 asder8215 commented Apr 8, 2026

This PR fixes the issue on PhantomPinned triggering field is never read warning by clippy through a built in attribute called rustc_marker_type.

A few questions I have are:

  • If a built in attribute approach is okay to use here, should I extend this attribute to all the other Phantom* types?
  • Currently, I have every change I made into 1 commit. Should I separate the changes I made for compiler and the changes I made for clippy into different commits?
  • Does a built in attribute like this have uses outside of being internal to the standard library? I'm wondering if people create marker types and wish to not have dead code warnings on them.

Note: This is my first time contributing to the compiler side of Rust repos, so I'm unsure if I'm doing anything wrong with making a built in attribute. I'm just going based off my intuition from go to references in my IDE on what everything is doing.

cc @clarfonthey

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 8, 2026

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

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

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 8, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 8, 2026

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

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

@rustbot

This comment has been minimized.

@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 Apr 8, 2026
@asder8215 asder8215 force-pushed the phantom_pin_dead_code branch from 218306c to 2206c25 Compare April 8, 2026 06:38
@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. has-merge-commits PR has merge commits, merge with caution. labels Apr 8, 2026
@asder8215 asder8215 force-pushed the phantom_pin_dead_code branch 2 times, most recently from 260b712 to 55627ab Compare April 8, 2026 06:42
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the phantom_pin_dead_code branch from 55627ab to 7b574a6 Compare April 8, 2026 07:28
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the phantom_pin_dead_code branch from 7b574a6 to 60f81d1 Compare April 8, 2026 07:41
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the phantom_pin_dead_code branch from 60f81d1 to c29ad8c Compare April 9, 2026 08:36
use std::marker::PhantomData;

pub mod inner {
use std::marker;
Copy link
Copy Markdown
Contributor Author

@asder8215 asder8215 Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

For some reason, it kept telling me that it couldn't find std::marker crate, so I couldn't shortcut the field types to marker::PhantomPinned/marker::PhantomData<T>.

I'm not sure how this test operated prior to this, or if I screwed up something to cause this issue.

}

/// Checks whether a given result of a name resolution are marked as `#[rustc_marker_type]`
pub fn has_rustc_marker_type_attr(tcx: TyCtxt<'_>, res: &Res) -> bool {
Copy link
Copy Markdown
Contributor Author

@asder8215 asder8215 Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

After hours of exploring how attributes works in the compiler, I figured out that the DefId that I could get from a field type's basic result (if it doesn't Err) could inform me about whether that field type has a specific attribute associated with it. If there's a better way to do this than the way I'm doing it, I'm all ears; I'm not even sure if this is the idiomatic way of getting an attribute from a specific type.

RustcMain,

/// Represents `#[rustc_marker_type]`
RustcMarkerType(Symbol, Span),
Copy link
Copy Markdown
Contributor Author

@asder8215 asder8215 Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

I'm not sure if this is correct. I took inspiration from other enum members written like this (like Lang or RustcAsPtr). I do know that it operates with attribute parsing from lint_helpers.rs and I assume that lets us know what symbol name represents this AttributeKind, whether duplicate of the attribute applied to a certain target produces a compiler error, what target is this attribute limited to. I'm unsure what create does specifically. My hunch tells me that it's related to whether the attribute macro takes an argument or not, but unsure; if that's the case, I might just not need any arguments as part of this enum member.

}

pub(crate) struct RustcMarkerTypeParser;
impl<S: Stage> NoArgsAttributeParser<S> for RustcMarkerTypeParser {
Copy link
Copy Markdown
Contributor Author

@asder8215 asder8215 Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

Let me know if this is the correct place to place this sort of attribute. I assumed that because rustc_marker_type helped clippy with dead field warnings, it should be part of lint_helpers.rs

),
rustc_attr!(rustc_force_inline,"`#[rustc_force_inline]` forces a free function to be inlined"),
rustc_attr!(rustc_scalable_vector,"`#[rustc_scalable_vector]` defines a scalable vector type"),
rustc_attr!( rustc_marker_type, "the `#[rustc_marker_type]` attribute is used by the standard library to mark certain marker types \
Copy link
Copy Markdown
Contributor Author

@asder8215 asder8215 Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

Also, let me know if this is the correct spot to put the rustc_attr! for rustc_marker_type. It's in miscellaneous right now.

@rust-bors

This comment has been minimized.

@asder8215 asder8215 force-pushed the phantom_pin_dead_code branch from c29ad8c to 36cbcae Compare April 10, 2026 05:54
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 10, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

…ypes not trigger dead code warnings from clippy
@asder8215 asder8215 force-pushed the phantom_pin_dead_code branch from 36cbcae to 9f0d57d Compare April 10, 2026 20:02
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 10, 2026

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.

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-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants