|
| 1 | +///! Definition of `InferCtxtLike` from the librarified type layer. |
| 2 | +use rustc_hir::def_id::{DefId, LocalDefId}; |
| 3 | +use rustc_middle::traits::solve::{Goal, NoSolution, SolverMode}; |
| 4 | +use rustc_middle::traits::ObligationCause; |
| 5 | +use rustc_middle::ty::fold::TypeFoldable; |
| 6 | +use rustc_middle::ty::{self, Ty, TyCtxt}; |
| 7 | +use rustc_span::DUMMY_SP; |
| 8 | +use rustc_type_ir::relate::Relate; |
| 9 | +use rustc_type_ir::InferCtxtLike; |
| 10 | + |
| 11 | +use super::{BoundRegionConversionTime, InferCtxt, SubregionOrigin}; |
| 12 | + |
| 13 | +impl<'tcx> InferCtxtLike for InferCtxt<'tcx> { |
| 14 | + type Interner = TyCtxt<'tcx>; |
| 15 | + |
| 16 | + fn cx(&self) -> TyCtxt<'tcx> { |
| 17 | + self.tcx |
| 18 | + } |
| 19 | + |
| 20 | + fn solver_mode(&self) -> ty::solve::SolverMode { |
| 21 | + match self.intercrate { |
| 22 | + true => SolverMode::Coherence, |
| 23 | + false => SolverMode::Normal, |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + fn universe(&self) -> ty::UniverseIndex { |
| 28 | + self.universe() |
| 29 | + } |
| 30 | + |
| 31 | + fn create_next_universe(&self) -> ty::UniverseIndex { |
| 32 | + self.create_next_universe() |
| 33 | + } |
| 34 | + |
| 35 | + fn universe_of_ty(&self, vid: ty::TyVid) -> Option<ty::UniverseIndex> { |
| 36 | + match self.probe_ty_var(vid) { |
| 37 | + Err(universe) => Some(universe), |
| 38 | + Ok(_) => None, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + fn universe_of_lt(&self, lt: ty::RegionVid) -> Option<ty::UniverseIndex> { |
| 43 | + match self.inner.borrow_mut().unwrap_region_constraints().probe_value(lt) { |
| 44 | + Err(universe) => Some(universe), |
| 45 | + Ok(_) => None, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + fn universe_of_ct(&self, ct: ty::ConstVid) -> Option<ty::UniverseIndex> { |
| 50 | + match self.probe_const_var(ct) { |
| 51 | + Err(universe) => Some(universe), |
| 52 | + Ok(_) => None, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + fn root_ty_var(&self, var: ty::TyVid) -> ty::TyVid { |
| 57 | + self.root_var(var) |
| 58 | + } |
| 59 | + |
| 60 | + fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid { |
| 61 | + self.root_const_var(var) |
| 62 | + } |
| 63 | + |
| 64 | + fn opportunistic_resolve_ty_var(&self, vid: ty::TyVid) -> Ty<'tcx> { |
| 65 | + match self.probe_ty_var(vid) { |
| 66 | + Ok(ty) => ty, |
| 67 | + Err(_) => Ty::new_var(self.tcx, self.root_var(vid)), |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> { |
| 72 | + self.opportunistic_resolve_int_var(vid) |
| 73 | + } |
| 74 | + |
| 75 | + fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> { |
| 76 | + self.opportunistic_resolve_float_var(vid) |
| 77 | + } |
| 78 | + |
| 79 | + fn opportunistic_resolve_ct_var(&self, vid: ty::ConstVid) -> ty::Const<'tcx> { |
| 80 | + match self.probe_const_var(vid) { |
| 81 | + Ok(ct) => ct, |
| 82 | + Err(_) => ty::Const::new_var(self.tcx, self.root_const_var(vid)), |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + fn opportunistic_resolve_effect_var(&self, vid: ty::EffectVid) -> ty::Const<'tcx> { |
| 87 | + match self.probe_effect_var(vid) { |
| 88 | + Some(ct) => ct, |
| 89 | + None => { |
| 90 | + ty::Const::new_infer(self.tcx, ty::InferConst::EffectVar(self.root_effect_var(vid))) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> ty::Region<'tcx> { |
| 96 | + self.inner.borrow_mut().unwrap_region_constraints().opportunistic_resolve_var(self.tcx, vid) |
| 97 | + } |
| 98 | + |
| 99 | + fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> { |
| 100 | + self.defining_opaque_types() |
| 101 | + } |
| 102 | + |
| 103 | + fn next_ty_infer(&self) -> Ty<'tcx> { |
| 104 | + self.next_ty_var(DUMMY_SP) |
| 105 | + } |
| 106 | + |
| 107 | + fn next_const_infer(&self) -> ty::Const<'tcx> { |
| 108 | + self.next_const_var(DUMMY_SP) |
| 109 | + } |
| 110 | + |
| 111 | + fn fresh_args_for_item(&self, def_id: DefId) -> ty::GenericArgsRef<'tcx> { |
| 112 | + self.fresh_args_for_item(DUMMY_SP, def_id) |
| 113 | + } |
| 114 | + |
| 115 | + fn instantiate_binder_with_infer<T: TypeFoldable<TyCtxt<'tcx>> + Copy>( |
| 116 | + &self, |
| 117 | + value: ty::Binder<'tcx, T>, |
| 118 | + ) -> T { |
| 119 | + self.instantiate_binder_with_fresh_vars( |
| 120 | + DUMMY_SP, |
| 121 | + BoundRegionConversionTime::HigherRankedType, |
| 122 | + value, |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + fn enter_forall<T: TypeFoldable<TyCtxt<'tcx>> + Copy, U>( |
| 127 | + &self, |
| 128 | + value: ty::Binder<'tcx, T>, |
| 129 | + f: impl FnOnce(T) -> U, |
| 130 | + ) -> U { |
| 131 | + self.enter_forall(value, f) |
| 132 | + } |
| 133 | + |
| 134 | + fn relate<T: Relate<TyCtxt<'tcx>>>( |
| 135 | + &self, |
| 136 | + param_env: ty::ParamEnv<'tcx>, |
| 137 | + lhs: T, |
| 138 | + variance: ty::Variance, |
| 139 | + rhs: T, |
| 140 | + ) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> { |
| 141 | + self.at(&ObligationCause::dummy(), param_env).relate_no_trace(lhs, variance, rhs) |
| 142 | + } |
| 143 | + |
| 144 | + fn eq_structurally_relating_aliases<T: Relate<TyCtxt<'tcx>>>( |
| 145 | + &self, |
| 146 | + param_env: ty::ParamEnv<'tcx>, |
| 147 | + lhs: T, |
| 148 | + rhs: T, |
| 149 | + ) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> { |
| 150 | + self.at(&ObligationCause::dummy(), param_env) |
| 151 | + .eq_structurally_relating_aliases_no_trace(lhs, rhs) |
| 152 | + } |
| 153 | + |
| 154 | + fn resolve_vars_if_possible<T>(&self, value: T) -> T |
| 155 | + where |
| 156 | + T: TypeFoldable<TyCtxt<'tcx>>, |
| 157 | + { |
| 158 | + self.resolve_vars_if_possible(value) |
| 159 | + } |
| 160 | + |
| 161 | + fn probe<T>(&self, probe: impl FnOnce() -> T) -> T { |
| 162 | + self.probe(|_| probe()) |
| 163 | + } |
| 164 | + |
| 165 | + fn sub_regions(&self, sub: ty::Region<'tcx>, sup: ty::Region<'tcx>) { |
| 166 | + self.sub_regions(SubregionOrigin::RelateRegionParamBound(DUMMY_SP), sub, sup) |
| 167 | + } |
| 168 | + |
| 169 | + fn register_ty_outlives(&self, ty: Ty<'tcx>, r: ty::Region<'tcx>) { |
| 170 | + self.register_region_obligation_with_cause(ty, r, &ObligationCause::dummy()); |
| 171 | + } |
| 172 | +} |
0 commit comments