Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ebfe7b0
moving trivial field reads attr to macros
josetorrs Aug 7, 2026
1b9c779
formatting
josetorrs Aug 7, 2026
7f8e375
moving attribute for Clone
josetorrs Aug 12, 2026
949df57
adding test
josetorrs Aug 13, 2026
6de1a8d
moving attr on Debug, remove from minicore
josetorrs Aug 13, 2026
609cb03
remove temporary changes
josetorrs Aug 13, 2026
b3773ee
revert closer to the original check
josetorrs Aug 13, 2026
dc662df
checking on impl item
josetorrs Aug 19, 2026
0b7febf
bless existing tests
josetorrs Aug 19, 2026
f26181b
update code comment
josetorrs Aug 19, 2026
7dbd378
bless macro stats tests
josetorrs Aug 19, 2026
6bc0535
blessing deriving all codegen test
josetorrs Aug 20, 2026
f3a904b
expanding attr to impl blocks and refactoring should_explore to not c…
josetorrs Aug 23, 2026
b2bde61
allow free functions as a target, move find_attr check eagerly in vis…
josetorrs Aug 23, 2026
c3526b7
expanding target to traits with a default method also moving and upda…
josetorrs Aug 23, 2026
6282fbd
adding invalid targets tests, rename introduced dead code tests
josetorrs Aug 24, 2026
f4e1a1d
remove old named tests
josetorrs Aug 24, 2026
52bd2b3
formatting
josetorrs Aug 24, 2026
b8adfe0
move invalid targets test to attributes tests directory
josetorrs Aug 24, 2026
cf0f6ce
forget to bless tests after move and rename
josetorrs Aug 24, 2026
d1d3be2
restoring previous commit change, putting attr lookup in should_explore
josetorrs Aug 26, 2026
42b9de3
fixing rebase
josetorrs Sep 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,12 @@ pub(crate) struct RustcTrivialFieldReadsParser;

impl NoArgsAttributeParser for RustcTrivialFieldReadsParser {
const PATH: &[Symbol] = &[sym::rustc_trivial_field_reads];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::Fn),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
]);
const STABILITY: AttributeStability = unstable!(rustc_attrs);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcTrivialFieldReads;
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ pub(crate) fn expand_deriving_clone(
explicit_self: true,
nonself_args: SmallVec::new(),
ret_ty: Self_,
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes: thin_vec![
cx.attr_word(sym::inline, span),
cx.attr_word(sym::rustc_trivial_field_reads, span)
],
fieldless_variants_strategy: FieldlessVariantsStrategy::Default,
combine_substructure: substructure,
}],
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ pub(crate) fn expand_deriving_debug(
explicit_self: true,
nonself_args: smallvec![(fmtr, sym::character('f'))],
ret_ty: Path(path_std!(fmt::Result)),
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes: thin_vec![
cx.attr_word(sym::inline, span),
cx.attr_word(sym::rustc_trivial_field_reads, span)
],
fieldless_variants_strategy:
FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless,
combine_substructure: combine_substructure(show_substructure),
Expand Down
15 changes: 7 additions & 8 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
| DefKind::ExternCrate
| DefKind::Use
| DefKind::Ctor(..)
| DefKind::ForeignMod => true,
| DefKind::ForeignMod => !find_attr!(tcx, def_id, RustcTrivialFieldReads),

@mejrs mejrs Aug 27, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm or maybe do this check for the things we support? I think that's DefKind::Fn only

View changes since the review

@josetorrs josetorrs Sep 5, 2026

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.

we can try this but I'm still getting a lot of the warnings whenever I build. I do not get the warnings when I put it it in visit_node but still trying to figure out why (moving back to draft while I figure it out)


DefKind::TyParam
| DefKind::ConstParam
Expand Down Expand Up @@ -444,17 +444,16 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
ControlFlow::Continue(())
}

