|
| 1 | +use rustc_hir as hir; |
| 2 | +use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferCtxt}; |
| 3 | +use rustc_infer::traits::{ImplSource, Obligation, PredicateObligation}; |
| 4 | +use rustc_middle::ty::fast_reject::DeepRejectCtxt; |
| 5 | +use rustc_middle::{span_bug, ty}; |
| 6 | +use rustc_type_ir::solve::NoSolution; |
| 7 | +use thin_vec::ThinVec; |
| 8 | + |
| 9 | +use super::SelectionContext; |
| 10 | + |
| 11 | +pub type HostEffectObligation<'tcx> = Obligation<'tcx, ty::HostEffectPredicate<'tcx>>; |
| 12 | + |
| 13 | +pub enum EvaluationFailure { |
| 14 | + Ambiguous, |
| 15 | + NoSolution, |
| 16 | +} |
| 17 | + |
| 18 | +pub fn evaluate_host_effect_obligation<'tcx>( |
| 19 | + selcx: &mut SelectionContext<'_, 'tcx>, |
| 20 | + obligation: &HostEffectObligation<'tcx>, |
| 21 | +) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> { |
| 22 | + if selcx.infcx.intercrate { |
| 23 | + span_bug!( |
| 24 | + obligation.cause.span, |
| 25 | + "should not select host obligation in old solver in intercrate mode" |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + match evaluate_host_effect_from_bounds(selcx, obligation) { |
| 30 | + Ok(result) => return Ok(result), |
| 31 | + Err(EvaluationFailure::Ambiguous) => return Err(EvaluationFailure::Ambiguous), |
| 32 | + Err(EvaluationFailure::NoSolution) => {} |
| 33 | + } |
| 34 | + |
| 35 | + match evaluate_host_effect_from_selection_candiate(selcx, obligation) { |
| 36 | + Ok(result) => return Ok(result), |
| 37 | + Err(EvaluationFailure::Ambiguous) => return Err(EvaluationFailure::Ambiguous), |
| 38 | + Err(EvaluationFailure::NoSolution) => {} |
| 39 | + } |
| 40 | + |
| 41 | + Err(EvaluationFailure::NoSolution) |
| 42 | +} |
| 43 | + |
| 44 | +fn match_candidate<'tcx>( |
| 45 | + infcx: &InferCtxt<'tcx>, |
| 46 | + obligation: &HostEffectObligation<'tcx>, |
| 47 | + candidate: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>, |
| 48 | +) -> Result<ThinVec<PredicateObligation<'tcx>>, NoSolution> { |
| 49 | + if !candidate.skip_binder().host.satisfies(obligation.predicate.host) { |
| 50 | + return Err(NoSolution); |
| 51 | + } |
| 52 | + |
| 53 | + let candidate = infcx.instantiate_binder_with_fresh_vars( |
| 54 | + obligation.cause.span, |
| 55 | + BoundRegionConversionTime::HigherRankedType, |
| 56 | + candidate, |
| 57 | + ); |
| 58 | + |
| 59 | + let mut nested = infcx |
| 60 | + .at(&obligation.cause, obligation.param_env) |
| 61 | + .eq(DefineOpaqueTypes::Yes, obligation.predicate.trait_ref, candidate.trait_ref)? |
| 62 | + .into_obligations(); |
| 63 | + |
| 64 | + for nested in &mut nested { |
| 65 | + nested.set_depth_from_parent(obligation.recursion_depth); |
| 66 | + } |
| 67 | + |
| 68 | + Ok(nested) |
| 69 | +} |
| 70 | + |
| 71 | +fn evaluate_host_effect_from_bounds<'tcx>( |
| 72 | + selcx: &mut SelectionContext<'_, 'tcx>, |
| 73 | + obligation: &HostEffectObligation<'tcx>, |
| 74 | +) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> { |
| 75 | + let infcx = selcx.infcx; |
| 76 | + let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx()); |
| 77 | + let mut candidate = None; |
| 78 | + |
| 79 | + for predicate in obligation.param_env.caller_bounds() { |
| 80 | + let bound_predicate = predicate.kind(); |
| 81 | + if let ty::ClauseKind::HostEffect(data) = predicate.kind().skip_binder() { |
| 82 | + let data = bound_predicate.rebind(data); |
| 83 | + if data.skip_binder().trait_ref.def_id != obligation.predicate.trait_ref.def_id { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + if !drcx.args_may_unify( |
| 88 | + obligation.predicate.trait_ref.args, |
| 89 | + data.skip_binder().trait_ref.args, |
| 90 | + ) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + let is_match = infcx.probe(|_| match_candidate(infcx, obligation, data).is_ok()); |
| 95 | + |
| 96 | + if is_match { |
| 97 | + if candidate.is_some() { |
| 98 | + return Err(EvaluationFailure::Ambiguous); |
| 99 | + } else { |
| 100 | + candidate = Some(data); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if let Some(data) = candidate { |
| 107 | + Ok(match_candidate(infcx, obligation, data) |
| 108 | + .expect("candidate matched before, so it should match again")) |
| 109 | + } else { |
| 110 | + Err(EvaluationFailure::NoSolution) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +fn evaluate_host_effect_from_selection_candiate<'tcx>( |
| 115 | + selcx: &mut SelectionContext<'_, 'tcx>, |
| 116 | + obligation: &HostEffectObligation<'tcx>, |
| 117 | +) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> { |
| 118 | + let tcx = selcx.tcx(); |
| 119 | + selcx.infcx.commit_if_ok(|_| { |
| 120 | + match selcx.select(&obligation.with(tcx, obligation.predicate.trait_ref)) { |
| 121 | + Ok(None) => Err(EvaluationFailure::Ambiguous), |
| 122 | + Err(_) => Err(EvaluationFailure::NoSolution), |
| 123 | + Ok(Some(source)) => match source { |
| 124 | + ImplSource::UserDefined(impl_) => { |
| 125 | + if tcx.constness(impl_.impl_def_id) != hir::Constness::Const { |
| 126 | + return Err(EvaluationFailure::NoSolution); |
| 127 | + } |
| 128 | + |
| 129 | + let mut nested = impl_.nested; |
| 130 | + nested.extend( |
| 131 | + tcx.const_conditions(impl_.impl_def_id) |
| 132 | + .instantiate(tcx, impl_.args) |
| 133 | + .into_iter() |
| 134 | + .map(|(trait_ref, _)| { |
| 135 | + obligation.with( |
| 136 | + tcx, |
| 137 | + trait_ref.to_host_effect_clause(tcx, obligation.predicate.host), |
| 138 | + ) |
| 139 | + }), |
| 140 | + ); |
| 141 | + |
| 142 | + for nested in &mut nested { |
| 143 | + nested.set_depth_from_parent(obligation.recursion_depth); |
| 144 | + } |
| 145 | + |
| 146 | + Ok(nested) |
| 147 | + } |
| 148 | + _ => Err(EvaluationFailure::NoSolution), |
| 149 | + }, |
| 150 | + } |
| 151 | + }) |
| 152 | +} |
0 commit comments