11 use crate::traits::query::NoSolution;
12 use crate::infer::InferCtxt;
13 use crate::infer::canonical::{Canonical, OriginalQueryValues};
14 use crate::ty::{self, Ty};
15 use rustc_data_structures::fx::FxHashSet;
17 pub type CanonicalGoal<'tcx> = Canonical<'tcx, InEnvironment<'tcx, ty::Predicate<'tcx>>>;
19 pub struct FulfillmentContext<'tcx> {
20 obligations: FxHashSet<InEnvironment<'tcx, PredicateObligation<'tcx>>>,
23 impl FulfillmentContext<'tcx> {
24 crate fn new() -> Self {
26 obligations: FxHashSet::default(),
32 infcx: &InferCtxt<'_, 'tcx>,
33 obligation: PredicateObligation<'tcx>,
34 ) -> InEnvironment<'tcx, PredicateObligation<'tcx>> {
35 assert!(!infcx.is_in_snapshot());
36 let obligation = infcx.resolve_vars_if_possible(&obligation);
38 let environment = match obligation.param_env.def_id {
39 Some(def_id) => infcx.tcx.environment(def_id),
40 None if obligation.param_env.caller_bounds.is_empty() => Environment {
41 clauses: ty::List::empty(),
43 _ => bug!("non-empty `ParamEnv` with no def-id"),
52 impl TraitEngine<'tcx> for FulfillmentContext<'tcx> {
53 fn normalize_projection_type(
55 infcx: &InferCtxt<'_, 'tcx>,
56 _param_env: ty::ParamEnv<'tcx>,
57 projection_ty: ty::ProjectionTy<'tcx>,
58 _cause: ObligationCause<'tcx>,
60 infcx.tcx.mk_ty(ty::Projection(projection_ty))
63 fn register_predicate_obligation(
65 infcx: &InferCtxt<'_, 'tcx>,
66 obligation: PredicateObligation<'tcx>,
68 self.obligations.insert(in_environment(infcx, obligation));
71 fn select_all_or_error(
73 infcx: &InferCtxt<'_, 'tcx>,
74 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
75 self.select_where_possible(infcx)?;
77 if self.obligations.is_empty() {
80 let errors = self.obligations.iter()
81 .map(|obligation| FulfillmentError {
82 obligation: obligation.goal.clone(),
83 code: FulfillmentErrorCode::CodeAmbiguity,
84 points_at_arg_span: false,
91 fn select_where_possible(
93 infcx: &InferCtxt<'_, 'tcx>,
94 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
95 let mut errors = Vec::new();
96 let mut next_round = FxHashSet::default();
97 let mut making_progress;
100 making_progress = false;
102 // We iterate over all obligations, and record if we are able
103 // to unambiguously prove at least one obligation.
104 for obligation in self.obligations.drain() {
105 let mut orig_values = OriginalQueryValues::default();
106 let canonical_goal = infcx.canonicalize_query(&InEnvironment {
107 environment: obligation.environment,
108 goal: obligation.goal.predicate,
109 }, &mut orig_values);
111 match infcx.tcx.evaluate_goal(canonical_goal) {
113 if response.is_proven() {
114 making_progress = true;
116 match infcx.instantiate_query_response_and_region_obligations(
117 &obligation.goal.cause,
118 obligation.goal.param_env,
122 Ok(infer_ok) => next_round.extend(
125 .map(|obligation| in_environment(infcx, obligation))
128 Err(_err) => errors.push(FulfillmentError {
129 obligation: obligation.goal,
130 code: FulfillmentErrorCode::CodeSelectionError(
131 SelectionError::Unimplemented
133 points_at_arg_span: false,
137 // Ambiguous: retry at next round.
138 next_round.insert(obligation);
142 Err(NoSolution) => errors.push(FulfillmentError {
143 obligation: obligation.goal,
144 code: FulfillmentErrorCode::CodeSelectionError(
145 SelectionError::Unimplemented
147 points_at_arg_span: false,
151 next_round = std::mem::replace(&mut self.obligations, next_round);
153 if !making_progress {
158 if errors.is_empty() {
165 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
166 self.obligations.iter().map(|obligation| obligation.goal.clone()).collect()