]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/resolve.rs
Rollup merge of #68469 - ollie27:skip_count, r=sfackler
[rust.git] / src / librustc / infer / resolve.rs
1 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2 use super::{FixupError, FixupResult, InferCtxt, Span};
3 use crate::ty::fold::{TypeFolder, TypeVisitor};
4 use crate::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
5
6 ///////////////////////////////////////////////////////////////////////////
7 // OPPORTUNISTIC VAR RESOLVER
8
9 /// The opportunistic resolver can be used at any time. It simply replaces
10 /// type/const variables that have been unified with the things they have
11 /// been unified with (similar to `shallow_resolve`, but deep). This is
12 /// useful for printing messages etc but also required at various
13 /// points for correctness.
14 pub struct OpportunisticVarResolver<'a, 'tcx> {
15     infcx: &'a InferCtxt<'a, 'tcx>,
16 }
17
18 impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
19     #[inline]
20     pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
21         OpportunisticVarResolver { infcx }
22     }
23 }
24
25 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
26     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
27         self.infcx.tcx
28     }
29
30     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
31         if !t.has_infer_types() && !t.has_infer_consts() {
32             t // micro-optimize -- if there is nothing in this type that this fold affects...
33         } else {
34             let t = self.infcx.shallow_resolve(t);
35             t.super_fold_with(self)
36         }
37     }
38
39     fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> {
40         if !ct.has_infer_consts() {
41             ct // micro-optimize -- if there is nothing in this const that this fold affects...
42         } else {
43             let ct = self.infcx.shallow_resolve(ct);
44             ct.super_fold_with(self)
45         }
46     }
47 }
48
49 /// The opportunistic type and region resolver is similar to the
50 /// opportunistic type resolver, but also opportunistically resolves
51 /// regions. It is useful for canonicalization.
52 pub struct OpportunisticTypeAndRegionResolver<'a, 'tcx> {
53     infcx: &'a InferCtxt<'a, 'tcx>,
54 }
55
56 impl<'a, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'tcx> {
57     pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
58         OpportunisticTypeAndRegionResolver { infcx }
59     }
60 }
61
62 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticTypeAndRegionResolver<'a, 'tcx> {
63     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
64         self.infcx.tcx
65     }
66
67     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
68         if !t.needs_infer() {
69             t // micro-optimize -- if there is nothing in this type that this fold affects...
70         } else {
71             let t0 = self.infcx.shallow_resolve(t);
72             t0.super_fold_with(self)
73         }
74     }
75
76     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
77         match *r {
78             ty::ReVar(rid) => {
79                 self.infcx.borrow_region_constraints().opportunistic_resolve_var(self.tcx(), rid)
80             }
81             _ => r,
82         }
83     }
84
85     fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
86         if !ct.needs_infer() {
87             ct // micro-optimize -- if there is nothing in this const that this fold affects...
88         } else {
89             let c0 = self.infcx.shallow_resolve(ct);
90             c0.super_fold_with(self)
91         }
92     }
93 }
94
95 ///////////////////////////////////////////////////////////////////////////
96 // UNRESOLVED TYPE FINDER
97
98 /// The unresolved type **finder** walks a type searching for
99 /// type variables that don't yet have a value. The first unresolved type is stored.
100 /// It does not construct the fully resolved type (which might
101 /// involve some hashing and so forth).
102 pub struct UnresolvedTypeFinder<'a, 'tcx> {
103     infcx: &'a InferCtxt<'a, 'tcx>,
104
105     /// Used to find the type parameter name and location for error reporting.
106     pub first_unresolved: Option<(Ty<'tcx>, Option<Span>)>,
107 }
108
109 impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
110     pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
111         UnresolvedTypeFinder { infcx, first_unresolved: None }
112     }
113 }
114
115 impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
116     fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
117         let t = self.infcx.shallow_resolve(t);
118         if t.has_infer_types() {
119             if let ty::Infer(infer_ty) = t.kind {
120                 // Since we called `shallow_resolve` above, this must
121                 // be an (as yet...) unresolved inference variable.
122                 let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
123                     let ty_vars = self.infcx.type_variables.borrow();
124                     if let TypeVariableOrigin {
125                         kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
126                         span,
127                     } = *ty_vars.var_origin(ty_vid)
128                     {
129                         Some(span)
130                     } else {
131                         None
132                     }
133                 } else {
134                     None
135                 };
136                 self.first_unresolved = Some((t, ty_var_span));
137                 true // Halt visiting.
138             } else {
139                 // Otherwise, visit its contents.
140                 t.super_visit_with(self)
141             }
142         } else {
143             // All type variables in inference types must already be resolved,
144             // - no need to visit the contents, continue visiting.
145             false
146         }
147     }
148 }
149
150 ///////////////////////////////////////////////////////////////////////////
151 // FULL TYPE RESOLUTION
152
153 /// Full type resolution replaces all type and region variables with
154 /// their concrete results. If any variable cannot be replaced (never unified, etc)
155 /// then an `Err` result is returned.
156 pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: &T) -> FixupResult<'tcx, T>
157 where
158     T: TypeFoldable<'tcx>,
159 {
160     let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
161     let result = value.fold_with(&mut full_resolver);
162     match full_resolver.err {
163         None => Ok(result),
164         Some(e) => Err(e),
165     }
166 }
167
168 // N.B. This type is not public because the protocol around checking the
169 // `err` field is not enforcable otherwise.
170 struct FullTypeResolver<'a, 'tcx> {
171     infcx: &'a InferCtxt<'a, 'tcx>,
172     err: Option<FixupError<'tcx>>,
173 }
174
175 impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
176     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
177         self.infcx.tcx
178     }
179
180     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
181         if !t.needs_infer() && !ty::keep_local(&t) {
182             t // micro-optimize -- if there is nothing in this type that this fold affects...
183         // ^ we need to have the `keep_local` check to un-default
184         // defaulted tuples.
185         } else {
186             let t = self.infcx.shallow_resolve(t);
187             match t.kind {
188                 ty::Infer(ty::TyVar(vid)) => {
189                     self.err = Some(FixupError::UnresolvedTy(vid));
190                     self.tcx().types.err
191                 }
192                 ty::Infer(ty::IntVar(vid)) => {
193                     self.err = Some(FixupError::UnresolvedIntTy(vid));
194                     self.tcx().types.err
195                 }
196                 ty::Infer(ty::FloatVar(vid)) => {
197                     self.err = Some(FixupError::UnresolvedFloatTy(vid));
198                     self.tcx().types.err
199                 }
200                 ty::Infer(_) => {
201                     bug!("Unexpected type in full type resolver: {:?}", t);
202                 }
203                 _ => t.super_fold_with(self),
204             }
205         }
206     }
207
208     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
209         match *r {
210             ty::ReVar(rid) => self
211                 .infcx
212                 .lexical_region_resolutions
213                 .borrow()
214                 .as_ref()
215                 .expect("region resolution not performed")
216                 .resolve_var(rid),
217             _ => r,
218         }
219     }
220
221     fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
222         if !c.needs_infer() && !ty::keep_local(&c) {
223             c // micro-optimize -- if there is nothing in this const that this fold affects...
224         // ^ we need to have the `keep_local` check to un-default
225         // defaulted tuples.
226         } else {
227             let c = self.infcx.shallow_resolve(c);
228             match c.val {
229                 ty::ConstKind::Infer(InferConst::Var(vid)) => {
230                     self.err = Some(FixupError::UnresolvedConst(vid));
231                     return self.tcx().consts.err;
232                 }
233                 ty::ConstKind::Infer(InferConst::Fresh(_)) => {
234                     bug!("Unexpected const in full const resolver: {:?}", c);
235                 }
236                 _ => {}
237             }
238             c.super_fold_with(self)
239         }
240     }
241 }