]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/relationships.rs
Auto merge of #93696 - Amanieu:compiler-builtins-0.1.68, r=Mark-Simulacrum
[rust.git] / compiler / rustc_trait_selection / src / traits / relationships.rs
1 use crate::infer::InferCtxt;
2 use crate::traits::query::evaluate_obligation::InferCtxtExt;
3 use crate::traits::{ObligationCause, PredicateObligation};
4 use rustc_infer::traits::TraitEngine;
5 use rustc_middle::ty::{self, ToPredicate};
6
7 pub(crate) fn update<'tcx, T>(
8     engine: &mut T,
9     infcx: &InferCtxt<'_, 'tcx>,
10     obligation: &PredicateObligation<'tcx>,
11 ) where
12     T: TraitEngine<'tcx>,
13 {
14     // (*) binder skipped
15     if let ty::PredicateKind::Trait(predicate) = obligation.predicate.kind().skip_binder() {
16         if let Some(ty) =
17             infcx.shallow_resolve(predicate.self_ty()).ty_vid().map(|t| infcx.root_var(t))
18         {
19             if infcx
20                 .tcx
21                 .lang_items()
22                 .sized_trait()
23                 .map_or(false, |st| st != predicate.trait_ref.def_id)
24             {
25                 let new_self_ty = infcx.tcx.types.unit;
26
27                 let trait_ref = ty::TraitRef {
28                     substs: infcx
29                         .tcx
30                         .mk_substs_trait(new_self_ty, &predicate.trait_ref.substs[1..]),
31                     ..predicate.trait_ref
32                 };
33
34                 // Then contstruct a new obligation with Self = () added
35                 // to the ParamEnv, and see if it holds.
36                 let o = rustc_infer::traits::Obligation::new(
37                     ObligationCause::dummy(),
38                     obligation.param_env,
39                     obligation
40                         .predicate
41                         .kind()
42                         .map_bound(|_| {
43                             // (*) binder moved here
44                             ty::PredicateKind::Trait(ty::TraitPredicate {
45                                 trait_ref,
46                                 constness: predicate.constness,
47                                 polarity: predicate.polarity,
48                             })
49                         })
50                         .to_predicate(infcx.tcx),
51                 );
52                 // Don't report overflow errors. Otherwise equivalent to may_hold.
53                 if let Ok(result) = infcx.probe(|_| infcx.evaluate_obligation(&o)) {
54                     if result.may_apply() {
55                         engine.relationships().entry(ty).or_default().self_in_trait = true;
56                     }
57                 }
58             }
59         }
60     }
61
62     if let ty::PredicateKind::Projection(predicate) = obligation.predicate.kind().skip_binder() {
63         // If the projection predicate (Foo::Bar == X) has X as a non-TyVid,
64         // we need to make it into one.
65         if let Some(vid) = predicate.term.ty().and_then(|ty| ty.ty_vid()) {
66             debug!("relationship: {:?}.output = true", vid);
67             engine.relationships().entry(vid).or_default().output = true;
68         }
69     }
70 }