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