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