]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/type_check/canonical.rs
Better errors for implied static bound
[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..universe {
56                 self.borrowck_context
57                     .constraints
58                     .universe_causes
59                     .insert(u + 1, universe_info.clone());
60             }
61         }
62
63         Ok(output)
64     }
65
66     pub(super) fn instantiate_canonical_with_fresh_inference_vars<T>(
67         &mut self,
68         span: Span,
69         canonical: &Canonical<'tcx, T>,
70     ) -> T
71     where
72         T: TypeFoldable<'tcx>,
73     {
74         let (instantiated, _) =
75             self.infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
76
77         for u in 0..canonical.max_universe.as_u32() {
78             let info = UniverseInfo::other();
79             self.borrowck_context
80                 .constraints
81                 .universe_causes
82                 .insert(ty::UniverseIndex::from_u32(u), info);
83         }
84
85         instantiated
86     }
87
88     #[instrument(skip(self), level = "debug")]
89     pub(super) fn prove_trait_ref(
90         &mut self,
91         trait_ref: ty::TraitRef<'tcx>,
92         locations: Locations,
93         category: ConstraintCategory<'tcx>,
94     ) {
95         self.prove_predicate(
96             ty::Binder::dummy(ty::PredicateKind::Trait(ty::TraitPredicate {
97                 trait_ref,
98                 constness: ty::BoundConstness::NotConst,
99                 polarity: ty::ImplPolarity::Positive,
100             }))
101             .to_predicate(self.tcx()),
102             locations,
103             category,
104         );
105     }
106
107     pub(super) fn normalize_and_prove_instantiated_predicates(
108         &mut self,
109         // Keep this parameter for now, in case we start using
110         // it in `ConstraintCategory` at some point.
111         _def_id: DefId,
112         instantiated_predicates: ty::InstantiatedPredicates<'tcx>,
113         locations: Locations,
114     ) {
115         for (predicate, span) in instantiated_predicates
116             .predicates
117             .into_iter()
118             .zip(instantiated_predicates.spans.into_iter())
119         {
120             debug!(?predicate);
121             let predicate = self.normalize(predicate, locations);
122             self.prove_predicate(predicate, locations, ConstraintCategory::Predicate(span));
123         }
124     }
125
126     pub(super) fn prove_predicates(
127         &mut self,
128         predicates: impl IntoIterator<Item = impl ToPredicate<'tcx>>,
129         locations: Locations,
130         category: ConstraintCategory<'tcx>,
131     ) {
132         for predicate in predicates {
133             let predicate = predicate.to_predicate(self.tcx());
134             debug!("prove_predicates(predicate={:?}, locations={:?})", predicate, locations,);
135
136             self.prove_predicate(predicate, locations, category);
137         }
138     }
139
140     #[instrument(skip(self), level = "debug")]
141     pub(super) fn prove_predicate(
142         &mut self,
143         predicate: ty::Predicate<'tcx>,
144         locations: Locations,
145         category: ConstraintCategory<'tcx>,
146     ) {
147         let param_env = self.param_env;
148         self.fully_perform_op(
149             locations,
150             category,
151             param_env.and(type_op::prove_predicate::ProvePredicate::new(predicate)),
152         )
153         .unwrap_or_else(|NoSolution| {
154             span_mirbug!(self, NoSolution, "could not prove {:?}", predicate);
155         })
156     }
157
158     #[instrument(skip(self), level = "debug")]
159     pub(super) fn normalize<T>(&mut self, value: T, location: impl NormalizeLocation) -> T
160     where
161         T: type_op::normalize::Normalizable<'tcx> + fmt::Display + Copy + 'tcx,
162     {
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)),
168         )
169         .unwrap_or_else(|NoSolution| {
170             span_mirbug!(self, NoSolution, "failed to normalize `{:?}`", value);
171             value
172         })
173     }
174 }