]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/type_check/canonical.rs
Rollup merge of #89235 - yaahc:junit-formatting, r=kennytm
[rust.git] / compiler / rustc_borrowck / src / type_check / canonical.rs
1 use std::fmt;
2
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;
8 use rustc_span::Span;
9 use rustc_trait_selection::traits::query::type_op::{self, TypeOpOutput};
10 use rustc_trait_selection::traits::query::Fallible;
11
12 use crate::diagnostics::{ToUniverseInfo, UniverseInfo};
13
14 use super::{Locations, NormalizeLocation, TypeChecker};
15
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
22     /// `locations`.
23     ///
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     pub(super) fn fully_perform_op<R, Op>(
28         &mut self,
29         locations: Locations,
30         category: ConstraintCategory,
31         op: Op,
32     ) -> Fallible<R>
33     where
34         Op: type_op::TypeOp<'tcx, Output = R>,
35         Canonical<'tcx, Op>: ToUniverseInfo<'tcx>,
36     {
37         let old_universe = self.infcx.universe();
38
39         let TypeOpOutput { output, constraints, canonicalized_query } =
40             op.fully_perform(self.infcx)?;
41
42         if let Some(data) = &constraints {
43             self.push_region_constraints(locations, category, data);
44         }
45
46         let universe = self.infcx.universe();
47
48         if old_universe != universe {
49             let universe_info = match canonicalized_query {
50                 Some(canonicalized_query) => canonicalized_query.to_universe_info(old_universe),
51                 None => UniverseInfo::other(),
52             };
53             for u in old_universe..universe {
54                 self.borrowck_context
55                     .constraints
56                     .universe_causes
57                     .insert(u + 1, universe_info.clone());
58             }
59         }
60
61         Ok(output)
62     }
63
64     pub(super) fn instantiate_canonical_with_fresh_inference_vars<T>(
65         &mut self,
66         span: Span,
67         canonical: &Canonical<'tcx, T>,
68     ) -> T
69     where
70         T: TypeFoldable<'tcx>,
71     {
72         let (instantiated, _) =
73             self.infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
74
75         for u in 0..canonical.max_universe.as_u32() {
76             let info = UniverseInfo::other();
77             self.borrowck_context
78                 .constraints
79                 .universe_causes
80                 .insert(ty::UniverseIndex::from_u32(u), info);
81         }
82
83         instantiated
84     }
85
86     pub(super) fn prove_trait_ref(
87         &mut self,
88         trait_ref: ty::TraitRef<'tcx>,
89         locations: Locations,
90         category: ConstraintCategory,
91     ) {
92         self.prove_predicates(
93             Some(ty::Binder::dummy(ty::PredicateKind::Trait(ty::TraitPredicate {
94                 trait_ref,
95                 constness: ty::BoundConstness::NotConst,
96             }))),
97             locations,
98             category,
99         );
100     }
101
102     pub(super) fn normalize_and_prove_instantiated_predicates(
103         &mut self,
104         // Keep this parameter for now, in case we start using
105         // it in `ConstraintCategory` at some point.
106         _def_id: DefId,
107         instantiated_predicates: ty::InstantiatedPredicates<'tcx>,
108         locations: Locations,
109     ) {
110         for (predicate, span) in instantiated_predicates
111             .predicates
112             .into_iter()
113             .zip(instantiated_predicates.spans.into_iter())
114         {
115             let predicate = self.normalize(predicate, locations);
116             self.prove_predicate(predicate, locations, ConstraintCategory::Predicate(span));
117         }
118     }
119
120     pub(super) fn prove_predicates(
121         &mut self,
122         predicates: impl IntoIterator<Item = impl ToPredicate<'tcx>>,
123         locations: Locations,
124         category: ConstraintCategory,
125     ) {
126         for predicate in predicates {
127             let predicate = predicate.to_predicate(self.tcx());
128             debug!("prove_predicates(predicate={:?}, locations={:?})", predicate, locations,);
129
130             self.prove_predicate(predicate, locations, category);
131         }
132     }
133
134     pub(super) fn prove_predicate(
135         &mut self,
136         predicate: ty::Predicate<'tcx>,
137         locations: Locations,
138         category: ConstraintCategory,
139     ) {
140         debug!("prove_predicate(predicate={:?}, location={:?})", predicate, locations,);
141
142         let param_env = self.param_env;
143         self.fully_perform_op(
144             locations,
145             category,
146             param_env.and(type_op::prove_predicate::ProvePredicate::new(predicate)),
147         )
148         .unwrap_or_else(|NoSolution| {
149             span_mirbug!(self, NoSolution, "could not prove {:?}", predicate);
150         })
151     }
152
153     pub(super) fn normalize<T>(&mut self, value: T, location: impl NormalizeLocation) -> T
154     where
155         T: type_op::normalize::Normalizable<'tcx> + fmt::Display + Copy + 'tcx,
156     {
157         debug!("normalize(value={:?}, location={:?})", value, location);
158         let param_env = self.param_env;
159         self.fully_perform_op(
160             location.to_locations(),
161             ConstraintCategory::Boring,
162             param_env.and(type_op::normalize::Normalize::new(value)),
163         )
164         .unwrap_or_else(|NoSolution| {
165             span_mirbug!(self, NoSolution, "failed to normalize `{:?}`", value);
166             value
167         })
168     }
169 }