1 use crate::hir::map::DefPathHash;
2 use crate::ich::{self, StableHashingContext};
3 use crate::traits::specialization_graph;
4 use crate::ty::fast_reject;
5 use crate::ty::fold::TypeFoldable;
6 use crate::ty::{Ty, TyCtxt};
8 use rustc_hir::def_id::DefId;
10 use rustc_data_structures::fx::FxHashMap;
11 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
12 use rustc_macros::HashStable;
14 /// A trait's definition with type information.
17 // We already have the def_path_hash below, no need to hash it twice
18 #[stable_hasher(ignore)]
21 pub unsafety: hir::Unsafety,
23 /// If `true`, then this trait had the `#[rustc_paren_sugar]`
24 /// attribute, indicating that it should be used with `Foo()`
25 /// sugar. This is a temporary thing -- eventually any trait will
26 /// be usable with the sugar (or without it).
27 pub paren_sugar: bool,
29 pub has_auto_impl: bool,
31 /// If `true`, then this trait has the `#[marker]` attribute, indicating
32 /// that all its associated items have defaults that cannot be overridden,
33 /// and thus `impl`s of it are allowed to overlap.
36 /// The ICH of this trait's DefPath, cached here so it doesn't have to be
37 /// recomputed all the time.
38 pub def_path_hash: DefPathHash,
42 pub struct TraitImpls {
43 blanket_impls: Vec<DefId>,
44 /// Impls indexed by their simplified self type, for fast lookup.
45 non_blanket_impls: FxHashMap<fast_reject::SimplifiedType, Vec<DefId>>,
51 unsafety: hir::Unsafety,
55 def_path_hash: DefPathHash,
57 TraitDef { def_id, unsafety, paren_sugar, has_auto_impl, is_marker, def_path_hash }
64 ) -> specialization_graph::Ancestors<'tcx> {
65 specialization_graph::ancestors(tcx, self.def_id, of_impl)
69 impl<'tcx> TyCtxt<'tcx> {
70 pub fn for_each_impl<F: FnMut(DefId)>(self, def_id: DefId, mut f: F) {
71 let impls = self.trait_impls_of(def_id);
73 for &impl_def_id in impls.blanket_impls.iter() {
77 for v in impls.non_blanket_impls.values() {
78 for &impl_def_id in v {
84 /// Iterate over every impl that could possibly match the
85 /// self type `self_ty`.
86 pub fn for_each_relevant_impl<F: FnMut(DefId)>(
92 let impls = self.trait_impls_of(def_id);
94 for &impl_def_id in impls.blanket_impls.iter() {
98 // simplify_type(.., false) basically replaces type parameters and
99 // projections with infer-variables. This is, of course, done on
100 // the impl trait-ref when it is instantiated, but not on the
101 // predicate trait-ref which is passed here.
103 // for example, if we match `S: Copy` against an impl like
104 // `impl<T:Copy> Copy for Option<T>`, we replace the type variable
105 // in `Option<T>` with an infer variable, to `Option<_>` (this
106 // doesn't actually change fast_reject output), but we don't
107 // replace `S` with anything - this impl of course can't be
108 // selected, and as there are hundreds of similar impls,
109 // considering them would significantly harm performance.
111 // This depends on the set of all impls for the trait. That is
112 // unfortunate. When we get red-green recompilation, we would like
113 // to have a way of knowing whether the set of relevant impls
114 // changed. The most naive
115 // way would be to compute the Vec of relevant impls and see whether
116 // it differs between compilations. That shouldn't be too slow by
117 // itself - we do quite a bit of work for each relevant impl anyway.
119 // If we want to be faster, we could have separate queries for
120 // blanket and non-blanket impls, and compare them separately.
122 // I think we'll cross that bridge when we get to it.
123 if let Some(simp) = fast_reject::simplify_type(self, self_ty, true) {
124 if let Some(impls) = impls.non_blanket_impls.get(&simp) {
125 for &impl_def_id in impls {
130 for &impl_def_id in impls.non_blanket_impls.values().flatten() {
136 /// Returns a vector containing all impls
137 pub fn all_impls(self, def_id: DefId) -> Vec<DefId> {
138 let impls = self.trait_impls_of(def_id);
143 .chain(impls.non_blanket_impls.values().flatten())
149 // Query provider for `trait_impls_of`.
150 pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> &TraitImpls {
151 let mut impls = TraitImpls::default();
154 let mut add_impl = |impl_def_id| {
155 let impl_self_ty = tcx.type_of(impl_def_id);
156 if impl_def_id.is_local() && impl_self_ty.references_error() {
160 if let Some(simplified_self_ty) = fast_reject::simplify_type(tcx, impl_self_ty, false) {
161 impls.non_blanket_impls.entry(simplified_self_ty).or_default().push(impl_def_id);
163 impls.blanket_impls.push(impl_def_id);
167 // Traits defined in the current crate can't have impls in upstream
168 // crates, so we don't bother querying the cstore.
169 if !trait_id.is_local() {
170 for &cnum in tcx.crates().iter() {
171 for &def_id in tcx.implementations_of_trait((cnum, trait_id)).iter() {
177 for &hir_id in tcx.hir().trait_impls(trait_id) {
178 add_impl(tcx.hir().local_def_id(hir_id));
182 tcx.arena.alloc(impls)
185 impl<'a> HashStable<StableHashingContext<'a>> for TraitImpls {
186 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
187 let TraitImpls { ref blanket_impls, ref non_blanket_impls } = *self;
189 ich::hash_stable_trait_impls(hcx, hasher, blanket_impls, non_blanket_impls);