3 use rustc_infer::infer::canonical::Canonical;
4 use rustc_infer::traits::query::NoSolution;
5 use rustc_middle::mir::ConstraintCategory;
6 use rustc_middle::ty::{self, ToPredicate, TypeFoldable};
7 use rustc_span::def_id::DefId;
9 use rustc_trait_selection::traits::query::type_op::{self, TypeOpOutput};
10 use rustc_trait_selection::traits::query::Fallible;
12 use crate::diagnostics::{ToUniverseInfo, UniverseInfo};
14 use super::{Locations, NormalizeLocation, TypeChecker};
16 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
17 /// Given some operation `op` that manipulates types, proves
18 /// predicates, or otherwise uses the inference context, executes
19 /// `op` and then executes all the further obligations that `op`
20 /// returns. This will yield a set of outlives constraints amongst
21 /// regions which are extracted and stored as having occurred at
24 /// **Any `rustc_infer::infer` operations that might generate region
25 /// constraints should occur within this method so that those
26 /// constraints can be properly localized!**
27 #[instrument(skip(self, op), level = "trace")]
28 pub(super) fn fully_perform_op<R: fmt::Debug, Op>(
31 category: ConstraintCategory<'tcx>,
35 Op: type_op::TypeOp<'tcx, Output = R>,
36 Op::ErrorInfo: ToUniverseInfo<'tcx>,
38 let old_universe = self.infcx.universe();
40 let TypeOpOutput { output, constraints, error_info } = op.fully_perform(self.infcx)?;
42 debug!(?output, ?constraints);
44 if let Some(data) = constraints {
45 self.push_region_constraints(locations, category, data);
48 let universe = self.infcx.universe();
50 if old_universe != universe {
51 let universe_info = match error_info {
52 Some(error_info) => error_info.to_universe_info(old_universe),
53 None => UniverseInfo::other(),
55 for u in old_universe..universe {
59 .insert(u + 1, universe_info.clone());
66 pub(super) fn instantiate_canonical_with_fresh_inference_vars<T>(
69 canonical: &Canonical<'tcx, T>,
72 T: TypeFoldable<'tcx>,
74 let (instantiated, _) =
75 self.infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
77 for u in 0..canonical.max_universe.as_u32() {
78 let info = UniverseInfo::other();
82 .insert(ty::UniverseIndex::from_u32(u), info);
88 #[instrument(skip(self), level = "debug")]
89 pub(super) fn prove_trait_ref(
91 trait_ref: ty::TraitRef<'tcx>,
93 category: ConstraintCategory<'tcx>,
96 ty::Binder::dummy(ty::PredicateKind::Trait(ty::TraitPredicate {
98 constness: ty::BoundConstness::NotConst,
99 polarity: ty::ImplPolarity::Positive,
101 .to_predicate(self.tcx()),
107 pub(super) fn normalize_and_prove_instantiated_predicates(
109 // Keep this parameter for now, in case we start using
110 // it in `ConstraintCategory` at some point.
112 instantiated_predicates: ty::InstantiatedPredicates<'tcx>,
113 locations: Locations,
115 for (predicate, span) in instantiated_predicates
118 .zip(instantiated_predicates.spans.into_iter())
121 let predicate = self.normalize(predicate, locations);
122 self.prove_predicate(predicate, locations, ConstraintCategory::Predicate(span));
126 pub(super) fn prove_predicates(
128 predicates: impl IntoIterator<Item = impl ToPredicate<'tcx>>,
129 locations: Locations,
130 category: ConstraintCategory<'tcx>,
132 for predicate in predicates {
133 let predicate = predicate.to_predicate(self.tcx());
134 debug!("prove_predicates(predicate={:?}, locations={:?})", predicate, locations,);
136 self.prove_predicate(predicate, locations, category);
140 #[instrument(skip(self), level = "debug")]
141 pub(super) fn prove_predicate(
143 predicate: ty::Predicate<'tcx>,
144 locations: Locations,
145 category: ConstraintCategory<'tcx>,
147 let param_env = self.param_env;
148 self.fully_perform_op(
151 param_env.and(type_op::prove_predicate::ProvePredicate::new(predicate)),
153 .unwrap_or_else(|NoSolution| {
154 span_mirbug!(self, NoSolution, "could not prove {:?}", predicate);
158 #[instrument(skip(self), level = "debug")]
159 pub(super) fn normalize<T>(&mut self, value: T, location: impl NormalizeLocation) -> T
161 T: type_op::normalize::Normalizable<'tcx> + fmt::Display + Copy + 'tcx,
163 let param_env = self.param_env;
164 self.fully_perform_op(
165 location.to_locations(),
166 ConstraintCategory::Boring,
167 param_env.and(type_op::normalize::Normalize::new(value)),
169 .unwrap_or_else(|NoSolution| {
170 span_mirbug!(self, NoSolution, "failed to normalize `{:?}`", value);