Skip to content

perf: only check for rustc_trivial_field_reads attribute on traits, not items, impls, etc. #89454

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 2 commits into from
Oct 7, 2021
Merged
Changes from all commits
Commits
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
42 changes: 4 additions & 38 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,46 +243,15 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
/// will be ignored for the purposes of dead code analysis (see PR #85200
/// for discussion).
fn should_ignore_item(&self, def_id: DefId) -> bool {
if !self.tcx.has_attr(def_id, sym::automatically_derived)
&& !self
.tcx
.impl_of_method(def_id)
.map_or(false, |impl_id| self.tcx.has_attr(impl_id, sym::automatically_derived))
{
return false;
}

let has_attr = |def_id| self.tcx.has_attr(def_id, sym::rustc_trivial_field_reads);

if has_attr(def_id) {
return true;
}

if let Some(impl_of) = self.tcx.impl_of_method(def_id) {
if has_attr(impl_of) {
return true;
if !self.tcx.has_attr(impl_of, sym::automatically_derived) {
return false;
}

if let Some(trait_of) = self.tcx.trait_id_of_impl(impl_of) {
if has_attr(trait_of) {
if self.tcx.has_attr(trait_of, sym::rustc_trivial_field_reads) {
return true;
}

if let Some(method_ident) = self.tcx.opt_item_name(def_id) {
if let Some(trait_method) = self
.tcx
.associated_items(trait_of)
.find_by_name_and_kind(self.tcx, method_ident, ty::AssocKind::Fn, trait_of)
{
if has_attr(trait_method.def_id) {
return true;
}
}
}
}
} else if let Some(trait_of) = self.tcx.trait_of_item(def_id) {
if has_attr(trait_of) {
return true;
}
}

Expand All @@ -291,10 +260,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {

fn visit_node(&mut self, node: Node<'tcx>) {
if let Some(item_def_id) = match node {
Node::Item(hir::Item { def_id, .. })
| Node::ForeignItem(hir::ForeignItem { def_id, .. })
| Node::TraitItem(hir::TraitItem { def_id, .. })
| Node::ImplItem(hir::ImplItem { def_id, .. }) => Some(def_id.to_def_id()),
Node::ImplItem(hir::ImplItem { def_id, .. }) => Some(def_id.to_def_id()),
_ => None,
} {
if self.should_ignore_item(item_def_id) {
Expand Down