]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/resolve.rs
Auto merge of #53815 - F001:if-let-guard, r=petrochenkov
[rust.git] / src / librustc / infer / resolve.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use super::{InferCtxt, FixupError, FixupResult};
12 use ty::{self, Ty, TyCtxt, TypeFoldable};
13 use ty::fold::{TypeFolder, TypeVisitor};
14
15 ///////////////////////////////////////////////////////////////////////////
16 // OPPORTUNISTIC TYPE RESOLVER
17
18 /// The opportunistic type resolver can be used at any time. It simply replaces
19 /// type variables that have been unified with the things they have
20 /// been unified with (similar to `shallow_resolve`, but deep). This is
21 /// useful for printing messages etc but also required at various
22 /// points for correctness.
23 pub struct OpportunisticTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
24     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
25 }
26
27 impl<'a, 'gcx, 'tcx> OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
28     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
29         OpportunisticTypeResolver { infcx: infcx }
30     }
31 }
32
33 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
34     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
35         self.infcx.tcx
36     }
37
38     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
39         if !t.has_infer_types() {
40             t // micro-optimize -- if there is nothing in this type that this fold affects...
41         } else {
42             let t0 = self.infcx.shallow_resolve(t);
43             t0.super_fold_with(self)
44         }
45     }
46 }
47
48 /// The opportunistic type and region resolver is similar to the
49 /// opportunistic type resolver, but also opportunistically resolves
50 /// regions. It is useful for canonicalization.
51 pub struct OpportunisticTypeAndRegionResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
52     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
53 }
54
55 impl<'a, 'gcx, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
56     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
57         OpportunisticTypeAndRegionResolver { infcx: infcx }
58     }
59 }
60
61 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
62     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
63         self.infcx.tcx
64     }
65
66     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
67         if !t.needs_infer() {
68             t // micro-optimize -- if there is nothing in this type that this fold affects...
69         } else {
70             let t0 = self.infcx.shallow_resolve(t);
71             t0.super_fold_with(self)
72         }
73     }
74
75     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
76         match *r {
77             ty::ReVar(rid) =>
78                 self.infcx.borrow_region_constraints()
79                           .opportunistic_resolve_var(self.tcx(), rid),
80             _ =>
81                 r,
82         }
83     }
84 }
85
86 ///////////////////////////////////////////////////////////////////////////
87 // UNRESOLVED TYPE FINDER
88
89 /// The unresolved type **finder** walks your type and searches for
90 /// type variables that don't yet have a value. They get pushed into a
91 /// vector. It does not construct the fully resolved type (which might
92 /// involve some hashing and so forth).
93 pub struct UnresolvedTypeFinder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
94     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
95 }
96
97 impl<'a, 'gcx, 'tcx> UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
98     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
99         UnresolvedTypeFinder { infcx }
100     }
101 }
102
103 impl<'a, 'gcx, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
104     fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
105         let t = self.infcx.shallow_resolve(t);
106         if t.has_infer_types() {
107             if let ty::Infer(_) = t.sty {
108                 // Since we called `shallow_resolve` above, this must
109                 // be an (as yet...) unresolved inference variable.
110                 true
111             } else {
112                 // Otherwise, visit its contents.
113                 t.super_visit_with(self)
114             }
115         } else {
116             // Micro-optimize: no inference types at all Can't have unresolved type
117             // variables, no need to visit the contents.
118             false
119         }
120     }
121 }
122
123 ///////////////////////////////////////////////////////////////////////////
124 // FULL TYPE RESOLUTION
125
126 /// Full type resolution replaces all type and region variables with
127 /// their concrete results. If any variable cannot be replaced (never unified, etc)
128 /// then an `Err` result is returned.
129 pub fn fully_resolve<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
130                                         value: &T) -> FixupResult<T>
131     where T : TypeFoldable<'tcx>
132 {
133     let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
134     let result = value.fold_with(&mut full_resolver);
135     match full_resolver.err {
136         None => Ok(result),
137         Some(e) => Err(e),
138     }
139 }
140
141 // N.B. This type is not public because the protocol around checking the
142 // `err` field is not enforcable otherwise.
143 struct FullTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
144     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
145     err: Option<FixupError>,
146 }
147
148 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx> {
149     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
150         self.infcx.tcx
151     }
152
153     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
154         if !t.needs_infer() && !ty::keep_local(&t) {
155             t // micro-optimize -- if there is nothing in this type that this fold affects...
156                 // ^ we need to have the `keep_local` check to un-default
157                 // defaulted tuples.
158         } else {
159             let t = self.infcx.shallow_resolve(t);
160             match t.sty {
161                 ty::Infer(ty::TyVar(vid)) => {
162                     self.err = Some(FixupError::UnresolvedTy(vid));
163                     self.tcx().types.err
164                 }
165                 ty::Infer(ty::IntVar(vid)) => {
166                     self.err = Some(FixupError::UnresolvedIntTy(vid));
167                     self.tcx().types.err
168                 }
169                 ty::Infer(ty::FloatVar(vid)) => {
170                     self.err = Some(FixupError::UnresolvedFloatTy(vid));
171                     self.tcx().types.err
172                 }
173                 ty::Infer(_) => {
174                     bug!("Unexpected type in full type resolver: {:?}", t);
175                 }
176                 _ => {
177                     t.super_fold_with(self)
178                 }
179             }
180         }
181     }
182
183     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
184         match *r {
185             ty::ReVar(rid) => self.infcx.lexical_region_resolutions
186                                         .borrow()
187                                         .as_ref()
188                                         .expect("region resolution not performed")
189                                         .resolve_var(rid),
190             _ => r,
191         }
192     }
193 }