rustc_middle/ty/
trait_def.rs1use std::iter;
2
3use rustc_data_structures::fx::FxIndexMap;
4use rustc_errors::ErrorGuaranteed;
5use rustc_hir as hir;
6use rustc_hir::def::DefKind;
7use rustc_hir::def_id::{DefId, LOCAL_CRATE};
8use rustc_macros::{Decodable, Encodable, HashStable};
9use tracing::debug;
10
11use crate::query::LocalCrate;
12use crate::traits::specialization_graph;
13use crate::ty::fast_reject::{self, SimplifiedType, TreatParams};
14use crate::ty::{Ident, Ty, TyCtxt};
15
16#[derive(HashStable, Encodable, Decodable)]
18pub struct TraitDef {
19 pub def_id: DefId,
20
21 pub safety: hir::Safety,
22
23 pub constness: hir::Constness,
25
26 pub paren_sugar: bool,
31
32 pub has_auto_impl: bool,
33
34 pub is_marker: bool,
38
39 pub is_coinductive: bool,
47
48 pub is_fundamental: bool,
52
53 pub skip_array_during_method_dispatch: bool,
57
58 pub skip_boxed_slice_during_method_dispatch: bool,
62
63 pub specialization_kind: TraitSpecializationKind,
66
67 pub must_implement_one_of: Option<Box<[Ident]>>,
70
71 pub implement_via_object: bool,
75
76 pub deny_explicit_impl: bool,
80}
81
82#[derive(HashStable, PartialEq, Clone, Copy, Encodable, Decodable)]
85pub enum TraitSpecializationKind {
86 None,
88 Marker,
93 AlwaysApplicable,
98}
99
100#[derive(Default, Debug, HashStable)]
101pub struct TraitImpls {
102 blanket_impls: Vec<DefId>,
103 non_blanket_impls: FxIndexMap<SimplifiedType, Vec<DefId>>,
105}
106
107impl TraitImpls {
108 pub fn is_empty(&self) -> bool {
109 self.blanket_impls.is_empty() && self.non_blanket_impls.is_empty()
110 }
111
112 pub fn blanket_impls(&self) -> &[DefId] {
113 self.blanket_impls.as_slice()
114 }
115
116 pub fn non_blanket_impls(&self) -> &FxIndexMap<SimplifiedType, Vec<DefId>> {
117 &self.non_blanket_impls
118 }
119}
120
121impl<'tcx> TraitDef {
122 pub fn ancestors(
123 &self,
124 tcx: TyCtxt<'tcx>,
125 of_impl: DefId,
126 ) -> Result<specialization_graph::Ancestors<'tcx>, ErrorGuaranteed> {
127 specialization_graph::ancestors(tcx, self.def_id, of_impl)
128 }
129}
130
131impl<'tcx> TyCtxt<'tcx> {
132 pub fn for_each_relevant_impl(
136 self,
137 trait_def_id: DefId,
138 self_ty: Ty<'tcx>,
139 mut f: impl FnMut(DefId),
140 ) {
141 let impls = self.trait_impls_of(trait_def_id);
147
148 for &impl_def_id in impls.blanket_impls.iter() {
149 f(impl_def_id);
150 }
151
152 if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsRigid) {
159 if let Some(impls) = impls.non_blanket_impls.get(&simp) {
160 for &impl_def_id in impls {
161 f(impl_def_id);
162 }
163 }
164 } else {
165 for &impl_def_id in impls.non_blanket_impls.values().flatten() {
166 f(impl_def_id);
167 }
168 }
169 }
170
171 pub fn non_blanket_impls_for_ty(
173 self,
174 trait_def_id: DefId,
175 self_ty: Ty<'tcx>,
176 ) -> impl Iterator<Item = DefId> {
177 let impls = self.trait_impls_of(trait_def_id);
178 if let Some(simp) =
179 fast_reject::simplify_type(self, self_ty, TreatParams::InstantiateWithInfer)
180 {
181 if let Some(impls) = impls.non_blanket_impls.get(&simp) {
182 return impls.iter().copied();
183 }
184 }
185
186 [].iter().copied()
187 }
188
189 pub fn all_impls(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> {
193 let TraitImpls { blanket_impls, non_blanket_impls } = self.trait_impls_of(trait_def_id);
194
195 blanket_impls.iter().chain(non_blanket_impls.iter().flat_map(|(_, v)| v)).cloned()
196 }
197}
198
199pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> TraitImpls {
201 let mut impls = TraitImpls::default();
202
203 if !trait_id.is_local() {
206 for &cnum in tcx.crates(()).iter() {
207 for &(impl_def_id, simplified_self_ty) in
208 tcx.implementations_of_trait((cnum, trait_id)).iter()
209 {
210 if let Some(simplified_self_ty) = simplified_self_ty {
211 impls
212 .non_blanket_impls
213 .entry(simplified_self_ty)
214 .or_default()
215 .push(impl_def_id);
216 } else {
217 impls.blanket_impls.push(impl_def_id);
218 }
219 }
220 }
221 }
222
223 for &impl_def_id in tcx.local_trait_impls(trait_id) {
224 let impl_def_id = impl_def_id.to_def_id();
225
226 let impl_self_ty = tcx.type_of(impl_def_id).instantiate_identity();
227
228 if let Some(simplified_self_ty) =
229 fast_reject::simplify_type(tcx, impl_self_ty, TreatParams::InstantiateWithInfer)
230 {
231 impls.non_blanket_impls.entry(simplified_self_ty).or_default().push(impl_def_id);
232 } else {
233 impls.blanket_impls.push(impl_def_id);
234 }
235 }
236
237 impls
238}
239
240pub(super) fn incoherent_impls_provider(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
242 let mut impls = Vec::new();
243 for cnum in iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) {
244 for &impl_def_id in tcx.crate_incoherent_impls((cnum, simp)) {
245 impls.push(impl_def_id)
246 }
247 }
248 debug!(?impls);
249
250 tcx.arena.alloc_slice(&impls)
251}
252
253pub(super) fn traits_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> &[DefId] {
254 let mut traits = Vec::new();
255 for id in tcx.hir_free_items() {
256 if matches!(tcx.def_kind(id.owner_id), DefKind::Trait | DefKind::TraitAlias) {
257 traits.push(id.owner_id.to_def_id())
258 }
259 }
260
261 tcx.arena.alloc_slice(&traits)
262}
263
264pub(super) fn trait_impls_in_crate_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> &[DefId] {
265 let mut trait_impls = Vec::new();
266 for id in tcx.hir_free_items() {
267 if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. })
268 && tcx.impl_trait_ref(id.owner_id).is_some()
269 {
270 trait_impls.push(id.owner_id.to_def_id())
271 }
272 }
273
274 tcx.arena.alloc_slice(&trait_impls)
275}