|
| 1 | +use super::{EvalCtxt, SolverMode}; |
| 2 | +use rustc_infer::traits::query::NoSolution; |
| 3 | +use rustc_middle::traits::solve::{Certainty, Goal, QueryResult}; |
| 4 | +use rustc_middle::ty; |
| 5 | + |
| 6 | +/// We may need to invert the alias relation direction if dealing an alias on the RHS. |
| 7 | +#[derive(Debug)] |
| 8 | +enum Invert { |
| 9 | + No, |
| 10 | + Yes, |
| 11 | +} |
| 12 | + |
| 13 | +impl<'tcx> EvalCtxt<'_, 'tcx> { |
| 14 | + #[instrument(level = "debug", skip(self), ret)] |
| 15 | + pub(super) fn compute_alias_relate_goal( |
| 16 | + &mut self, |
| 17 | + goal: Goal<'tcx, (ty::Term<'tcx>, ty::Term<'tcx>, ty::AliasRelationDirection)>, |
| 18 | + ) -> QueryResult<'tcx> { |
| 19 | + let tcx = self.tcx(); |
| 20 | + let Goal { param_env, predicate: (lhs, rhs, direction) } = goal; |
| 21 | + if lhs.is_infer() || rhs.is_infer() { |
| 22 | + bug!( |
| 23 | + "`AliasRelate` goal with an infer var on lhs or rhs which should have been instantiated" |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + match (lhs.to_alias_ty(tcx), rhs.to_alias_ty(tcx)) { |
| 28 | + (None, None) => bug!("`AliasRelate` goal without an alias on either lhs or rhs"), |
| 29 | + |
| 30 | + // RHS is not a projection, only way this is true is if LHS normalizes-to RHS |
| 31 | + (Some(alias_lhs), None) => self.assemble_normalizes_to_candidate( |
| 32 | + param_env, |
| 33 | + alias_lhs, |
| 34 | + rhs, |
| 35 | + direction, |
| 36 | + Invert::No, |
| 37 | + ), |
| 38 | + |
| 39 | + // LHS is not a projection, only way this is true is if RHS normalizes-to LHS |
| 40 | + (None, Some(alias_rhs)) => self.assemble_normalizes_to_candidate( |
| 41 | + param_env, |
| 42 | + alias_rhs, |
| 43 | + lhs, |
| 44 | + direction, |
| 45 | + Invert::Yes, |
| 46 | + ), |
| 47 | + |
| 48 | + (Some(alias_lhs), Some(alias_rhs)) => { |
| 49 | + debug!("both sides are aliases"); |
| 50 | + |
| 51 | + let mut candidates = Vec::new(); |
| 52 | + // LHS normalizes-to RHS |
| 53 | + candidates.extend(self.assemble_normalizes_to_candidate( |
| 54 | + param_env, |
| 55 | + alias_lhs, |
| 56 | + rhs, |
| 57 | + direction, |
| 58 | + Invert::No, |
| 59 | + )); |
| 60 | + // RHS normalizes-to RHS |
| 61 | + candidates.extend(self.assemble_normalizes_to_candidate( |
| 62 | + param_env, |
| 63 | + alias_rhs, |
| 64 | + lhs, |
| 65 | + direction, |
| 66 | + Invert::Yes, |
| 67 | + )); |
| 68 | + // Relate via substs |
| 69 | + let subst_relate_response = self |
| 70 | + .assemble_subst_relate_candidate(param_env, alias_lhs, alias_rhs, direction); |
| 71 | + candidates.extend(subst_relate_response); |
| 72 | + debug!(?candidates); |
| 73 | + |
| 74 | + if let Some(merged) = self.try_merge_responses(&candidates) { |
| 75 | + Ok(merged) |
| 76 | + } else { |
| 77 | + // When relating two aliases and we have ambiguity, we prefer |
| 78 | + // relating the generic arguments of the aliases over normalizing |
| 79 | + // them. This is necessary for inference during typeck. |
| 80 | + // |
| 81 | + // As this is incomplete, we must not do so during coherence. |
| 82 | + match self.solver_mode() { |
| 83 | + SolverMode::Normal => { |
| 84 | + if let Ok(subst_relate_response) = subst_relate_response { |
| 85 | + Ok(subst_relate_response) |
| 86 | + } else if let Ok(bidirectional_normalizes_to_response) = self |
| 87 | + .assemble_bidirectional_normalizes_to_candidate( |
| 88 | + param_env, lhs, rhs, direction, |
| 89 | + ) |
| 90 | + { |
| 91 | + Ok(bidirectional_normalizes_to_response) |
| 92 | + } else { |
| 93 | + self.flounder(&candidates) |
| 94 | + } |
| 95 | + } |
| 96 | + SolverMode::Coherence => self.flounder(&candidates), |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + #[instrument(level = "debug", skip(self), ret)] |
| 104 | + fn assemble_normalizes_to_candidate( |
| 105 | + &mut self, |
| 106 | + param_env: ty::ParamEnv<'tcx>, |
| 107 | + alias: ty::AliasTy<'tcx>, |
| 108 | + other: ty::Term<'tcx>, |
| 109 | + direction: ty::AliasRelationDirection, |
| 110 | + invert: Invert, |
| 111 | + ) -> QueryResult<'tcx> { |
| 112 | + self.probe(|ecx| { |
| 113 | + ecx.normalizes_to_inner(param_env, alias, other, direction, invert)?; |
| 114 | + ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + fn normalizes_to_inner( |
| 119 | + &mut self, |
| 120 | + param_env: ty::ParamEnv<'tcx>, |
| 121 | + alias: ty::AliasTy<'tcx>, |
| 122 | + other: ty::Term<'tcx>, |
| 123 | + direction: ty::AliasRelationDirection, |
| 124 | + invert: Invert, |
| 125 | + ) -> Result<(), NoSolution> { |
| 126 | + let other = match direction { |
| 127 | + // This is purely an optimization. |
| 128 | + ty::AliasRelationDirection::Equate => other, |
| 129 | + |
| 130 | + ty::AliasRelationDirection::Subtype => { |
| 131 | + let fresh = self.next_term_infer_of_kind(other); |
| 132 | + let (sub, sup) = match invert { |
| 133 | + Invert::No => (fresh, other), |
| 134 | + Invert::Yes => (other, fresh), |
| 135 | + }; |
| 136 | + self.sub(param_env, sub, sup)?; |
| 137 | + fresh |
| 138 | + } |
| 139 | + }; |
| 140 | + self.add_goal(Goal::new( |
| 141 | + self.tcx(), |
| 142 | + param_env, |
| 143 | + ty::Binder::dummy(ty::ProjectionPredicate { projection_ty: alias, term: other }), |
| 144 | + )); |
| 145 | + |
| 146 | + Ok(()) |
| 147 | + } |
| 148 | + |
| 149 | + fn assemble_subst_relate_candidate( |
| 150 | + &mut self, |
| 151 | + param_env: ty::ParamEnv<'tcx>, |
| 152 | + alias_lhs: ty::AliasTy<'tcx>, |
| 153 | + alias_rhs: ty::AliasTy<'tcx>, |
| 154 | + direction: ty::AliasRelationDirection, |
| 155 | + ) -> QueryResult<'tcx> { |
| 156 | + self.probe(|ecx| { |
| 157 | + match direction { |
| 158 | + ty::AliasRelationDirection::Equate => { |
| 159 | + ecx.eq(param_env, alias_lhs, alias_rhs)?; |
| 160 | + } |
| 161 | + ty::AliasRelationDirection::Subtype => { |
| 162 | + ecx.sub(param_env, alias_lhs, alias_rhs)?; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) |
| 167 | + }) |
| 168 | + } |
| 169 | + |
| 170 | + fn assemble_bidirectional_normalizes_to_candidate( |
| 171 | + &mut self, |
| 172 | + param_env: ty::ParamEnv<'tcx>, |
| 173 | + lhs: ty::Term<'tcx>, |
| 174 | + rhs: ty::Term<'tcx>, |
| 175 | + direction: ty::AliasRelationDirection, |
| 176 | + ) -> QueryResult<'tcx> { |
| 177 | + self.probe(|ecx| { |
| 178 | + ecx.normalizes_to_inner( |
| 179 | + param_env, |
| 180 | + lhs.to_alias_ty(ecx.tcx()).unwrap(), |
| 181 | + rhs, |
| 182 | + direction, |
| 183 | + Invert::No, |
| 184 | + )?; |
| 185 | + ecx.normalizes_to_inner( |
| 186 | + param_env, |
| 187 | + rhs.to_alias_ty(ecx.tcx()).unwrap(), |
| 188 | + lhs, |
| 189 | + direction, |
| 190 | + Invert::Yes, |
| 191 | + )?; |
| 192 | + ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) |
| 193 | + }) |
| 194 | + } |
| 195 | +} |
0 commit comments