|
| 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 infer::InferCtxt; |
| 12 | +use infer::canonical::{Canonical, Canonicalize}; |
| 13 | +use traits::EvaluationResult; |
| 14 | +use traits::query::CanonicalPredicateGoal; |
| 15 | +use ty::{ParamEnv, ParamEnvAnd, Predicate, TyCtxt}; |
| 16 | + |
| 17 | +impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> { |
| 18 | + /// Evaluates whether the predicate can be satisfied (by any means) |
| 19 | + /// in the given `ParamEnv`. |
| 20 | + pub fn predicate_may_hold( |
| 21 | + &self, |
| 22 | + param_env: ParamEnv<'tcx>, |
| 23 | + predicate: Predicate<'tcx>, |
| 24 | + ) -> bool { |
| 25 | + self.evaluate_obligation(param_env, predicate).may_apply() |
| 26 | + } |
| 27 | + |
| 28 | + /// Evaluates whether the predicate can be satisfied in the given |
| 29 | + /// `ParamEnv`, and returns `false` if not certain. However, this is |
| 30 | + /// not entirely accurate if inference variables are involved. |
| 31 | + pub fn predicate_must_hold( |
| 32 | + &self, |
| 33 | + param_env: ParamEnv<'tcx>, |
| 34 | + predicate: Predicate<'tcx>, |
| 35 | + ) -> bool { |
| 36 | + self.evaluate_obligation(param_env, predicate) == EvaluationResult::EvaluatedToOk |
| 37 | + } |
| 38 | + |
| 39 | + fn evaluate_obligation( |
| 40 | + &self, |
| 41 | + param_env: ParamEnv<'tcx>, |
| 42 | + predicate: Predicate<'tcx>, |
| 43 | + ) -> EvaluationResult { |
| 44 | + let (c_pred, _) = self.canonicalize_query(¶m_env.and(predicate)); |
| 45 | + |
| 46 | + self.tcx.global_tcx().evaluate_obligation(c_pred) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<'gcx: 'tcx, 'tcx> Canonicalize<'gcx, 'tcx> for ParamEnvAnd<'tcx, Predicate<'tcx>> { |
| 51 | + type Canonicalized = CanonicalPredicateGoal<'gcx>; |
| 52 | + |
| 53 | + fn intern( |
| 54 | + _gcx: TyCtxt<'_, 'gcx, 'gcx>, |
| 55 | + value: Canonical<'gcx, Self::Lifted>, |
| 56 | + ) -> Self::Canonicalized { |
| 57 | + value |
| 58 | + } |
| 59 | +} |
0 commit comments