1 //! Defines a Chalk-based `TraitEngine`
3 use crate::infer::canonical::OriginalQueryValues;
4 use crate::infer::InferCtxt;
5 use crate::traits::query::NoSolution;
7 ChalkEnvironmentAndGoal, FulfillmentError, FulfillmentErrorCode, ObligationCause,
8 PredicateObligation, SelectionError, TraitEngine,
10 use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
11 use rustc_middle::ty::{self, Ty, TypeVisitable};
13 pub struct FulfillmentContext<'tcx> {
14 obligations: FxIndexSet<PredicateObligation<'tcx>>,
16 relationships: FxHashMap<ty::TyVid, ty::FoundRelationships>,
19 impl FulfillmentContext<'_> {
20 pub(crate) fn new() -> Self {
22 obligations: FxIndexSet::default(),
23 relationships: FxHashMap::default(),
28 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
29 fn normalize_projection_type(
31 infcx: &InferCtxt<'tcx>,
32 _param_env: ty::ParamEnv<'tcx>,
33 projection_ty: ty::ProjectionTy<'tcx>,
34 _cause: ObligationCause<'tcx>,
36 infcx.tcx.mk_ty(ty::Projection(projection_ty))
39 fn register_predicate_obligation(
41 infcx: &InferCtxt<'tcx>,
42 obligation: PredicateObligation<'tcx>,
44 assert!(!infcx.is_in_snapshot());
45 let obligation = infcx.resolve_vars_if_possible(obligation);
47 super::relationships::update(self, infcx, &obligation);
49 self.obligations.insert(obligation);
52 fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
54 let errors = self.select_where_possible(infcx);
56 if !errors.is_empty() {
61 // any remaining obligations are errors
64 .map(|obligation| FulfillmentError {
65 obligation: obligation.clone(),
66 code: FulfillmentErrorCode::CodeAmbiguity,
67 // FIXME - does Chalk have a notation of 'root obligation'?
68 // This is just for diagnostics, so it's okay if this is wrong
69 root_obligation: obligation.clone(),
74 fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
75 assert!(!infcx.is_in_snapshot());
77 let mut errors = Vec::new();
78 let mut next_round = FxIndexSet::default();
79 let mut making_progress;
82 making_progress = false;
84 // We iterate over all obligations, and record if we are able
85 // to unambiguously prove at least one obligation.
86 for obligation in self.obligations.drain(..) {
87 let obligation = infcx.resolve_vars_if_possible(obligation);
88 let environment = obligation.param_env.caller_bounds();
89 let goal = ChalkEnvironmentAndGoal { environment, goal: obligation.predicate };
90 let mut orig_values = OriginalQueryValues::default();
91 if goal.references_error() {
96 infcx.canonicalize_query_preserving_universes(goal, &mut orig_values);
98 match infcx.tcx.evaluate_goal(canonical_goal) {
100 if response.is_proven() {
101 making_progress = true;
103 match infcx.instantiate_query_response_and_region_obligations(
105 obligation.param_env,
109 Ok(infer_ok) => next_round.extend(
110 infer_ok.obligations.into_iter().map(|obligation| {
111 assert!(!infcx.is_in_snapshot());
112 infcx.resolve_vars_if_possible(obligation)
116 Err(_err) => errors.push(FulfillmentError {
117 obligation: obligation.clone(),
118 code: FulfillmentErrorCode::CodeSelectionError(
119 SelectionError::Unimplemented,
121 // FIXME - does Chalk have a notation of 'root obligation'?
122 // This is just for diagnostics, so it's okay if this is wrong
123 root_obligation: obligation,
127 // Ambiguous: retry at next round.
128 next_round.insert(obligation);
132 Err(NoSolution) => errors.push(FulfillmentError {
133 obligation: obligation.clone(),
134 code: FulfillmentErrorCode::CodeSelectionError(
135 SelectionError::Unimplemented,
137 // FIXME - does Chalk have a notation of 'root obligation'?
138 // This is just for diagnostics, so it's okay if this is wrong
139 root_obligation: obligation,
143 next_round = std::mem::replace(&mut self.obligations, next_round);
145 if !making_progress {
153 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
154 self.obligations.iter().cloned().collect()
157 fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships> {
158 &mut self.relationships