1use std::marker::PhantomData;
2
3use smallvec::smallvec;
4
5use crate::data_structures::HashSet;
6use crate::inherent::*;
7use crate::lang_items::TraitSolverLangItem;
8use crate::outlives::{Component, push_outlives_components};
9use crate::{self as ty, Interner, Upcast as _};
10
11pub struct Elaborator<I: Interner, O> {
18 cx: I,
19 stack: Vec<O>,
20 visited: HashSet<ty::Binder<I, ty::PredicateKind<I>>>,
21 mode: Filter,
22 elaborate_sized: ElaborateSized,
23}
24
25enum Filter {
26 All,
27 OnlySelf,
28}
29
30#[derive(Eq, PartialEq)]
31enum ElaborateSized {
32 Yes,
33 No,
34}
35
36pub trait Elaboratable<I: Interner> {
38 fn predicate(&self) -> I::Predicate;
39
40 fn child(&self, clause: I::Clause) -> Self;
42
43 fn child_with_derived_cause(
46 &self,
47 clause: I::Clause,
48 span: I::Span,
49 parent_trait_pred: ty::Binder<I, ty::TraitPredicate<I>>,
50 index: usize,
51 ) -> Self;
52}
53
54pub struct ClauseWithSupertraitSpan<I: Interner> {
55 pub clause: I::Clause,
56 pub supertrait_span: I::Span,
58}
59impl<I: Interner> ClauseWithSupertraitSpan<I> {
60 pub fn new(clause: I::Clause, span: I::Span) -> Self {
61 ClauseWithSupertraitSpan { clause, supertrait_span: span }
62 }
63}
64impl<I: Interner> Elaboratable<I> for ClauseWithSupertraitSpan<I> {
65 fn predicate(&self) -> <I as Interner>::Predicate {
66 self.clause.as_predicate()
67 }
68
69 fn child(&self, clause: <I as Interner>::Clause) -> Self {
70 ClauseWithSupertraitSpan { clause, supertrait_span: self.supertrait_span }
71 }
72
73 fn child_with_derived_cause(
74 &self,
75 clause: <I as Interner>::Clause,
76 supertrait_span: <I as Interner>::Span,
77 _parent_trait_pred: crate::Binder<I, crate::TraitPredicate<I>>,
78 _index: usize,
79 ) -> Self {
80 ClauseWithSupertraitSpan { clause, supertrait_span }
81 }
82}
83
84pub fn elaborate<I: Interner, O: Elaboratable<I>>(
85 cx: I,
86 obligations: impl IntoIterator<Item = O>,
87) -> Elaborator<I, O> {
88 let mut elaborator = Elaborator {
89 cx,
90 stack: Vec::new(),
91 visited: HashSet::default(),
92 mode: Filter::All,
93 elaborate_sized: ElaborateSized::No,
94 };
95 elaborator.extend_deduped(obligations);
96 elaborator
97}
98
99impl<I: Interner, O: Elaboratable<I>> Elaborator<I, O> {
100 fn extend_deduped(&mut self, obligations: impl IntoIterator<Item = O>) {
102 self.stack.extend(
107 obligations.into_iter().filter(|o| {
108 self.visited.insert(self.cx.anonymize_bound_vars(o.predicate().kind()))
109 }),
110 );
111 }
112
113 pub fn filter_only_self(mut self) -> Self {
116 self.mode = Filter::OnlySelf;
117 self
118 }
119
120 pub fn elaborate_sized(mut self) -> Self {
123 self.elaborate_sized = ElaborateSized::Yes;
124 self
125 }
126
127 fn elaborate(&mut self, elaboratable: &O) {
128 let cx = self.cx;
129
130 let Some(clause) = elaboratable.predicate().as_clause() else {
132 return;
133 };
134
135 if self.elaborate_sized == ElaborateSized::No
142 && let Some(did) = clause.as_trait_clause().map(|c| c.def_id())
143 && self.cx.is_lang_item(did, TraitSolverLangItem::Sized)
144 {
145 return;
146 }
147
148 let bound_clause = clause.kind();
149 match bound_clause.skip_binder() {
150 ty::ClauseKind::Trait(data) => {
151 if data.polarity != ty::PredicatePolarity::Positive {
153 return;
154 }
155
156 let map_to_child_clause =
157 |(index, (clause, span)): (usize, (I::Clause, I::Span))| {
158 elaboratable.child_with_derived_cause(
159 clause.instantiate_supertrait(cx, bound_clause.rebind(data.trait_ref)),
160 span,
161 bound_clause.rebind(data),
162 index,
163 )
164 };
165
166 match self.mode {
168 Filter::All => self.extend_deduped(
169 cx.explicit_implied_predicates_of(data.def_id())
170 .iter_identity()
171 .enumerate()
172 .map(map_to_child_clause),
173 ),
174 Filter::OnlySelf => self.extend_deduped(
175 cx.explicit_super_predicates_of(data.def_id())
176 .iter_identity()
177 .enumerate()
178 .map(map_to_child_clause),
179 ),
180 };
181 }
182 ty::ClauseKind::HostEffect(data) => self.extend_deduped(
184 cx.explicit_implied_const_bounds(data.def_id()).iter_identity().map(|trait_ref| {
185 elaboratable.child(
186 trait_ref
187 .to_host_effect_clause(cx, data.constness)
188 .instantiate_supertrait(cx, bound_clause.rebind(data.trait_ref)),
189 )
190 }),
191 ),
192 ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => {
193 if r_min.is_bound() {
208 return;
209 }
210
211 let mut components = smallvec![];
212 push_outlives_components(cx, ty_max, &mut components);
213 self.extend_deduped(
214 components
215 .into_iter()
216 .filter_map(|component| elaborate_component_to_clause(cx, component, r_min))
217 .map(|clause| elaboratable.child(bound_clause.rebind(clause).upcast(cx))),
218 );
219 }
220 ty::ClauseKind::RegionOutlives(..) => {
221 }
223 ty::ClauseKind::WellFormed(..) => {
224 }
227 ty::ClauseKind::Projection(..) => {
228 }
230 ty::ClauseKind::ConstEvaluatable(..) => {
231 }
234 ty::ClauseKind::ConstArgHasType(..) => {
235 }
237 ty::ClauseKind::UnstableFeature(_) => {
238 }
240 }
241 }
242}
243
244fn elaborate_component_to_clause<I: Interner>(
245 cx: I,
246 component: Component<I>,
247 outlives_region: I::Region,
248) -> Option<ty::ClauseKind<I>> {
249 match component {
250 Component::Region(r) => {
251 if r.is_bound() {
252 None
253 } else {
254 Some(ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(r, outlives_region)))
255 }
256 }
257
258 Component::Param(p) => {
259 let ty = Ty::new_param(cx, p);
260 Some(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, outlives_region)))
261 }
262
263 Component::Placeholder(p) => {
264 let ty = Ty::new_placeholder(cx, p);
265 Some(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, outlives_region)))
266 }
267
268 Component::UnresolvedInferenceVariable(_) => None,
269
270 Component::Alias(alias_ty) => {
271 Some(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
274 alias_ty.to_ty(cx),
275 outlives_region,
276 )))
277 }
278
279 Component::EscapingAlias(_) => {
280 None
283 }
284 }
285}
286
287impl<I: Interner, O: Elaboratable<I>> Iterator for Elaborator<I, O> {
288 type Item = O;
289
290 fn size_hint(&self) -> (usize, Option<usize>) {
291 (self.stack.len(), None)
292 }
293
294 fn next(&mut self) -> Option<Self::Item> {
295 if let Some(obligation) = self.stack.pop() {
297 self.elaborate(&obligation);
298 Some(obligation)
299 } else {
300 None
301 }
302 }
303}
304
305pub fn supertrait_def_ids<I: Interner>(
314 cx: I,
315 trait_def_id: I::DefId,
316) -> impl Iterator<Item = I::DefId> {
317 let mut set = HashSet::default();
318 let mut stack = vec![trait_def_id];
319
320 set.insert(trait_def_id);
321
322 std::iter::from_fn(move || {
323 let trait_def_id = stack.pop()?;
324
325 for (predicate, _) in cx.explicit_super_predicates_of(trait_def_id).iter_identity() {
326 if let ty::ClauseKind::Trait(data) = predicate.kind().skip_binder()
327 && set.insert(data.def_id())
328 {
329 stack.push(data.def_id());
330 }
331 }
332
333 Some(trait_def_id)
334 })
335}
336
337pub fn supertraits<I: Interner>(
338 cx: I,
339 trait_ref: ty::Binder<I, ty::TraitRef<I>>,
340) -> FilterToTraits<I, Elaborator<I, I::Clause>> {
341 elaborate(cx, [trait_ref.upcast(cx)]).filter_only_self().filter_to_traits()
342}
343
344impl<I: Interner> Elaborator<I, I::Clause> {
345 fn filter_to_traits(self) -> FilterToTraits<I, Self> {
346 FilterToTraits { _cx: PhantomData, base_iterator: self }
347 }
348}
349
350pub struct FilterToTraits<I: Interner, It: Iterator<Item = I::Clause>> {
353 _cx: PhantomData<I>,
354 base_iterator: It,
355}
356
357impl<I: Interner, It: Iterator<Item = I::Clause>> Iterator for FilterToTraits<I, It> {
358 type Item = ty::Binder<I, ty::TraitRef<I>>;
359
360 fn next(&mut self) -> Option<ty::Binder<I, ty::TraitRef<I>>> {
361 while let Some(pred) = self.base_iterator.next() {
362 if let Some(data) = pred.as_trait_clause() {
363 return Some(data.map_bound(|t| t.trait_ref));
364 }
365 }
366 None
367 }
368
369 fn size_hint(&self) -> (usize, Option<usize>) {
370 let (_, upper) = self.base_iterator.size_hint();
371 (0, upper)
372 }
373}
374
375pub fn elaborate_outlives_assumptions<I: Interner>(
376 cx: I,
377 assumptions: impl IntoIterator<Item = ty::OutlivesPredicate<I, I::GenericArg>>,
378) -> HashSet<ty::OutlivesPredicate<I, I::GenericArg>> {
379 let mut collected = HashSet::default();
380
381 for ty::OutlivesPredicate(arg1, r2) in assumptions {
382 collected.insert(ty::OutlivesPredicate(arg1, r2));
383 match arg1.kind() {
384 ty::GenericArgKind::Type(ty1) => {
387 let mut components = smallvec![];
388 push_outlives_components(cx, ty1, &mut components);
389 for c in components {
390 match c {
391 Component::Region(r1) => {
392 if !r1.is_bound() {
393 collected.insert(ty::OutlivesPredicate(r1.into(), r2));
394 }
395 }
396
397 Component::Param(p) => {
398 let ty = Ty::new_param(cx, p);
399 collected.insert(ty::OutlivesPredicate(ty.into(), r2));
400 }
401
402 Component::Placeholder(p) => {
403 let ty = Ty::new_placeholder(cx, p);
404 collected.insert(ty::OutlivesPredicate(ty.into(), r2));
405 }
406
407 Component::Alias(alias_ty) => {
408 collected.insert(ty::OutlivesPredicate(alias_ty.to_ty(cx).into(), r2));
409 }
410
411 Component::UnresolvedInferenceVariable(_) | Component::EscapingAlias(_) => {
412 }
413 }
414 }
415 }
416 ty::GenericArgKind::Lifetime(_) => {}
418 ty::GenericArgKind::Const(_) => {}
420 }
421 }
422
423 collected
424}