rustc_trait_selection/traits/query/
evaluate_obligation.rs

1use rustc_macros::extension;
2use rustc_middle::span_bug;
3
4use crate::infer::InferCtxt;
5use crate::infer::canonical::OriginalQueryValues;
6use crate::traits::{
7    EvaluationResult, ObligationCtxt, OverflowError, PredicateObligation, SelectionContext,
8};
9
10#[extension(pub trait InferCtxtExt<'tcx>)]
11impl<'tcx> InferCtxt<'tcx> {
12    /// Evaluates whether the predicate can be satisfied (by any means)
13    /// in the given `ParamEnv`.
14    fn predicate_may_hold(&self, obligation: &PredicateObligation<'tcx>) -> bool {
15        self.evaluate_obligation_no_overflow(obligation).may_apply()
16    }
17
18    /// Evaluates whether the predicate can be satisfied in the given
19    /// `ParamEnv`, and returns `false` if not certain. However, this is
20    /// not entirely accurate if inference variables are involved.
21    ///
22    /// This version may conservatively fail when outlives obligations
23    /// are required. Therefore, this version should only be used for
24    /// optimizations or diagnostics and be treated as if it can always
25    /// return `false`.
26    ///
27    /// # Example
28    ///
29    /// ```
30    /// # #![allow(dead_code)]
31    /// trait Trait {}
32    ///
33    /// fn check<T: Trait>() {}
34    ///
35    /// fn foo<T: 'static>()
36    /// where
37    ///     &'static T: Trait,
38    /// {
39    ///     // Evaluating `&'?0 T: Trait` adds a `'?0: 'static` outlives obligation,
40    ///     // which means that `predicate_must_hold_considering_regions` will return
41    ///     // `false`.
42    ///     check::<&'_ T>();
43    /// }
44    /// ```
45    fn predicate_must_hold_considering_regions(
46        &self,
47        obligation: &PredicateObligation<'tcx>,
48    ) -> bool {
49        self.evaluate_obligation_no_overflow(obligation).must_apply_considering_regions()
50    }
51
52    /// Evaluates whether the predicate can be satisfied in the given
53    /// `ParamEnv`, and returns `false` if not certain. However, this is
54    /// not entirely accurate if inference variables are involved.
55    ///
56    /// This version ignores all outlives constraints.
57    fn predicate_must_hold_modulo_regions(&self, obligation: &PredicateObligation<'tcx>) -> bool {
58        self.evaluate_obligation_no_overflow(obligation).must_apply_modulo_regions()
59    }
60
61    /// Evaluate a given predicate, capturing overflow and propagating it back.
62    fn evaluate_obligation(
63        &self,
64        obligation: &PredicateObligation<'tcx>,
65    ) -> Result<EvaluationResult, OverflowError> {
66        let mut _orig_values = OriginalQueryValues::default();
67
68        let param_env = obligation.param_env;
69
70        if self.next_trait_solver() {
71            self.probe(|snapshot| {
72                let ocx = ObligationCtxt::new(self);
73                ocx.register_obligation(obligation.clone());
74                let mut result = EvaluationResult::EvaluatedToOk;
75                for error in ocx.select_all_or_error() {
76                    if error.is_true_error() {
77                        return Ok(EvaluationResult::EvaluatedToErr);
78                    } else {
79                        result = result.max(EvaluationResult::EvaluatedToAmbig);
80                    }
81                }
82                if self.opaque_types_added_in_snapshot(snapshot) {
83                    result = result.max(EvaluationResult::EvaluatedToOkModuloOpaqueTypes);
84                } else if self.region_constraints_added_in_snapshot(snapshot) {
85                    result = result.max(EvaluationResult::EvaluatedToOkModuloRegions);
86                }
87                Ok(result)
88            })
89        } else {
90            let c_pred =
91                self.canonicalize_query(param_env.and(obligation.predicate), &mut _orig_values);
92            self.tcx.at(obligation.cause.span).evaluate_obligation(c_pred)
93        }
94    }
95
96    /// Helper function that canonicalizes and runs the query. If an
97    /// overflow results, we re-run it in the local context so we can
98    /// report a nice error.
99    fn evaluate_obligation_no_overflow(
100        &self,
101        obligation: &PredicateObligation<'tcx>,
102    ) -> EvaluationResult {
103        // Run canonical query. If overflow occurs, rerun from scratch but this time
104        // in standard trait query mode so that overflow is handled appropriately
105        // within `SelectionContext`.
106        match self.evaluate_obligation(obligation) {
107            Ok(result) => result,
108            Err(OverflowError::Canonical) => {
109                let mut selcx = SelectionContext::new(self);
110                selcx.evaluate_root_obligation(obligation).unwrap_or_else(|r| match r {
111                    OverflowError::Canonical => {
112                        span_bug!(
113                            obligation.cause.span,
114                            "Overflow should be caught earlier in standard query mode: {:?}, {:?}",
115                            obligation,
116                            r,
117                        )
118                    }
119                    OverflowError::Error(_) => EvaluationResult::EvaluatedToErr,
120                })
121            }
122            Err(OverflowError::Error(_)) => EvaluationResult::EvaluatedToErr,
123        }
124    }
125}