]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/freshen.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / librustc / infer / freshen.rs
1 // Copyright 2014 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 //! Freshening is the process of replacing unknown variables with fresh types. The idea is that
12 //! the type, after freshening, contains no inference variables but instead contains either a
13 //! value for each variable or fresh "arbitrary" types wherever a variable would have been.
14 //!
15 //! Freshening is used primarily to get a good type for inserting into a cache. The result
16 //! summarizes what the type inferencer knows "so far". The primary place it is used right now is
17 //! in the trait matching algorithm, which needs to be able to cache whether an `impl` self type
18 //! matches some other type X -- *without* affecting `X`. That means if that if the type `X` is in
19 //! fact an unbound type variable, we want the match to be regarded as ambiguous, because depending
20 //! on what type that type variable is ultimately assigned, the match may or may not succeed.
21 //!
22 //! Note that you should be careful not to allow the output of freshening to leak to the user in
23 //! error messages or in any other form. Freshening is only really useful as an internal detail.
24 //!
25 //! __An important detail concerning regions.__ The freshener also replaces *all* regions with
26 //! 'erased. The reason behind this is that, in general, we do not take region relationships into
27 //! account when making type-overloaded decisions. This is important because of the design of the
28 //! region inferencer, which is not based on unification but rather on accumulating and then
29 //! solving a set of constraints. In contrast, the type inferencer assigns a value to each type
30 //! variable only once, and it does so as soon as it can, so it is reasonable to ask what the type
31 //! inferencer knows "so far".
32
33 use ty::{self, Ty, TyCtxt, TypeFoldable};
34 use ty::fold::TypeFolder;
35 use util::nodemap::FnvHashMap;
36 use std::collections::hash_map::Entry;
37
38 use super::InferCtxt;
39 use super::unify_key::ToType;
40
41 pub struct TypeFreshener<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
42     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
43     freshen_count: u32,
44     freshen_map: FnvHashMap<ty::InferTy, Ty<'tcx>>,
45 }
46
47 impl<'a, 'gcx, 'tcx> TypeFreshener<'a, 'gcx, 'tcx> {
48     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>)
49                -> TypeFreshener<'a, 'gcx, 'tcx> {
50         TypeFreshener {
51             infcx: infcx,
52             freshen_count: 0,
53             freshen_map: FnvHashMap(),
54         }
55     }
56
57     fn freshen<F>(&mut self,
58                   opt_ty: Option<Ty<'tcx>>,
59                   key: ty::InferTy,
60                   freshener: F)
61                   -> Ty<'tcx> where
62         F: FnOnce(u32) -> ty::InferTy,
63     {
64         match opt_ty {
65             Some(ty) => { return ty.fold_with(self); }
66             None => { }
67         }
68
69         match self.freshen_map.entry(key) {
70             Entry::Occupied(entry) => *entry.get(),
71             Entry::Vacant(entry) => {
72                 let index = self.freshen_count;
73                 self.freshen_count += 1;
74                 let t = self.infcx.tcx.mk_infer(freshener(index));
75                 entry.insert(t);
76                 t
77             }
78         }
79     }
80 }
81
82 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
83     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
84         self.infcx.tcx
85     }
86
87     fn fold_region(&mut self, r: &'tcx ty::Region) -> &'tcx ty::Region {
88         match *r {
89             ty::ReEarlyBound(..) |
90             ty::ReLateBound(..) => {
91                 // leave bound regions alone
92                 r
93             }
94
95             ty::ReStatic |
96             ty::ReFree(_) |
97             ty::ReScope(_) |
98             ty::ReVar(_) |
99             ty::ReSkolemized(..) |
100             ty::ReEmpty |
101             ty::ReErased => {
102                 // replace all free regions with 'erased
103                 self.tcx().mk_region(ty::ReErased)
104             }
105         }
106     }
107
108     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
109         if !t.needs_infer() && !t.has_erasable_regions() {
110             return t;
111         }
112
113         let tcx = self.infcx.tcx;
114
115         match t.sty {
116             ty::TyInfer(ty::TyVar(v)) => {
117                 let opt_ty = self.infcx.type_variables.borrow_mut().probe(v);
118                 self.freshen(
119                     opt_ty,
120                     ty::TyVar(v),
121                     ty::FreshTy)
122             }
123
124             ty::TyInfer(ty::IntVar(v)) => {
125                 self.freshen(
126                     self.infcx.int_unification_table.borrow_mut()
127                                                     .probe(v)
128                                                     .map(|v| v.to_type(tcx)),
129                     ty::IntVar(v),
130                     ty::FreshIntTy)
131             }
132
133             ty::TyInfer(ty::FloatVar(v)) => {
134                 self.freshen(
135                     self.infcx.float_unification_table.borrow_mut()
136                                                       .probe(v)
137                                                       .map(|v| v.to_type(tcx)),
138                     ty::FloatVar(v),
139                     ty::FreshFloatTy)
140             }
141
142             ty::TyInfer(ty::FreshTy(c)) |
143             ty::TyInfer(ty::FreshIntTy(c)) |
144             ty::TyInfer(ty::FreshFloatTy(c)) => {
145                 if c >= self.freshen_count {
146                     bug!("Encountered a freshend type with id {} \
147                           but our counter is only at {}",
148                          c,
149                          self.freshen_count);
150                 }
151                 t
152             }
153
154             ty::TyBool |
155             ty::TyChar |
156             ty::TyInt(..) |
157             ty::TyUint(..) |
158             ty::TyFloat(..) |
159             ty::TyEnum(..) |
160             ty::TyBox(..) |
161             ty::TyStr |
162             ty::TyError |
163             ty::TyArray(..) |
164             ty::TySlice(..) |
165             ty::TyRawPtr(..) |
166             ty::TyRef(..) |
167             ty::TyFnDef(..) |
168             ty::TyFnPtr(_) |
169             ty::TyTrait(..) |
170             ty::TyStruct(..) |
171             ty::TyClosure(..) |
172             ty::TyNever |
173             ty::TyTuple(..) |
174             ty::TyProjection(..) |
175             ty::TyParam(..) |
176             ty::TyAnon(..) => {
177                 t.super_fold_with(self)
178             }
179         }
180     }
181 }