]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/solve/fulfill.rs
Replacing bound vars is actually instantiating a binder
[rust.git] / compiler / rustc_trait_selection / src / solve / fulfill.rs
1 use std::mem;
2
3 use rustc_infer::infer::InferCtxt;
4 use rustc_infer::traits::{
5     query::NoSolution, FulfillmentError, FulfillmentErrorCode, MismatchedProjectionTypes,
6     PredicateObligation, SelectionError, TraitEngine,
7 };
8 use rustc_middle::ty;
9 use rustc_middle::ty::error::{ExpectedFound, TypeError};
10
11 use super::{Certainty, InferCtxtEvalExt};
12
13 /// A trait engine using the new trait solver.
14 ///
15 /// This is mostly identical to how `evaluate_all` works inside of the
16 /// solver, except that the requirements are slightly different.
17 ///
18 /// Unlike `evaluate_all` it is possible to add new obligations later on
19 /// and we also have to track diagnostics information by using `Obligation`
20 /// instead of `Goal`.
21 ///
22 /// It is also likely that we want to use slightly different datastructures
23 /// here as this will have to deal with far more root goals than `evaluate_all`.
24 pub struct FulfillmentCtxt<'tcx> {
25     obligations: Vec<PredicateObligation<'tcx>>,
26 }
27
28 impl<'tcx> FulfillmentCtxt<'tcx> {
29     pub fn new() -> FulfillmentCtxt<'tcx> {
30         FulfillmentCtxt { obligations: Vec::new() }
31     }
32 }
33
34 impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
35     fn register_predicate_obligation(
36         &mut self,
37         _infcx: &InferCtxt<'tcx>,
38         obligation: PredicateObligation<'tcx>,
39     ) {
40         self.obligations.push(obligation);
41     }
42
43     fn collect_remaining_errors(&mut self) -> Vec<FulfillmentError<'tcx>> {
44         self.obligations
45             .drain(..)
46             .map(|obligation| FulfillmentError {
47                 obligation: obligation.clone(),
48                 code: FulfillmentErrorCode::CodeAmbiguity,
49                 root_obligation: obligation,
50             })
51             .collect()
52     }
53
54     fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
55         let mut errors = Vec::new();
56         for i in 0.. {
57             if !infcx.tcx.recursion_limit().value_within_limit(i) {
58                 unimplemented!("overflowed on pending obligations: {:?}", self.obligations);
59             }
60
61             let mut has_changed = false;
62             for obligation in mem::take(&mut self.obligations) {
63                 let goal = obligation.clone().into();
64                 let (changed, certainty) = match infcx.evaluate_root_goal(goal) {
65                     Ok(result) => result,
66                     Err(NoSolution) => {
67                         errors.push(FulfillmentError {
68                             obligation: obligation.clone(),
69                             code: match goal.predicate.kind().skip_binder() {
70                                 ty::PredicateKind::Clause(ty::Clause::Projection(_)) => {
71                                     FulfillmentErrorCode::CodeProjectionError(
72                                         // FIXME: This could be a `Sorts` if the term is a type
73                                         MismatchedProjectionTypes { err: TypeError::Mismatch },
74                                     )
75                                 }
76                                 ty::PredicateKind::Subtype(pred) => {
77                                     let (a, b) = infcx.instantiate_binder_with_placeholders(
78                                         goal.predicate.kind().rebind((pred.a, pred.b)),
79                                     );
80                                     let expected_found = ExpectedFound::new(true, a, b);
81                                     FulfillmentErrorCode::CodeSubtypeError(
82                                         expected_found,
83                                         TypeError::Sorts(expected_found),
84                                     )
85                                 }
86                                 ty::PredicateKind::Coerce(pred) => {
87                                     let (a, b) = infcx.instantiate_binder_with_placeholders(
88                                         goal.predicate.kind().rebind((pred.a, pred.b)),
89                                     );
90                                     let expected_found = ExpectedFound::new(false, a, b);
91                                     FulfillmentErrorCode::CodeSubtypeError(
92                                         expected_found,
93                                         TypeError::Sorts(expected_found),
94                                     )
95                                 }
96                                 ty::PredicateKind::ConstEquate(a, b) => {
97                                     let (a, b) = infcx.instantiate_binder_with_placeholders(
98                                         goal.predicate.kind().rebind((a, b)),
99                                     );
100                                     let expected_found = ExpectedFound::new(true, a, b);
101                                     FulfillmentErrorCode::CodeConstEquateError(
102                                         expected_found,
103                                         TypeError::ConstMismatch(expected_found),
104                                     )
105                                 }
106                                 ty::PredicateKind::Clause(_)
107                                 | ty::PredicateKind::WellFormed(_)
108                                 | ty::PredicateKind::ObjectSafe(_)
109                                 | ty::PredicateKind::ClosureKind(_, _, _)
110                                 | ty::PredicateKind::ConstEvaluatable(_)
111                                 | ty::PredicateKind::TypeWellFormedFromEnv(_)
112                                 | ty::PredicateKind::Ambiguous => {
113                                     FulfillmentErrorCode::CodeSelectionError(
114                                         SelectionError::Unimplemented,
115                                     )
116                                 }
117                             },
118                             root_obligation: obligation,
119                         });
120                         continue;
121                     }
122                 };
123
124                 has_changed |= changed;
125                 match certainty {
126                     Certainty::Yes => {}
127                     Certainty::Maybe(_) => self.obligations.push(obligation),
128                 }
129             }
130
131             if !has_changed {
132                 break;
133             }
134         }
135
136         errors
137     }
138
139     fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
140         self.obligations.clone()
141     }
142
143     fn drain_unstalled_obligations(
144         &mut self,
145         _: &InferCtxt<'tcx>,
146     ) -> Vec<PredicateObligation<'tcx>> {
147         unimplemented!()
148     }
149 }