|
| 1 | +use rustc_hir::def_id::DefId; |
| 2 | +use rustc_infer::infer::at::ToTrace; |
| 3 | +use rustc_infer::infer::canonical::CanonicalVarValues; |
| 4 | +use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; |
| 5 | +use rustc_infer::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime}; |
| 6 | +use rustc_infer::traits::query::NoSolution; |
| 7 | +use rustc_infer::traits::ObligationCause; |
| 8 | +use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind}; |
| 9 | +use rustc_middle::ty::{self, ir::TypeVisitor, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable}; |
| 10 | +use rustc_span::DUMMY_SP; |
| 11 | +use std::ops::ControlFlow; |
| 12 | + |
| 13 | +use super::search_graph::SearchGraph; |
| 14 | +use super::Goal; |
| 15 | + |
| 16 | +pub struct EvalCtxt<'a, 'tcx> { |
| 17 | + // FIXME: should be private. |
| 18 | + pub(super) infcx: &'a InferCtxt<'tcx>, |
| 19 | + |
| 20 | + pub(super) var_values: CanonicalVarValues<'tcx>, |
| 21 | + |
| 22 | + pub(super) search_graph: &'a mut SearchGraph<'tcx>, |
| 23 | + |
| 24 | + /// This field is used by a debug assertion in [`EvalCtxt::evaluate_goal`], |
| 25 | + /// see the comment in that method for more details. |
| 26 | + pub in_projection_eq_hack: bool, |
| 27 | +} |
| 28 | + |
| 29 | +impl<'tcx> EvalCtxt<'_, 'tcx> { |
| 30 | + pub(super) fn probe<T>(&mut self, f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> T) -> T { |
| 31 | + self.infcx.probe(|_| f(self)) |
| 32 | + } |
| 33 | + |
| 34 | + pub(super) fn tcx(&self) -> TyCtxt<'tcx> { |
| 35 | + self.infcx.tcx |
| 36 | + } |
| 37 | + |
| 38 | + pub(super) fn next_ty_infer(&self) -> Ty<'tcx> { |
| 39 | + self.infcx.next_ty_var(TypeVariableOrigin { |
| 40 | + kind: TypeVariableOriginKind::MiscVariable, |
| 41 | + span: DUMMY_SP, |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + pub(super) fn next_const_infer(&self, ty: Ty<'tcx>) -> ty::Const<'tcx> { |
| 46 | + self.infcx.next_const_var( |
| 47 | + ty, |
| 48 | + ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span: DUMMY_SP }, |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + /// Is the projection predicate is of the form `exists<T> <Ty as Trait>::Assoc = T`. |
| 53 | + /// |
| 54 | + /// This is the case if the `term` is an inference variable in the innermost universe |
| 55 | + /// and does not occur in any other part of the predicate. |
| 56 | + pub(super) fn term_is_fully_unconstrained( |
| 57 | + &self, |
| 58 | + goal: Goal<'tcx, ty::ProjectionPredicate<'tcx>>, |
| 59 | + ) -> bool { |
| 60 | + let term_is_infer = match goal.predicate.term.unpack() { |
| 61 | + ty::TermKind::Ty(ty) => { |
| 62 | + if let &ty::Infer(ty::TyVar(vid)) = ty.kind() { |
| 63 | + match self.infcx.probe_ty_var(vid) { |
| 64 | + Ok(value) => bug!("resolved var in query: {goal:?} {value:?}"), |
| 65 | + Err(universe) => universe == self.universe(), |
| 66 | + } |
| 67 | + } else { |
| 68 | + false |
| 69 | + } |
| 70 | + } |
| 71 | + ty::TermKind::Const(ct) => { |
| 72 | + if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() { |
| 73 | + match self.infcx.probe_const_var(vid) { |
| 74 | + Ok(value) => bug!("resolved var in query: {goal:?} {value:?}"), |
| 75 | + Err(universe) => universe == self.universe(), |
| 76 | + } |
| 77 | + } else { |
| 78 | + false |
| 79 | + } |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + // Guard against `<T as Trait<?0>>::Assoc = ?0>`. |
| 84 | + struct ContainsTerm<'tcx> { |
| 85 | + term: ty::Term<'tcx>, |
| 86 | + } |
| 87 | + impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ContainsTerm<'tcx> { |
| 88 | + type BreakTy = (); |
| 89 | + fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 90 | + if t.needs_infer() { |
| 91 | + if ty::Term::from(t) == self.term { |
| 92 | + ControlFlow::Break(()) |
| 93 | + } else { |
| 94 | + t.super_visit_with(self) |
| 95 | + } |
| 96 | + } else { |
| 97 | + ControlFlow::Continue(()) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> { |
| 102 | + if c.needs_infer() { |
| 103 | + if ty::Term::from(c) == self.term { |
| 104 | + ControlFlow::Break(()) |
| 105 | + } else { |
| 106 | + c.super_visit_with(self) |
| 107 | + } |
| 108 | + } else { |
| 109 | + ControlFlow::Continue(()) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + let mut visitor = ContainsTerm { term: goal.predicate.term }; |
| 115 | + |
| 116 | + term_is_infer |
| 117 | + && goal.predicate.projection_ty.visit_with(&mut visitor).is_continue() |
| 118 | + && goal.param_env.visit_with(&mut visitor).is_continue() |
| 119 | + } |
| 120 | + |
| 121 | + #[instrument(level = "debug", skip(self, param_env), ret)] |
| 122 | + pub(super) fn eq<T: ToTrace<'tcx>>( |
| 123 | + &self, |
| 124 | + param_env: ty::ParamEnv<'tcx>, |
| 125 | + lhs: T, |
| 126 | + rhs: T, |
| 127 | + ) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> { |
| 128 | + self.infcx |
| 129 | + .at(&ObligationCause::dummy(), param_env) |
| 130 | + .eq(lhs, rhs) |
| 131 | + .map(|InferOk { value: (), obligations }| { |
| 132 | + obligations.into_iter().map(|o| o.into()).collect() |
| 133 | + }) |
| 134 | + .map_err(|e| { |
| 135 | + debug!(?e, "failed to equate"); |
| 136 | + NoSolution |
| 137 | + }) |
| 138 | + } |
| 139 | + |
| 140 | + pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<'tcx> + Copy>( |
| 141 | + &self, |
| 142 | + value: ty::Binder<'tcx, T>, |
| 143 | + ) -> T { |
| 144 | + self.infcx.instantiate_binder_with_fresh_vars( |
| 145 | + DUMMY_SP, |
| 146 | + LateBoundRegionConversionTime::HigherRankedType, |
| 147 | + value, |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + pub(super) fn instantiate_binder_with_placeholders<T: TypeFoldable<'tcx> + Copy>( |
| 152 | + &self, |
| 153 | + value: ty::Binder<'tcx, T>, |
| 154 | + ) -> T { |
| 155 | + self.infcx.instantiate_binder_with_placeholders(value) |
| 156 | + } |
| 157 | + |
| 158 | + pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T |
| 159 | + where |
| 160 | + T: TypeFoldable<'tcx>, |
| 161 | + { |
| 162 | + self.infcx.resolve_vars_if_possible(value) |
| 163 | + } |
| 164 | + |
| 165 | + pub(super) fn fresh_substs_for_item(&self, def_id: DefId) -> ty::SubstsRef<'tcx> { |
| 166 | + self.infcx.fresh_substs_for_item(DUMMY_SP, def_id) |
| 167 | + } |
| 168 | + |
| 169 | + pub(super) fn universe(&self) -> ty::UniverseIndex { |
| 170 | + self.infcx.universe() |
| 171 | + } |
| 172 | +} |
0 commit comments