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_impl<F: FnMut(DefId)>(self, trait_def_id: DefId, mut f: F) {
134 let impls = self.trait_impls_of(trait_def_id);
135
136 for &impl_def_id in impls.blanket_impls.iter() {
137 f(impl_def_id);
138 }
139
140 for v in impls.non_blanket_impls.values() {
141 for &impl_def_id in v {
142 f(impl_def_id);
143 }
144 }
145 }
146
147 pub fn for_each_relevant_impl(
151 self,
152 trait_def_id: DefId,
153 self_ty: Ty<'tcx>,
154 mut f: impl FnMut(DefId),
155 ) {
156 let impls = self.trait_impls_of(trait_def_id);
162
163 for &impl_def_id in impls.blanket_impls.iter() {
164 f(impl_def_id);
165 }
166
167 if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsRigid) {
174 if let Some(impls) = impls.non_blanket_impls.get(&simp) {
175 for &impl_def_id in impls {
176 f(impl_def_id);
177 }
178 }
179 } else {
180 for &impl_def_id in impls.non_blanket_impls.values().flatten() {
181 f(impl_def_id);
182 }
183 }
184 }
185
186 pub fn non_blanket_impls_for_ty(
188 self,
189 trait_def_id: DefId,
190 self_ty: Ty<'tcx>,
191 ) -> impl Iterator<Item = DefId> + 'tcx {
192 let impls = self.trait_impls_of(trait_def_id);
193 if let Some(simp) =
194 fast_reject::simplify_type(self, self_ty, TreatParams::InstantiateWithInfer)
195 {
196 if let Some(impls) = impls.non_blanket_impls.get(&simp) {
197 return impls.iter().copied();
198 }
199 }
200
201 [].iter().copied()
202 }
203
204 pub fn all_impls(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> + 'tcx {
208 let TraitImpls { blanket_impls, non_blanket_impls } = self.trait_impls_of(trait_def_id);
209
210 blanket_impls.iter().chain(non_blanket_impls.iter().flat_map(|(_, v)| v)).cloned()
211 }
212}
213
214pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> TraitImpls {
216 let mut impls = TraitImpls::default();
217
218 if !trait_id.is_local() {
221 for &cnum in tcx.crates(()).iter() {
222 for &(impl_def_id, simplified_self_ty) in
223 tcx.implementations_of_trait((cnum, trait_id)).iter()
224 {
225 if let Some(simplified_self_ty) = simplified_self_ty {
226 impls
227 .non_blanket_impls
228 .entry(simplified_self_ty)
229 .or_default()
230 .push(impl_def_id);
231 } else {
232 impls.blanket_impls.push(impl_def_id);
233 }
234 }
235 }
236 }
237
238 for &impl_def_id in tcx.hir().trait_impls(trait_id) {
239 let impl_def_id = impl_def_id.to_def_id();
240
241 let impl_self_ty = tcx.type_of(impl_def_id).instantiate_identity();
242
243 if let Some(simplified_self_ty) =
244 fast_reject::simplify_type(tcx, impl_self_ty, TreatParams::InstantiateWithInfer)
245 {
246 impls.non_blanket_impls.entry(simplified_self_ty).or_default().push(impl_def_id);
247 } else {
248 impls.blanket_impls.push(impl_def_id);
249 }
250 }
251
252 impls
253}
254
255pub(super) fn incoherent_impls_provider(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
257 let mut impls = Vec::new();
258 for cnum in iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) {
259 for &impl_def_id in tcx.crate_incoherent_impls((cnum, simp)) {
260 impls.push(impl_def_id)
261 }
262 }
263 debug!(?impls);
264
265 tcx.arena.alloc_slice(&impls)
266}
267
268pub(super) fn traits_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> &[DefId] {
269 let mut traits = Vec::new();
270 for id in tcx.hir().items() {
271 if matches!(tcx.def_kind(id.owner_id), DefKind::Trait | DefKind::TraitAlias) {
272 traits.push(id.owner_id.to_def_id())
273 }
274 }
275
276 tcx.arena.alloc_slice(&traits)
277}
278
279pub(super) fn trait_impls_in_crate_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> &[DefId] {
280 let mut trait_impls = Vec::new();
281 for id in tcx.hir().items() {
282 if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. })
283 && tcx.impl_trait_ref(id.owner_id).is_some()
284 {
285 trait_impls.push(id.owner_id.to_def_id())
286 }
287 }
288
289 tcx.arena.alloc_slice(&trait_impls)
290}