]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/subst.rs
auto merge of #13049 : alexcrichton/rust/io-fill, r=huonw
[rust.git] / src / librustc / middle / subst.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 // Type substitutions.
12
13 use middle::ty;
14 use middle::ty_fold;
15 use middle::ty_fold::TypeFolder;
16 use util::ppaux::Repr;
17
18 use std::rc::Rc;
19 use syntax::codemap::Span;
20 use syntax::owned_slice::OwnedSlice;
21
22 ///////////////////////////////////////////////////////////////////////////
23 // Public trait `Subst`
24 //
25 // Just call `foo.subst(tcx, substs)` to perform a substitution across
26 // `foo`.
27 // Or use `foo.subst_spanned(tcx, substs, Some(span))` when there is more
28 // information available (for better errors).
29
30 pub trait Subst {
31     fn subst(&self, tcx: &ty::ctxt, substs: &ty::substs) -> Self {
32         self.subst_spanned(tcx, substs, None)
33     }
34     fn subst_spanned(&self, tcx: &ty::ctxt,
35                      substs: &ty::substs,
36                      span: Option<Span>) -> Self;
37 }
38
39 ///////////////////////////////////////////////////////////////////////////
40 // Substitution over types
41 //
42 // Because this is so common, we make a special optimization to avoid
43 // doing anything if `substs` is a no-op.  I tried to generalize these
44 // to all subst methods but ran into trouble due to the limitations of
45 // our current method/trait matching algorithm. - Niko
46
47 impl Subst for ty::t {
48     fn subst_spanned(&self, tcx: &ty::ctxt,
49                      substs: &ty::substs,
50                      span: Option<Span>) -> ty::t {
51         if ty::substs_is_noop(substs) && !ty::type_has_params(*self) {
52             *self
53         } else {
54             let mut folder = SubstFolder {
55                 tcx: tcx,
56                 substs: substs,
57                 span: span,
58                 root_ty: Some(*self)
59             };
60             folder.fold_ty(*self)
61         }
62     }
63 }
64
65 struct SubstFolder<'a> {
66     tcx: &'a ty::ctxt,
67     substs: &'a ty::substs,
68
69     // The location for which the substitution is performed, if available.
70     span: Option<Span>,
71
72     // The root type that is being substituted, if available.
73     root_ty: Option<ty::t>
74 }
75
76 impl<'a> TypeFolder for SubstFolder<'a> {
77     fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
78
79     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
80         r.subst(self.tcx, self.substs)
81     }
82
83     fn fold_ty(&mut self, t: ty::t) -> ty::t {
84         if !ty::type_needs_subst(t) {
85             return t;
86         }
87
88         match ty::get(t).sty {
89             ty::ty_param(p) => {
90                 if p.idx < self.substs.tps.len() {
91                     *self.substs.tps.get(p.idx)
92                 } else {
93                     let root_msg = match self.root_ty {
94                         Some(root) => format!(" in the substitution of `{}`",
95                                               root.repr(self.tcx)),
96                         None => ~""
97                     };
98                     let m = format!("missing type param `{}`{}",
99                                     t.repr(self.tcx), root_msg);
100                     match self.span {
101                         Some(span) => self.tcx.sess.span_err(span, m),
102                         None => self.tcx.sess.err(m)
103                     }
104                     ty::mk_err()
105                 }
106             }
107             ty::ty_self(_) => {
108                 match self.substs.self_ty {
109                     Some(ty) => ty,
110                     None => {
111                         let root_msg = match self.root_ty {
112                             Some(root) => format!(" in the substitution of `{}`",
113                                                   root.repr(self.tcx)),
114                             None => ~""
115                         };
116                         let m = format!("missing `Self` type param{}", root_msg);
117                         match self.span {
118                             Some(span) => self.tcx.sess.span_err(span, m),
119                             None => self.tcx.sess.err(m)
120                         }
121                         ty::mk_err()
122                     }
123                 }
124             }
125             _ => ty_fold::super_fold_ty(self, t)
126         }
127     }
128 }
129
130 ///////////////////////////////////////////////////////////////////////////
131 // Other types
132
133 impl<T:Subst> Subst for Vec<T> {
134     fn subst_spanned(&self, tcx: &ty::ctxt,
135                      substs: &ty::substs,
136                      span: Option<Span>) -> Vec<T> {
137         self.map(|t| t.subst_spanned(tcx, substs, span))
138     }
139 }
140 impl<T:Subst> Subst for Rc<T> {
141     fn subst_spanned(&self, tcx: &ty::ctxt,
142                      substs: &ty::substs,
143                      span: Option<Span>) -> Rc<T> {
144         Rc::new((**self).subst_spanned(tcx, substs, span))
145     }
146 }
147
148 impl<T:Subst> Subst for OwnedSlice<T> {
149     fn subst_spanned(&self, tcx: &ty::ctxt,
150                      substs: &ty::substs,
151                      span: Option<Span>) -> OwnedSlice<T> {
152         self.map(|t| t.subst_spanned(tcx, substs, span))
153     }
154 }
155
156 impl<T:Subst + 'static> Subst for @T {
157     fn subst_spanned(&self, tcx: &ty::ctxt,
158                      substs: &ty::substs,
159                      span: Option<Span>) -> @T {
160         match self {
161             t => @(**t).subst_spanned(tcx, substs, span)
162         }
163     }
164 }
165
166 impl<T:Subst> Subst for Option<T> {
167     fn subst_spanned(&self, tcx: &ty::ctxt,
168                      substs: &ty::substs,
169                      span: Option<Span>) -> Option<T> {
170         self.as_ref().map(|t| t.subst_spanned(tcx, substs, span))
171     }
172 }
173
174 impl Subst for ty::TraitRef {
175     fn subst_spanned(&self, tcx: &ty::ctxt,
176                      substs: &ty::substs,
177                      span: Option<Span>) -> ty::TraitRef {
178         ty::TraitRef {
179             def_id: self.def_id,
180             substs: self.substs.subst_spanned(tcx, substs, span)
181         }
182     }
183 }
184
185 impl Subst for ty::substs {
186     fn subst_spanned(&self, tcx: &ty::ctxt,
187                      substs: &ty::substs,
188                      span: Option<Span>) -> ty::substs {
189         ty::substs {
190             regions: self.regions.subst_spanned(tcx, substs, span),
191             self_ty: self.self_ty.map(|typ| typ.subst_spanned(tcx, substs, span)),
192             tps: self.tps.map(|typ| typ.subst_spanned(tcx, substs, span))
193         }
194     }
195 }
196
197 impl Subst for ty::RegionSubsts {
198     fn subst_spanned(&self, tcx: &ty::ctxt,
199                      substs: &ty::substs,
200                      span: Option<Span>) -> ty::RegionSubsts {
201         match *self {
202             ty::ErasedRegions => {
203                 ty::ErasedRegions
204             }
205             ty::NonerasedRegions(ref regions) => {
206                 ty::NonerasedRegions(regions.subst_spanned(tcx, substs, span))
207             }
208         }
209     }
210 }
211
212 impl Subst for ty::BareFnTy {
213     fn subst_spanned(&self, tcx: &ty::ctxt,
214                      substs: &ty::substs,
215                      span: Option<Span>) -> ty::BareFnTy {
216         let mut folder = SubstFolder {
217             tcx: tcx,
218             substs: substs,
219             span: span,
220             root_ty: None
221         };
222         folder.fold_bare_fn_ty(self)
223     }
224 }
225
226 impl Subst for ty::ParamBounds {
227     fn subst_spanned(&self, tcx: &ty::ctxt,
228                      substs: &ty::substs,
229                      span: Option<Span>) -> ty::ParamBounds {
230         ty::ParamBounds {
231             builtin_bounds: self.builtin_bounds,
232             trait_bounds: self.trait_bounds.subst_spanned(tcx, substs, span)
233         }
234     }
235 }
236
237 impl Subst for ty::TypeParameterDef {
238     fn subst_spanned(&self, tcx: &ty::ctxt,
239                      substs: &ty::substs,
240                      span: Option<Span>) -> ty::TypeParameterDef {
241         ty::TypeParameterDef {
242             ident: self.ident,
243             def_id: self.def_id,
244             bounds: self.bounds.subst_spanned(tcx, substs, span),
245             default: self.default.map(|x| x.subst_spanned(tcx, substs, span))
246         }
247     }
248 }
249
250 impl Subst for ty::Generics {
251     fn subst_spanned(&self, tcx: &ty::ctxt,
252                      substs: &ty::substs,
253                      span: Option<Span>) -> ty::Generics {
254         ty::Generics {
255             type_param_defs: self.type_param_defs.subst_spanned(tcx, substs, span),
256             region_param_defs: self.region_param_defs.subst_spanned(tcx, substs, span),
257         }
258     }
259 }
260
261 impl Subst for ty::RegionParameterDef {
262     fn subst_spanned(&self, _: &ty::ctxt,
263                      _: &ty::substs,
264                      _: Option<Span>) -> ty::RegionParameterDef {
265         *self
266     }
267 }
268
269 impl Subst for ty::Region {
270     fn subst_spanned(&self, _tcx: &ty::ctxt,
271                      substs: &ty::substs,
272                      _: Option<Span>) -> ty::Region {
273         // Note: This routine only handles regions that are bound on
274         // type declarationss and other outer declarations, not those
275         // bound in *fn types*. Region substitution of the bound
276         // regions that appear in a function signature is done using
277         // the specialized routine
278         // `middle::typeck::check::regionmanip::replace_late_regions_in_fn_sig()`.
279         match self {
280             &ty::ReEarlyBound(_, i, _) => {
281                 match substs.regions {
282                     ty::ErasedRegions => ty::ReStatic,
283                     ty::NonerasedRegions(ref regions) => *regions.get(i),
284                 }
285             }
286             _ => *self
287         }
288     }
289 }
290
291 impl Subst for ty::ty_param_bounds_and_ty {
292     fn subst_spanned(&self, tcx: &ty::ctxt,
293                      substs: &ty::substs,
294                      span: Option<Span>) -> ty::ty_param_bounds_and_ty {
295         ty::ty_param_bounds_and_ty {
296             generics: self.generics.subst_spanned(tcx, substs, span),
297             ty: self.ty.subst_spanned(tcx, substs, span)
298         }
299     }
300 }