]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/type_check/canonical.rs
Merge from rustc
[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     #[instrument(skip(self, op), level = "trace")]
28     pub(super) fn fully_perform_op<R: fmt::Debug, Op>(
29         &mut self,
30         locations: Locations,
31         category: ConstraintCategory<'tcx>,
32         op: Op,
33     ) -> Fallible<R>
34     where
35         Op: type_op::TypeOp<'tcx, Output = R>,
36         Op::ErrorInfo: ToUniverseInfo<'tcx>,
37     {
38         let old_universe = self.infcx.universe();
39
40         let TypeOpOutput { output, constraints, error_info } = op.fully_perform(self.infcx)?;
41
42         debug!(?output, ?constraints);
43
44         if let Some(data) = constraints {
45             self.push_region_constraints(locations, category, data);
46         }
47
48         let universe = self.infcx.universe();
49
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(),
54             };
55             for u in (old_universe + 1)..=universe {
56                 self.borrowck_context.constraints.universe_causes.insert(u, universe_info.clone());
57             }
58         }
59
60         Ok(output)
61     }
62
63     pub(super) fn instantiate_canonical_with_fresh_inference_vars<T>(
64         &mut self,
65         span: Span,
66         canonical: &Canonical<'tcx, T>,
67     ) -> T
68     where
69         T: TypeFoldable<'tcx>,
70     {
71         let old_universe = self.infcx.universe();
72
73         let (instantiated, _) =
74             self.infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
75
76         for u in (old_universe + 1)..=self.infcx.universe() {
77             self.borrowck_context.constraints.universe_causes.insert(u, UniverseInfo::other());
78         }
79
80         instantiated
81     }
82
83     #[instrument(skip(self), level = "debug")]
84     pub(super) fn prove_trait_ref(
85         &mut self,
86         trait_ref: ty::TraitRef<'tcx>,
87         locations: Locations,
88         category: ConstraintCategory<'tcx>,
89     ) {
90         self.prove_predicate(
91             ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::Trait(ty::TraitPredicate {
92                 trait_ref,
93                 constness: ty::BoundConstness::NotConst,
94                 polarity: ty::ImplPolarity::Positive,
95             }))),
96             locations,
97             category,
98         );
99     }
100
101     #[instrument(level = "debug", skip(self))]
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             debug!(?predicate);
116             let category = ConstraintCategory::Predicate(span);
117             let predicate = self.normalize_with_category(predicate, locations, category);
118             self.prove_predicate(predicate, locations, category);
119         }
120     }
121
122     pub(super) fn prove_predicates(
123         &mut self,
124         predicates: impl IntoIterator<
125             Item = impl ToPredicate<'tcx, ty::Predicate<'tcx>> + std::fmt::Debug,
126         >,
127         locations: Locations,
128         category: ConstraintCategory<'tcx>,
129     ) {
130         for predicate in predicates {
131             self.prove_predicate(predicate, locations, category);
132         }
133     }
134
135     #[instrument(skip(self), level = "debug")]
136     pub(super) fn prove_predicate(
137         &mut self,
138         predicate: impl ToPredicate<'tcx, ty::Predicate<'tcx>> + std::fmt::Debug,
139         locations: Locations,
140         category: ConstraintCategory<'tcx>,
141     ) {
142         let param_env = self.param_env;
143         let predicate = predicate.to_predicate(self.tcx());
144         self.fully_perform_op(
145             locations,
146             category,
147             param_env.and(type_op::prove_predicate::ProvePredicate::new(predicate)),
148         )
149         .unwrap_or_else(|NoSolution| {
150             span_mirbug!(self, NoSolution, "could not prove {:?}", predicate);
151         })
152     }
153
154     pub(super) fn normalize<T>(&mut self, value: T, location: impl NormalizeLocation) -> T
155     where
156         T: type_op::normalize::Normalizable<'tcx> + fmt::Display + Copy + 'tcx,
157     {
158         self.normalize_with_category(value, location, ConstraintCategory::Boring)
159     }
160
161     #[instrument(skip(self), level = "debug")]
162     pub(super) fn normalize_with_category<T>(
163         &mut self,
164         value: T,
165         location: impl NormalizeLocation,
166         category: ConstraintCategory<'tcx>,
167     ) -> T
168     where
169         T: type_op::normalize::Normalizable<'tcx> + fmt::Display + Copy + 'tcx,
170     {
171         let param_env = self.param_env;
172         self.fully_perform_op(
173             location.to_locations(),
174             category,
175             param_env.and(type_op::normalize::Normalize::new(value)),
176         )
177         .unwrap_or_else(|NoSolution| {
178             span_mirbug!(self, NoSolution, "failed to normalize `{:?}`", value);
179             value
180         })
181     }
182 }