/// Automatically generated items marked with `rustc_trivial_field_reads`
/// Items marked with `rustc_trivial_field_reads`
/// will be ignored for the purposes of dead code analysis (see PR #85200
/// for discussion).
/// for discussion, and PR #160666).
fn should_ignore_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) -> bool {
if let hir::ImplItemImplKind::Trait { .. } = impl_item.impl_kind
&& let impl_of = self.tcx.local_parent(impl_item.owner_id.def_id)
&& self.tcx.is_automatically_derived(impl_of.to_def_id())
&& let trait_ref =
self.tcx.impl_trait_ref(impl_of).instantiate_identity().skip_norm_wip()
&& find_attr!(self.tcx, trait_ref.def_id, RustcTrivialFieldReads)
&& find_attr!(self.tcx, impl_item.owner_id.def_id, RustcTrivialFieldReads)

@mejrs mejrs Aug 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This attribute check is redundant now, right?

View changes since the review

{
let trait_ref = self.tcx.impl_trait_ref(impl_of).instantiate_identity().skip_norm_wip();

if let ty::Adt(adt_def, _) = trait_ref.self_ty().kind()
&& let Some(adt_def_id) = adt_def.did().as_local()
{
Expand All @@ -463,7 +462,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
return true;
}

false
return false;
}

fn visit_node(
Expand Down
3 changes: 1 addition & 2 deletions library/core/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ mod uninit;
#[stable(feature = "rust1", since = "1.0.0")]
#[lang = "clone"]
#[rustc_diagnostic_item = "Clone"]
#[rustc_trivial_field_reads]
#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
pub const trait Clone: Sized {
/// Returns a duplicate of the value.
Expand Down Expand Up @@ -285,7 +284,7 @@ pub const unsafe trait TrivialClone: [const] Clone {}
/// Derive macro generating an impl of the trait `Clone`.
#[rustc_builtin_macro]
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow_internal_unstable(core_intrinsics, derive_clone_copy_internals, trivial_clone)]
#[allow_internal_unstable(core_intrinsics, derive_clone_copy_internals, trivial_clone, rustc_attrs)]
pub macro Clone($item:item) {
/* compiler built-in */
}
Expand Down
3 changes: 1 addition & 2 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,6 @@ impl Display for Arguments<'_> {
)]
#[doc(alias = "{:?}")]
#[rustc_diagnostic_item = "Debug"]
#[rustc_trivial_field_reads]
pub trait Debug: PointeeSized {
#[doc = include_str!("fmt_trait_method_doc.md")]
///
Expand Down Expand Up @@ -1089,7 +1088,7 @@ pub(crate) mod macros {
/// Derive macro generating an impl of the trait `Debug`.
#[rustc_builtin_macro]
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow_internal_unstable(core_intrinsics, fmt_helpers_for_derive)]
#[allow_internal_unstable(core_intrinsics, fmt_helpers_for_derive, rustc_attrs)]
pub macro Debug($item:item) {
/* compiler built-in */
}
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/attributes/trivial-field-reads-invalid-targets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Checks that `#[rustc_trivial_field_reads]` are not allowed due to invalid targets

#![feature(rustc_attrs)]
#![deny(dead_code)]

trait Whatever {
#[rustc_trivial_field_reads] //~ERROR the `rustc_trivial_field_reads` attribute cannot be used on required trait methods
fn read(&self);
}

#[rustc_trivial_field_reads] //~ERROR the `rustc_trivial_field_reads` attribute cannot be used on traits
trait Whatever1 {}

#[rustc_trivial_field_reads] //~ERROR the `rustc_trivial_field_reads` attribute cannot be used on macro defs
macro_rules! number {
() => {
67
};
}

fn main() {
let _ = number!();
}
26 changes: 26 additions & 0 deletions tests/ui/attributes/trivial-field-reads-invalid-targets.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: the `rustc_trivial_field_reads` attribute cannot be used on required trait methods
--> $DIR/trivial-field-reads-invalid-targets.rs:7:7
|
LL | #[rustc_trivial_field_reads]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the `rustc_trivial_field_reads` attribute can only be applied to functions with a body

error: the `rustc_trivial_field_reads` attribute cannot be used on traits
--> $DIR/trivial-field-reads-invalid-targets.rs:11:3
|
LL | #[rustc_trivial_field_reads]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the `rustc_trivial_field_reads` attribute can only be applied to functions

error: the `rustc_trivial_field_reads` attribute cannot be used on macro defs
--> $DIR/trivial-field-reads-invalid-targets.rs:14:3
|
LL | #[rustc_trivial_field_reads]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the `rustc_trivial_field_reads` attribute can only be applied to functions

error: aborting due to 3 previous errors

Loading
Loading