]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/resolve.rs
Update const_forget.rs
[rust.git] / src / librustc_infer / infer / resolve.rs
1 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2 use super::{FixupError, FixupResult, InferCtxt, Span};
3 use rustc::ty::fold::{TypeFolder, TypeVisitor};
4 use rustc::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) => self
79                 .infcx
80                 .inner
81                 .borrow_mut()
82                 .unwrap_region_constraints()
83                 .opportunistic_resolve_var(self.tcx(), rid),
84             _ => r,
85         }
86     }
87
88     fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
89         if !ct.needs_infer() {
90             ct // micro-optimize -- if there is nothing in this const that this fold affects...
91         } else {
92             let c0 = self.infcx.shallow_resolve(ct);
93             c0.super_fold_with(self)
94         }
95     }
96 }
97
98 ///////////////////////////////////////////////////////////////////////////
99 // UNRESOLVED TYPE FINDER
100
101 /// The unresolved type **finder** walks a type searching for
102 /// type variables that don't yet have a value. The first unresolved type is stored.
103 /// It does not construct the fully resolved type (which might
104 /// involve some hashing and so forth).
105 pub struct UnresolvedTypeFinder<'a, 'tcx> {
106     infcx: &'a InferCtxt<'a, 'tcx>,
107
108     /// Used to find the type parameter name and location for error reporting.
109     pub first_unresolved: Option<(Ty<'tcx>, Option<Span>)>,
110 }
111
112 impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
113     pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
114         UnresolvedTypeFinder { infcx, first_unresolved: None }
115     }
116 }
117
118 impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
119     fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
120         let t = self.infcx.shallow_resolve(t);
121         if t.has_infer_types() {
122             if let ty::Infer(infer_ty) = t.kind {
123                 // Since we called `shallow_resolve` above, this must
124                 // be an (as yet...) unresolved inference variable.
125                 let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
126                     let ty_vars = &self.infcx.inner.borrow().type_variables;
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 // FULL TYPE RESOLUTION
155
156 /// Full type resolution replaces all type and region variables with
157 /// their concrete results. If any variable cannot be replaced (never unified, etc)
158 /// then an `Err` result is returned.
159 pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: &T) -> FixupResult<'tcx, T>
160 where
161     T: TypeFoldable<'tcx>,
162 {
163     let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
164     let result = value.fold_with(&mut full_resolver);
165     match full_resolver.err {
166         None => Ok(result),
167         Some(e) => Err(e),
168     }
169 }
170
171 // N.B. This type is not public because the protocol around checking the
172 // `err` field is not enforcable otherwise.
173 struct FullTypeResolver<'a, 'tcx> {
174     infcx: &'a InferCtxt<'a, 'tcx>,
175     err: Option<FixupError<'tcx>>,
176 }
177
178 impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
179     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
180         self.infcx.tcx
181     }
182
183     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
184         if !t.needs_infer() && !ty::keep_local(&t) {
185             t // micro-optimize -- if there is nothing in this type that this fold affects...
186         // ^ we need to have the `keep_local` check to un-default
187         // defaulted tuples.
188         } else {
189             let t = self.infcx.shallow_resolve(t);
190             match t.kind {
191                 ty::Infer(ty::TyVar(vid)) => {
192                     self.err = Some(FixupError::UnresolvedTy(vid));
193                     self.tcx().types.err
194                 }
195                 ty::Infer(ty::IntVar(vid)) => {
196                     self.err = Some(FixupError::UnresolvedIntTy(vid));
197                     self.tcx().types.err
198                 }
199                 ty::Infer(ty::FloatVar(vid)) => {
200                     self.err = Some(FixupError::UnresolvedFloatTy(vid));
201                     self.tcx().types.err
202                 }
203                 ty::Infer(_) => {
204                     bug!("Unexpected type in full type resolver: {:?}", t);
205                 }
206                 _ => t.super_fold_with(self),
207             }
208         }
209     }
210
211     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
212         match *r {
213             ty::ReVar(rid) => self
214                 .infcx
215                 .lexical_region_resolutions
216                 .borrow()
217                 .as_ref()
218                 .expect("region resolution not performed")
219                 .resolve_var(rid),
220             _ => r,
221         }
222     }
223
224     fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
225         if !c.needs_infer() && !ty::keep_local(&c) {
226             c // micro-optimize -- if there is nothing in this const that this fold affects...
227         // ^ we need to have the `keep_local` check to un-default
228         // defaulted tuples.
229         } else {
230             let c = self.infcx.shallow_resolve(c);
231             match c.val {
232                 ty::ConstKind::Infer(InferConst::Var(vid)) => {
233                     self.err = Some(FixupError::UnresolvedConst(vid));
234                     return self.tcx().consts.err;
235                 }
236                 ty::ConstKind::Infer(InferConst::Fresh(_)) => {
237                     bug!("Unexpected const in full const resolver: {:?}", c);
238                 }
239                 _ => {}
240             }
241             c.super_fold_with(self)
242         }
243     }
244 }