Skip to content

Commit

Permalink
Remove query impl_item_trait_item_ids, fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
mu001999 committed Apr 19, 2024
1 parent e48bbd3 commit 16eb6c2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
26 changes: 0 additions & 26 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,32 +819,6 @@ rustc_queries! {
desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) }
}

/// Maps from associated items on the impl specified by `impl_id`
/// to the corresponding associated item on the trait.
///
/// For example, with the following code
///
/// ```
/// struct Type {}
/// // DefId
/// trait Trait { // trait_id
/// fn f(); // trait_f
/// fn g() {} // trait_g
/// }
///
/// impl Trait for Type { // impl_id
/// fn f() {} // impl_f
/// fn g() {} // impl_g
/// }
/// ```
///
/// The map returned for `tcx.impl_item_trait_item_ids(impl_id)` would be
///`{ impl_f: trait_f, impl_g: trait_g }`
query impl_item_trait_item_ids(impl_id: DefId) -> &'tcx DefIdMap<DefId> {
arena_cache
desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) }
}

/// Given `fn_def_id` of a trait or of an impl that implements a given trait:
/// if `fn_def_id` is the def id of a function defined inside a trait, then it creates and returns
/// the associated items that correspond to each impl trait in return position for that trait.
Expand Down
32 changes: 23 additions & 9 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// expectations such as `#[expect(unused)]` and `#[expect(dead_code)]` is live, and everything else
// is dead.

use hir::def_id::{LocalDefIdMap, LocalDefIdSet};
use hir::def_id::{DefIdMap, LocalDefIdMap, LocalDefIdSet};
use hir::ItemKind;
use rustc_data_structures::unord::UnordSet;
use rustc_errors::MultiSpan;
Expand Down Expand Up @@ -80,6 +80,7 @@ struct MarkSymbolVisitor<'tcx> {
// and the span of their respective impl (i.e., part of the derive
// macro)
ignored_derived_traits: LocalDefIdMap<Vec<(DefId, DefId)>>,
impl_item_trait_item_ids_cache: DefIdMap<DefIdMap<DefId>>,
}

impl<'tcx> MarkSymbolVisitor<'tcx> {
Expand Down Expand Up @@ -490,7 +491,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
let mut ready;
(ready, unsolved_impl_items) =
unsolved_impl_items.into_iter().partition(|&(impl_id, impl_item_id)| {
self.impl_item_with_used_self_and_trait_term(impl_id, impl_item_id)
self.impl_item_with_used_self_and_trait_item(impl_id, impl_item_id)
});

while !ready.is_empty() {
Expand All @@ -500,12 +501,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {

(ready, unsolved_impl_items) =
unsolved_impl_items.into_iter().partition(|&(impl_id, impl_item_id)| {
self.impl_item_with_used_self_and_trait_term(impl_id, impl_item_id)
self.impl_item_with_used_self_and_trait_item(impl_id, impl_item_id)
});
}
}

fn impl_item_with_used_self_and_trait_term(
fn impl_item_with_used_self_and_trait_item(
&mut self,
impl_id: hir::ItemId,
impl_item_id: LocalDefId,
Expand All @@ -520,20 +521,32 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
// for the public method, we don't know the trait item is used or not,
// so we mark the method live if the self is used
return self.live_symbols.contains(&local_def_id);
} else if let Some(trait_item_id) = self
.tcx
.impl_item_trait_item_ids(impl_id.owner_id.to_def_id())
.get(&impl_item_id.to_def_id())
}

let impl_id = impl_id.owner_id.to_def_id();
let impl_item_trait_item_ids = self
.impl_item_trait_item_ids_cache
.entry(impl_id)
.or_insert_with(|| Self::impl_item_trait_item_ids(self.tcx, impl_id));

if let Some(trait_item_id) = impl_item_trait_item_ids.get(&impl_item_id.to_def_id())
&& let Some(local_id) = trait_item_id.as_local()
{
// for the private method, we can know the trait item is ued or not,
// for the private method, we can know the trait item is used or not,
// so we mark the method live if the self is used and the trait item is used
return self.live_symbols.contains(&local_id)
&& self.live_symbols.contains(&local_def_id);
}
}
false
}

fn impl_item_trait_item_ids(tcx: TyCtxt<'tcx>, impl_id: DefId) -> DefIdMap<DefId> {
tcx.associated_items(impl_id)
.in_definition_order()
.filter_map(|item| item.trait_item_def_id.map(|trait_item| (item.def_id, trait_item)))
.collect()
}
}

impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
Expand Down Expand Up @@ -888,6 +901,7 @@ fn live_symbols_and_ignored_derived_traits(
ignore_variant_stack: vec![],
struct_constructors,
ignored_derived_traits: Default::default(),
impl_item_trait_item_ids_cache: Default::default(),
};
symbol_visitor.mark_live_symbols();
symbol_visitor.solve_rest_impl_items(unsolved_impl_items);
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_ty_utils/src/assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub(crate) fn provide(providers: &mut Providers) {
associated_types_for_impl_traits_in_associated_fn,
associated_type_for_impl_trait_in_trait,
impl_item_implementor_ids,
impl_item_trait_item_ids,
..*providers
};
}
Expand Down Expand Up @@ -93,13 +92,6 @@ fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> DefIdMap<DefId>
.collect()
}

fn impl_item_trait_item_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> DefIdMap<DefId> {
tcx.associated_items(impl_id)
.in_definition_order()
.filter_map(|item| item.trait_item_def_id.map(|trait_item| (item.def_id, trait_item)))
.collect()
}

fn associated_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AssocItem {
let id = tcx.local_def_id_to_hir_id(def_id);
let parent_def_id = tcx.hir().get_parent_item(id);
Expand Down

0 comments on commit 16eb6c2

Please sign in to comment.