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.
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.
11 // Type substitutions.
15 use middle::ty_fold::TypeFolder;
16 use util::ppaux::Repr;
19 use syntax::codemap::Span;
20 use syntax::owned_slice::OwnedSlice;
22 ///////////////////////////////////////////////////////////////////////////
23 // Public trait `Subst`
25 // Just call `foo.subst(tcx, substs)` to perform a substitution across
27 // Or use `foo.subst_spanned(tcx, substs, Some(span))` when there is more
28 // information available (for better errors).
31 fn subst(&self, tcx: &ty::ctxt, substs: &ty::substs) -> Self {
32 self.subst_spanned(tcx, substs, None)
34 fn subst_spanned(&self, tcx: &ty::ctxt,
36 span: Option<Span>) -> Self;
39 ///////////////////////////////////////////////////////////////////////////
40 // Substitution over types
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
47 impl Subst for ty::t {
48 fn subst_spanned(&self, tcx: &ty::ctxt,
50 span: Option<Span>) -> ty::t {
51 if ty::substs_is_noop(substs) && !ty::type_has_params(*self) {
54 let mut folder = SubstFolder {
65 struct SubstFolder<'a> {
67 substs: &'a ty::substs,
69 // The location for which the substitution is performed, if available.
72 // The root type that is being substituted, if available.
73 root_ty: Option<ty::t>
76 impl<'a> TypeFolder for SubstFolder<'a> {
77 fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
79 fn fold_region(&mut self, r: ty::Region) -> ty::Region {
80 r.subst(self.tcx, self.substs)
83 fn fold_ty(&mut self, t: ty::t) -> ty::t {
84 if !ty::type_needs_subst(t) {
88 match ty::get(t).sty {
90 if p.idx < self.substs.tps.len() {
91 *self.substs.tps.get(p.idx)
93 let root_msg = match self.root_ty {
94 Some(root) => format!(" in the substitution of `{}`",
98 let m = format!("missing type param `{}`{}",
99 t.repr(self.tcx), root_msg);
101 Some(span) => self.tcx.sess.span_err(span, m),
102 None => self.tcx.sess.err(m)
108 match self.substs.self_ty {
111 let root_msg = match self.root_ty {
112 Some(root) => format!(" in the substitution of `{}`",
113 root.repr(self.tcx)),
116 let m = format!("missing `Self` type param{}", root_msg);
118 Some(span) => self.tcx.sess.span_err(span, m),
119 None => self.tcx.sess.err(m)
125 _ => ty_fold::super_fold_ty(self, t)
130 ///////////////////////////////////////////////////////////////////////////
133 impl<T:Subst> Subst for Vec<T> {
134 fn subst_spanned(&self, tcx: &ty::ctxt,
136 span: Option<Span>) -> Vec<T> {
137 self.map(|t| t.subst_spanned(tcx, substs, span))
140 impl<T:Subst> Subst for Rc<T> {
141 fn subst_spanned(&self, tcx: &ty::ctxt,
143 span: Option<Span>) -> Rc<T> {
144 Rc::new((**self).subst_spanned(tcx, substs, span))
148 impl<T:Subst> Subst for OwnedSlice<T> {
149 fn subst_spanned(&self, tcx: &ty::ctxt,
151 span: Option<Span>) -> OwnedSlice<T> {
152 self.map(|t| t.subst_spanned(tcx, substs, span))
156 impl<T:Subst + 'static> Subst for @T {
157 fn subst_spanned(&self, tcx: &ty::ctxt,
159 span: Option<Span>) -> @T {
161 t => @(**t).subst_spanned(tcx, substs, span)
166 impl<T:Subst> Subst for Option<T> {
167 fn subst_spanned(&self, tcx: &ty::ctxt,
169 span: Option<Span>) -> Option<T> {
170 self.as_ref().map(|t| t.subst_spanned(tcx, substs, span))
174 impl Subst for ty::TraitRef {
175 fn subst_spanned(&self, tcx: &ty::ctxt,
177 span: Option<Span>) -> ty::TraitRef {
180 substs: self.substs.subst_spanned(tcx, substs, span)
185 impl Subst for ty::substs {
186 fn subst_spanned(&self, tcx: &ty::ctxt,
188 span: Option<Span>) -> 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))
197 impl Subst for ty::RegionSubsts {
198 fn subst_spanned(&self, tcx: &ty::ctxt,
200 span: Option<Span>) -> ty::RegionSubsts {
202 ty::ErasedRegions => {
205 ty::NonerasedRegions(ref regions) => {
206 ty::NonerasedRegions(regions.subst_spanned(tcx, substs, span))
212 impl Subst for ty::BareFnTy {
213 fn subst_spanned(&self, tcx: &ty::ctxt,
215 span: Option<Span>) -> ty::BareFnTy {
216 let mut folder = SubstFolder {
222 folder.fold_bare_fn_ty(self)
226 impl Subst for ty::ParamBounds {
227 fn subst_spanned(&self, tcx: &ty::ctxt,
229 span: Option<Span>) -> ty::ParamBounds {
231 builtin_bounds: self.builtin_bounds,
232 trait_bounds: self.trait_bounds.subst_spanned(tcx, substs, span)
237 impl Subst for ty::TypeParameterDef {
238 fn subst_spanned(&self, tcx: &ty::ctxt,
240 span: Option<Span>) -> ty::TypeParameterDef {
241 ty::TypeParameterDef {
244 bounds: self.bounds.subst_spanned(tcx, substs, span),
245 default: self.default.map(|x| x.subst_spanned(tcx, substs, span))
250 impl Subst for ty::Generics {
251 fn subst_spanned(&self, tcx: &ty::ctxt,
253 span: Option<Span>) -> 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),
261 impl Subst for ty::RegionParameterDef {
262 fn subst_spanned(&self, _: &ty::ctxt,
264 _: Option<Span>) -> ty::RegionParameterDef {
269 impl Subst for ty::Region {
270 fn subst_spanned(&self, _tcx: &ty::ctxt,
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()`.
280 &ty::ReEarlyBound(_, i, _) => {
281 match substs.regions {
282 ty::ErasedRegions => ty::ReStatic,
283 ty::NonerasedRegions(ref regions) => *regions.get(i),
291 impl Subst for ty::ty_param_bounds_and_ty {
292 fn subst_spanned(&self, tcx: &ty::ctxt,
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)