]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/evaluate_obligation.rs
Refactor mod/check (part vii)
[rust.git] / src / librustc_traits / evaluate_obligation.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::traits::{EvaluationResult, Obligation, ObligationCause,
12                     OverflowError, SelectionContext, TraitQueryMode};
13 use rustc::traits::query::CanonicalPredicateGoal;
14 use rustc::ty::query::Providers;
15 use rustc::ty::{ParamEnvAnd, TyCtxt};
16 use syntax::codemap::DUMMY_SP;
17
18 crate fn provide(p: &mut Providers) {
19     *p = Providers {
20         evaluate_obligation,
21         ..*p
22     };
23 }
24
25 fn evaluate_obligation<'tcx>(
26     tcx: TyCtxt<'_, 'tcx, 'tcx>,
27     goal: CanonicalPredicateGoal<'tcx>,
28 ) -> Result<EvaluationResult, OverflowError> {
29     tcx.infer_ctxt().enter(|ref infcx| {
30         let (
31             ParamEnvAnd {
32                 param_env,
33                 value: predicate,
34             },
35             _canonical_inference_vars,
36         ) = infcx.instantiate_canonical_with_fresh_inference_vars(DUMMY_SP, &goal);
37
38         let mut selcx = SelectionContext::with_query_mode(&infcx, TraitQueryMode::Canonical);
39         let obligation = Obligation::new(ObligationCause::dummy(), param_env, predicate);
40
41         selcx.evaluate_obligation_recursively(&obligation)
42     })
43 }