-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
expanding #[rustc_trivial_field_reads] to more targets, place on Derive and Clone
#160666
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
Open
josetorrs
wants to merge
21
commits into
rust-lang:main
Choose a base branch
from
josetorrs:move-trivial-reads-to-macros
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2516db2
moving trivial field reads attr to macros
josetorrs 2ae71e8
formatting
josetorrs aa42902
moving attribute for Clone
josetorrs b24b06a
adding test
josetorrs e49f2ac
moving attr on Debug, remove from minicore
josetorrs d60acd1
remove temporary changes
josetorrs efc60e4
revert closer to the original check
josetorrs 938133c
checking on impl item
josetorrs 6fa2a60
bless existing tests
josetorrs 32cef99
update code comment
josetorrs d1cc7e1
bless macro stats tests
josetorrs 7196365
blessing deriving all codegen test
josetorrs 7173d6f
expanding attr to impl blocks and refactoring should_explore to not c…
josetorrs 4f4c22a
allow free functions as a target, move find_attr check eagerly in vis…
josetorrs e496b2d
expanding target to traits with a default method also moving and upda…
josetorrs 3f535a8
adding invalid targets tests, rename introduced dead code tests
josetorrs 009892d
remove old named tests
josetorrs 7f12f2f
formatting
josetorrs 95eef6e
move invalid targets test to attributes tests directory
josetorrs a0e5d96
forget to bless tests after move and rename
josetorrs 8112d19
restoring previous commit change, putting attr lookup in should_explore
josetorrs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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), | ||
|
|
||
| DefKind::TyParam | ||
| | DefKind::ConstParam | ||
|
|
@@ -432,17 +432,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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This attribute check is redundant now, right? |
||
| { | ||
| 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() | ||
| { | ||
|
|
@@ -451,7 +450,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { | |
| return true; | ||
| } | ||
|
|
||
| false | ||
| return false; | ||
| } | ||
|
|
||
| fn visit_node( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/ui/attributes/trivial-field-reads-invalid-targets.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
26
tests/ui/attributes/trivial-field-reads-invalid-targets.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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::FnonlyView changes since the review