Skip to main content

rustc_type_ir/
param_env.rs

1use std::iter;
2use std::marker::PhantomData;
3
4pub use clause_filter::NoClauses;
5use clause_filter::*;
6use either::Either;
7pub use iterator::CallerBoundsIterator;
8use output_as::*;
9use output_filter::*;
10use polarity_filter::*;
11use self_ty_filter::*;
12use trait_filter::*;
13
14use crate::inherent::*;
15use crate::lang_items::SolverTraitLangItem;
16use crate::{
17    Binder, ClauseKind, ClausePolarity, CompressedTraitRefs, Interner, TraitClause, Upcast,
18};
19
20#[must_use]
21pub struct CallerBoundsAccessor<I, Iter, CF>
22where
23    I: Interner,
24    Iter: Iterator<Item = I::Clause>,
25    CF: ClauseFilter,
26{
27    clauses: Iter,
28    clause_filter: CF,
29    cx: PhantomData<I>,
30}
31
32impl<I, Iter> CallerBoundsAccessor<I, Iter, NoClauses>
33where
34    I: Interner,
35    Iter: Iterator<Item = I::Clause>,
36{
37    #[inline]
38    pub fn new(clauses: Iter) -> Self {
39        Self { clauses, clause_filter: NoClauses, cx: PhantomData }
40    }
41}
42
43mod iterator {
44    use super::*;
45
46    pub trait CallerBoundsIterator<I, Iter>
47    where
48        I: Interner,
49        Iter: Iterator<Item = I::Clause>,
50    {
51        type Item;
52        /// Note: likely less efficient than staying in `CallerBoundsIterator` land.
53        fn iter(self) -> impl Iterator<Item = Self::Item>;
54    }
55    impl<I, Iter> CallerBoundsIterator<I, Iter> for CallerBoundsAccessor<I, Iter, NonTraitClauses>
56    where
57        I: Interner,
58        Iter: Iterator<Item = I::Clause>,
59    {
60        type Item = I::Clause;
61
62        #[inline]
63        fn iter(self) -> impl Iterator<Item = Self::Item> {
64            self.clauses.filter(|&i| {
65                !#[allow(non_exhaustive_omitted_patterns)] match i.kind().skip_binder() {
    ClauseKind::Trait(_) | ClauseKind::CompressedTrait(_) => true,
    _ => false,
}matches!(
66                    i.kind().skip_binder(),
67                    ClauseKind::Trait(_) | ClauseKind::CompressedTrait(_)
68                )
69            })
70        }
71    }
72
73    impl<I, Iter> CallerBoundsIterator<I, Iter> for CallerBoundsAccessor<I, Iter, AllClauses<I>>
74    where
75        I: Interner,
76        Iter: Iterator<Item = I::Clause>,
77    {
78        type Item = Binder<I, ClauseKind<I>>;
79
80        #[inline]
81        fn iter(self) -> impl Iterator<Item = Self::Item> {
82            let cx = self.clause_filter.0;
83            self.clauses.flat_map(move |clause| match clause.kind().skip_binder() {
84                // Reconstruct clauses from compressed trait clauses.
85                ClauseKind::CompressedTrait(ct) => {
86                    Either::Left(ct.compressed_trait_refs.iter().map(move |c| {
87                        c.trait_clause_from_single_bit(cx, ct.self_ty).map_bound(ClauseKind::Trait)
88                    }))
89                }
90                _ => Either::Right(iter::once(clause.kind())),
91            })
92        }
93    }
94
95    impl<I, Iter> CallerBoundsIterator<I, Iter> for CallerBoundsAccessor<I, Iter, AllClausesInterned<I>>
96    where
97        I: Interner,
98        Iter: Iterator<Item = I::Clause>,
99    {
100        type Item = I::Clause;
101
102        #[inline]
103        fn iter(self) -> impl Iterator<Item = Self::Item> {
104            let cx = self.clause_filter.0;
105            self.clauses.flat_map(move |clause| match clause.kind().skip_binder() {
106                // Reconstruct clauses from compressed trait clauses.
107                ClauseKind::CompressedTrait(ct) => Either::Left(
108                    ct.compressed_trait_refs
109                        .iter()
110                        .map(move |c| c.trait_clause_from_single_bit(cx, ct.self_ty).upcast(cx)),
111                ),
112                _ => Either::Right(iter::once(clause)),
113            })
114        }
115    }
116
117    #[inline]
118    fn filter_trait_clause_helper<I, P, S, T, F, O, X>(
119        filter: &TraitClauses<I, P, S, T, F, O>,
120        clause: TraitClause<I>,
121        bound: Binder<I, X>,
122    ) -> bool
123    where
124        I: Interner,
125        P: PolarityFilter,
126        S: SelfTyFilter<I>,
127        T: TraitFilter<I>,
128        F: OutputFilter<I>,
129        O: OutputAs<I>,
130    {
131        let TraitClauses {
132            cx: _,
133            polarity_filter,
134            self_type_filter,
135            trait_filter,
136            output_filter,
137            output: _,
138        } = filter;
139        polarity_filter.allow_polarity(clause.polarity)
140            && self_type_filter.allow_self_ty(clause.self_ty())
141            && trait_filter.allow_clause(clause)
142            && output_filter.allow_trait_clause(bound.rebind(clause))
143    }
144
145    #[inline]
146    fn filter_compressed_trait_helper<I, T, F>(
147        trait_filter: &T,
148        output_filter: &F,
149        cx: I,
150        self_ty: I::GenericArgs,
151        trait_ref_single_bit: CompressedTraitRefs,
152    ) -> Option<Binder<I, TraitClause<I>>>
153    where
154        I: Interner,
155        T: TraitFilter<I>,
156        F: OutputFilter<I>,
157    {
158        if let Some(def_id) =
159            trait_filter.allow_compressed_trait_ref_single_bit(cx, trait_ref_single_bit)
160        {
161            // Only if all filtering allows this compressed clause do we reconstruct the trait clause,
162            // but we don't intern it yet, since that depends on the output transformation. If we can
163            // avoid re-interning, that's worth a *lot*.
164            Some(CompressedTraitRefs::trait_clause_from_def_id(cx, self_ty, def_id))
165                .filter(|i| output_filter.allow_trait_clause(*i))
166        } else {
167            None
168        }
169    }
170
171    struct CallerBoundsIteratorImpl<I, Iter, CF>
172    where
173        I: Interner,
174        Iter: Iterator<Item = I::Clause>,
175        CF: ClauseFilter,
176    {
177        accessor: CallerBoundsAccessor<I, Iter, CF>,
178        decompression_in_progress: CompressedTraitRefs,
179        // self ty of the currently decompression-in-progress trait ref,
180        // if applicable.
181        self_ty: Option<I::GenericArgs>,
182    }
183
184    impl<I, Iter, P, S, T, F, O> Iterator
185        for CallerBoundsIteratorImpl<I, Iter, TraitClauses<I, P, S, T, F, O>>
186    where
187        I: Interner,
188        Iter: Iterator<Item = I::Clause>,
189        P: PolarityFilter,
190        S: SelfTyFilter<I>,
191        T: TraitFilter<I>,
192        F: OutputFilter<I>,
193        O: OutputAs<I>,
194    {
195        type Item = O::Output;
196
197        fn next(&mut self) -> Option<Self::Item> {
198            let CallerBoundsAccessor { clauses, clause_filter: filter, cx: _ } = &mut self.accessor;
199            let cx = filter.cx;
200
201            // This loop finds the next item in `clauses` that can be returned.
202            // If `clauses` is empty, we bail out of the function inside the loop,
203            // otherwise the next non-filtered element is returned using `break`,
204            // expanding compressed trait refs.
205            let res = loop {
206                // First check whether we're still expanding some compressed trait,
207                // and make one step of progress towards it if we can.
208                // `self.compressed_elem` is `0` when we're not expanding a compressed trait,
209                // and in that case this function returns `None`
210                if let Some(trait_ref_single_bit) =
211                    self.decompression_in_progress.get_and_mask_least_significant_bit()
212                {
213                    match filter_compressed_trait_helper(
214                        &filter.trait_filter,
215                        &filter.output_filter,
216                        cx,
217                        self.self_ty.unwrap(),
218                        trait_ref_single_bit,
219                    ) {
220                        Some(tc) => break tc,
221                        None => continue,
222                    }
223                } else {
224                    // Only if we indeed are not, do we check the next item in the iterator.
225                    // If exhausted, we also return None.
226                    let clause = clauses.next()?;
227                    let kind = clause.kind();
228                    match kind.skip_binder() {
229                        // If we find a trait clause, and it matches, return it
230                        ClauseKind::Trait(trait_clause)
231                            if filter_trait_clause_helper(&filter, trait_clause, kind) =>
232                        {
233                            break kind.rebind(trait_clause);
234                        }
235                        // If we find a compressed trait clause, and it matches, store it in `self.compressed_elem`
236                        ClauseKind::CompressedTrait(ct)
237                            if filter.self_type_filter.allow_self_ty(ct.self_ty.type_at(0))
238                                && filter
239                                    .polarity_filter
240                                    .allow_polarity(ClausePolarity::Positive)
241                                && filter
242                                    .trait_filter
243                                    .allow_any_compressed_trait_ref(ct.compressed_trait_refs) =>
244                        {
245                            self.decompression_in_progress = ct.compressed_trait_refs;
246                            self.self_ty = Some(ct.self_ty);
247                        }
248                        _ => continue,
249                    }
250                }
251            };
252
253            Some(O::construct_from_trait_clause(res, cx))
254        }
255    }
256
257    impl<I, Iter, P, S, T, F, O> CallerBoundsIterator<I, Iter>
258        for CallerBoundsAccessor<I, Iter, TraitClauses<I, P, S, T, F, O>>
259    where
260        I: Interner,
261        Iter: Iterator<Item = I::Clause>,
262        P: PolarityFilter,
263        S: SelfTyFilter<I>,
264        T: TraitFilter<I>,
265        F: OutputFilter<I>,
266        O: OutputAs<I>,
267    {
268        type Item = O::Output;
269
270        #[inline]
271        fn iter(self) -> impl Iterator<Item = Self::Item> {
272            CallerBoundsIteratorImpl {
273                accessor: self,
274                decompression_in_progress: CompressedTraitRefs::empty(),
275                self_ty: None,
276            }
277        }
278    }
279}
280
281mod clause_filter {
282    use super::*;
283
284    pub trait ClauseFilter {}
285
286    pub struct NoClauses;
287    impl ClauseFilter for NoClauses {}
288
289    pub struct NonTraitClauses;
290    impl ClauseFilter for NonTraitClauses {}
291
292    #[derive(#[automatically_derived]impl<I: ::core::clone::Clone, P: ::core::clone::Clone,
    S: ::core::clone::Clone, T: ::core::clone::Clone, F: ::core::clone::Clone,
    O: ::core::clone::Clone> ::core::clone::Clone for
    TraitClauses<I, P, S, T, F, O> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        TraitClauses {
            cx: ::core::clone::Clone::clone(&self.cx),
            polarity_filter: ::core::clone::Clone::clone(&self.polarity_filter),
            self_type_filter: ::core::clone::Clone::clone(&self.self_type_filter),
            trait_filter: ::core::clone::Clone::clone(&self.trait_filter),
            output_filter: ::core::clone::Clone::clone(&self.output_filter),
            output: ::core::clone::Clone::clone(&self.output),
        }
    }
}Clone, #[automatically_derived]impl<I: ::core::marker::Copy, P: ::core::marker::Copy,
    S: ::core::marker::Copy, T: ::core::marker::Copy, F: ::core::marker::Copy,
    O: ::core::marker::Copy> ::core::marker::Copy for
    TraitClauses<I, P, S, T, F, O> {
}Copy)]
293    pub struct TraitClauses<I, P, S, T, F, O> {
294        pub cx: I,
295        pub polarity_filter: P,
296        pub self_type_filter: S,
297        pub trait_filter: T,
298        pub output_filter: F,
299        pub output: O,
300    }
301
302    impl<I, P, S, T, F, O> ClauseFilter for TraitClauses<I, P, S, T, F, O> {}
303
304    pub struct AllClauses<I>(pub I)
305    where
306        I: Interner;
307    impl<I> ClauseFilter for AllClauses<I> where I: Interner {}
308
309    pub struct AllClausesInterned<I>(pub I)
310    where
311        I: Interner;
312    impl<I> ClauseFilter for AllClausesInterned<I> where I: Interner {}
313
314    impl<I, Iter> CallerBoundsAccessor<I, Iter, NoClauses>
315    where
316        I: Interner,
317        Iter: Iterator<Item = I::Clause>,
318    {
319        #[inline]
320        pub fn non_trait_clauses(self) -> CallerBoundsAccessor<I, Iter, NonTraitClauses> {
321            CallerBoundsAccessor {
322                clauses: self.clauses,
323                clause_filter: NonTraitClauses,
324                cx: PhantomData,
325            }
326        }
327
328        #[inline]
329        pub fn trait_clauses(
330            self,
331            cx: I,
332        ) -> CallerBoundsAccessor<
333            I,
334            Iter,
335            TraitClauses<
336                I,
337                NoPolarityFilter,
338                NoSelfTyFilter,
339                NoTraitFilter,
340                NoOutputFilter,
341                OutputAsTraitClause,
342            >,
343        > {
344            CallerBoundsAccessor {
345                clauses: self.clauses,
346                clause_filter: TraitClauses {
347                    polarity_filter: NoPolarityFilter,
348                    self_type_filter: NoSelfTyFilter,
349                    trait_filter: NoTraitFilter,
350                    output_filter: NoOutputFilter,
351                    output: OutputAsTraitClause,
352                    cx,
353                },
354                cx: PhantomData,
355            }
356        }
357
358        /// Warning: may be a performance hazzard!
359        #[inline]
360        pub fn all_clauses_interned(
361            self,
362            cx: I,
363        ) -> CallerBoundsAccessor<I, Iter, AllClausesInterned<I>> {
364            CallerBoundsAccessor {
365                clauses: self.clauses,
366                clause_filter: AllClausesInterned(cx),
367                cx: PhantomData,
368            }
369        }
370
371        /// Warning: may be a performance hazzard!
372        #[inline]
373        pub fn all_clauses(self, cx: I) -> CallerBoundsAccessor<I, Iter, AllClauses<I>> {
374            CallerBoundsAccessor {
375                clauses: self.clauses,
376                clause_filter: AllClauses(cx),
377                cx: PhantomData,
378            }
379        }
380    }
381}
382
383mod polarity_filter {
384    use super::*;
385
386    pub trait PolarityFilter {
387        fn allow_polarity(&self, p: ClausePolarity) -> bool;
388    }
389
390    pub struct NoPolarityFilter;
391
392    impl PolarityFilter for NoPolarityFilter {
393        #[inline]
394        fn allow_polarity(&self, _p: ClausePolarity) -> bool {
395            true
396        }
397    }
398    impl PolarityFilter for ClausePolarity {
399        #[inline]
400        fn allow_polarity(&self, p: ClausePolarity) -> bool {
401            *self == p
402        }
403    }
404
405    impl<I, Iter, P, S, T, F, O> CallerBoundsAccessor<I, Iter, TraitClauses<I, P, S, T, F, O>>
406    where
407        I: Interner,
408        Iter: Iterator<Item = I::Clause>,
409        P: PolarityFilter,
410        S: SelfTyFilter<I>,
411        T: TraitFilter<I>,
412        F: OutputFilter<I>,
413        O: OutputAs<I>,
414    {
415        #[inline]
416        pub fn with_polarity(
417            self,
418            polarity: ClausePolarity,
419        ) -> CallerBoundsAccessor<I, Iter, TraitClauses<I, ClausePolarity, S, T, F, O>> {
420            let CallerBoundsAccessor {
421                clauses: iter,
422                clause_filter:
423                    TraitClauses {
424                        cx,
425                        polarity_filter: _,
426                        self_type_filter,
427                        trait_filter,
428                        output_filter,
429                        output,
430                    },
431                cx: PhantomData,
432            } = self;
433            CallerBoundsAccessor {
434                clauses: iter,
435                clause_filter: TraitClauses {
436                    cx,
437                    polarity_filter: polarity,
438                    self_type_filter,
439                    trait_filter,
440                    output_filter,
441                    output,
442                },
443                cx: PhantomData,
444            }
445        }
446        #[inline]
447        pub fn validate_self_ty<SF: Fn(I::Ty) -> bool>(
448            self,
449            validate_fn: SF,
450        ) -> CallerBoundsAccessor<I, Iter, TraitClauses<I, P, SelfTyFilterFn<SF>, T, F, O>>
451        {
452            let CallerBoundsAccessor {
453                clauses: iter,
454                clause_filter:
455                    TraitClauses {
456                        cx,
457                        polarity_filter,
458                        self_type_filter: _,
459                        trait_filter,
460                        output_filter,
461                        output,
462                    },
463                cx: PhantomData,
464            } = self;
465            CallerBoundsAccessor {
466                clauses: iter,
467                clause_filter: TraitClauses {
468                    cx,
469                    polarity_filter,
470                    self_type_filter: SelfTyFilterFn { f: validate_fn },
471                    trait_filter,
472                    output_filter,
473                    output,
474                },
475                cx: PhantomData,
476            }
477        }
478        #[inline]
479        pub fn with_self_ty(
480            self,
481            desired_self_ty: I::Ty,
482        ) -> CallerBoundsAccessor<
483            I,
484            Iter,
485            TraitClauses<I, P, SelfTyFilterFn<impl Fn(I::Ty) -> bool>, T, F, O>,
486        > {
487            self.validate_self_ty(move |actual_self_ty| actual_self_ty == desired_self_ty)
488        }
489
490        #[inline]
491        fn for_target_trait(
492            self,
493            trait_filter: TargetTrait<I>,
494        ) -> CallerBoundsAccessor<I, Iter, TraitClauses<I, P, S, TargetTrait<I>, F, O>> {
495            let CallerBoundsAccessor {
496                clauses: iter,
497                clause_filter:
498                    TraitClauses {
499                        cx,
500                        polarity_filter,
501                        self_type_filter,
502                        trait_filter: _,
503                        output_filter,
504                        output,
505                    },
506                cx: PhantomData,
507            } = self;
508            CallerBoundsAccessor {
509                clauses: iter,
510                clause_filter: TraitClauses {
511                    cx,
512                    polarity_filter,
513                    self_type_filter,
514                    trait_filter,
515                    output_filter,
516                    output,
517                },
518                cx: PhantomData,
519            }
520        }
521        #[inline]
522        pub fn for_trait(
523            self,
524            def_id: I::TraitId,
525        ) -> CallerBoundsAccessor<I, Iter, TraitClauses<I, P, S, TargetTrait<I>, F, O>> {
526            let cx = self.clause_filter.cx;
527
528            self.for_target_trait(TargetTrait {
529                def_id,
530                lang_item_compressed: cx
531                    .as_trait_lang_item(def_id)
532                    .and_then(|lang_item| CompressedTraitRefs::from_lang_item(lang_item)),
533            })
534        }
535        #[inline]
536        pub fn for_trait_lang_item(
537            self,
538            lang_item: SolverTraitLangItem,
539        ) -> CallerBoundsAccessor<I, Iter, TraitClauses<I, P, S, TargetTrait<I>, F, O>> {
540            let cx = self.clause_filter.cx;
541            self.for_target_trait(TargetTrait {
542                def_id: cx.require_trait_lang_item(lang_item),
543                lang_item_compressed: CompressedTraitRefs::from_lang_item(lang_item),
544            })
545        }
546
547        /// Filter the final [`TraitClause`] before interning.
548        #[inline]
549        pub fn filter<OF>(
550            self,
551            f: OF,
552        ) -> CallerBoundsAccessor<I, Iter, TraitClauses<I, P, S, T, OutputFilterFn<OF>, O>>
553        where
554            OF: Fn(Binder<I, TraitClause<I>>) -> bool,
555        {
556            let CallerBoundsAccessor {
557                clauses: iter,
558                clause_filter:
559                    TraitClauses {
560                        cx,
561                        polarity_filter,
562                        self_type_filter,
563                        trait_filter,
564                        output_filter: _,
565                        output,
566                    },
567                cx: PhantomData,
568            } = self;
569            CallerBoundsAccessor {
570                clauses: iter,
571                clause_filter: TraitClauses {
572                    cx,
573                    polarity_filter,
574                    self_type_filter,
575                    trait_filter,
576                    output_filter: OutputFilterFn { f },
577                    output,
578                },
579                cx: PhantomData,
580            }
581        }
582
583        #[inline]
584        pub fn interned(
585            self,
586        ) -> CallerBoundsAccessor<I, Iter, TraitClauses<I, P, S, T, F, OutputAsInternedClause>>
587        {
588            let CallerBoundsAccessor {
589                clauses: iter,
590                clause_filter:
591                    TraitClauses {
592                        cx,
593                        polarity_filter,
594                        self_type_filter,
595                        trait_filter,
596                        output_filter,
597                        output: _,
598                    },
599                cx: PhantomData,
600            } = self;
601            CallerBoundsAccessor {
602                clauses: iter,
603                clause_filter: TraitClauses {
604                    cx,
605                    polarity_filter,
606                    self_type_filter,
607                    trait_filter,
608                    output_filter,
609                    output: OutputAsInternedClause,
610                },
611                cx: PhantomData,
612            }
613        }
614    }
615}
616
617mod self_ty_filter {
618    use super::*;
619
620    pub trait SelfTyFilter<I>
621    where
622        I: Interner,
623    {
624        fn allow_self_ty(&self, ty: I::Ty) -> bool;
625    }
626
627    pub struct NoSelfTyFilter;
628
629    impl<I> SelfTyFilter<I> for NoSelfTyFilter
630    where
631        I: Interner,
632    {
633        #[inline]
634        fn allow_self_ty(&self, _ty: I::Ty) -> bool {
635            true
636        }
637    }
638
639    pub struct SelfTyFilterFn<F> {
640        pub f: F,
641    }
642    impl<I, F> SelfTyFilter<I> for SelfTyFilterFn<F>
643    where
644        I: Interner,
645        F: Fn(I::Ty) -> bool,
646    {
647        #[inline]
648        fn allow_self_ty(&self, ty: I::Ty) -> bool {
649            (self.f)(ty)
650        }
651    }
652}
653
654mod trait_filter {
655    use super::*;
656    use crate::CompressedTraitRefs;
657
658    pub trait TraitFilter<I>
659    where
660        I: Interner,
661    {
662        fn allow_clause(&self, clause: TraitClause<I>) -> bool;
663        /// Returns the defid of the trait if this filter allows the given `trait_ref`.
664        /// Note that `trait_ref` should have only a single bit set.
665        fn allow_compressed_trait_ref_single_bit(
666            &self,
667            cx: I,
668            trait_ref: CompressedTraitRefs,
669        ) -> Option<I::TraitId>;
670        /// returns true if this trait filter *might* allow any
671        /// of the traits in `trait_ref` to match.
672        fn allow_any_compressed_trait_ref(&self, trait_ref: CompressedTraitRefs) -> bool;
673    }
674
675    pub struct NoTraitFilter;
676
677    impl<I> TraitFilter<I> for NoTraitFilter
678    where
679        I: Interner,
680    {
681        #[inline]
682        fn allow_clause(&self, _clause: TraitClause<I>) -> bool {
683            true
684        }
685
686        #[inline]
687        fn allow_compressed_trait_ref_single_bit(
688            &self,
689            cx: I,
690            trait_ref: CompressedTraitRefs,
691        ) -> Option<I::TraitId> {
692            Some(cx.require_trait_lang_item(trait_ref.lang_item_from_single_bit()))
693        }
694
695        fn allow_any_compressed_trait_ref(&self, _trait_ref: CompressedTraitRefs) -> bool {
696            true
697        }
698    }
699
700    pub struct TargetTrait<I>
701    where
702        I: Interner,
703    {
704        pub def_id: I::TraitId,
705        /// When scanning specifically for trait refs that can be compressed,
706        /// this value has a single bit set, representing the compressed form
707        /// of the trait we're looking for.
708        pub lang_item_compressed: Option<CompressedTraitRefs>,
709    }
710    impl<I> TraitFilter<I> for TargetTrait<I>
711    where
712        I: Interner,
713    {
714        #[inline]
715        fn allow_clause(&self, clause: TraitClause<I>) -> bool {
716            // When looking for a compressed trait, you'd think no normal clause
717            // can ever match so we could return false unconditionally when
718            // self.lang_item_compressed is `None`. However, bound clauses are
719            // never compressed, so in cases where we have `for<'a> T: Send`,
720            // a `Send` bound which would otherwise be compressed is actually not,
721            // and we should return it.
722            clause.def_id() == self.def_id
723        }
724
725        #[inline]
726        fn allow_compressed_trait_ref_single_bit(
727            &self,
728            _cx: I,
729            trait_ref: CompressedTraitRefs,
730        ) -> Option<I::TraitId> {
731            if let Some(target) = self.lang_item_compressed {
732                (trait_ref == target).then_some(self.def_id)
733            } else {
734                // When looking for a normal trait, any compressed clause can never match so we
735                // return false unconditionally.
736                None
737            }
738        }
739
740        fn allow_any_compressed_trait_ref(&self, trait_ref: CompressedTraitRefs) -> bool {
741            if let Some(target) = self.lang_item_compressed {
742                // only if the given `trait_ref` has the bit set that corresponds to `target`, do we
743                // return true. Otherwise we can skip this compressed trait instantly.
744                trait_ref.contains(target)
745            } else {
746                // When looking for a normal trait, any compressed clause can never match so we
747                // return false unconditionally.
748                false
749            }
750        }
751    }
752}
753
754mod output_filter {
755    use super::*;
756
757    pub trait OutputFilter<I>
758    where
759        I: Interner,
760    {
761        fn allow_trait_clause(&self, clause: Binder<I, TraitClause<I>>) -> bool;
762    }
763
764    pub struct NoOutputFilter;
765
766    impl<I> OutputFilter<I> for NoOutputFilter
767    where
768        I: Interner,
769    {
770        #[inline]
771        fn allow_trait_clause(&self, _clause: Binder<I, TraitClause<I>>) -> bool {
772            true
773        }
774    }
775
776    pub struct OutputFilterFn<F> {
777        pub f: F,
778    }
779    impl<I, F> OutputFilter<I> for OutputFilterFn<F>
780    where
781        I: Interner,
782        F: Fn(Binder<I, TraitClause<I>>) -> bool,
783    {
784        #[inline]
785        fn allow_trait_clause(&self, clause: Binder<I, TraitClause<I>>) -> bool {
786            (self.f)(clause)
787        }
788    }
789}
790
791mod output_as {
792    use super::*;
793
794    pub trait OutputAs<I: Interner> {
795        type Output;
796        fn construct_from_trait_clause(tc: Binder<I, TraitClause<I>>, cx: I) -> Self::Output;
797    }
798
799    pub struct OutputAsInternedClause;
800    impl<I: Interner> OutputAs<I> for OutputAsInternedClause {
801        type Output = I::Clause;
802        fn construct_from_trait_clause(tc: Binder<I, TraitClause<I>>, cx: I) -> I::Clause {
803            tc.upcast(cx)
804        }
805    }
806
807    pub struct OutputAsTraitClause;
808    impl<I: Interner> OutputAs<I> for OutputAsTraitClause {
809        type Output = Binder<I, TraitClause<I>>;
810        fn construct_from_trait_clause(
811            tc: Binder<I, TraitClause<I>>,
812            _cx: I,
813        ) -> Binder<I, TraitClause<I>> {
814            tc
815        }
816    }
817}