Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Type substitutions.
|
|
|
|
|
|
|
|
use middle::ty;
|
2013-10-29 04:25:18 -05:00
|
|
|
use middle::ty_fold;
|
2014-05-12 16:12:51 -05:00
|
|
|
use middle::ty_fold::{TypeFoldable, TypeFolder};
|
2014-02-02 04:53:23 -06:00
|
|
|
use util::ppaux::Repr;
|
2014-01-31 22:57:59 -06:00
|
|
|
|
2014-02-02 04:53:23 -06:00
|
|
|
use syntax::codemap::Span;
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Public trait `Subst`
|
|
|
|
//
|
|
|
|
// Just call `foo.subst(tcx, substs)` to perform a substitution across
|
2014-05-12 16:12:51 -05:00
|
|
|
// `foo`. Or use `foo.subst_spanned(tcx, substs, Some(span))` when
|
|
|
|
// there is more information available (for better errors).
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
|
|
|
|
pub trait Subst {
|
2014-03-05 21:07:47 -06:00
|
|
|
fn subst(&self, tcx: &ty::ctxt, substs: &ty::substs) -> Self {
|
2014-02-02 04:53:23 -06:00
|
|
|
self.subst_spanned(tcx, substs, None)
|
|
|
|
}
|
2014-05-12 16:12:51 -05:00
|
|
|
|
2014-03-05 21:07:47 -06:00
|
|
|
fn subst_spanned(&self, tcx: &ty::ctxt,
|
2014-02-02 04:53:23 -06:00
|
|
|
substs: &ty::substs,
|
2014-05-12 16:12:51 -05:00
|
|
|
span: Option<Span>)
|
|
|
|
-> Self;
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
}
|
|
|
|
|
2014-05-12 16:12:51 -05:00
|
|
|
impl<T:TypeFoldable> Subst for T {
|
|
|
|
fn subst_spanned(&self,
|
|
|
|
tcx: &ty::ctxt,
|
2014-02-02 04:53:23 -06:00
|
|
|
substs: &ty::substs,
|
2014-05-12 16:12:51 -05:00
|
|
|
span: Option<Span>)
|
|
|
|
-> T
|
|
|
|
{
|
|
|
|
let mut folder = SubstFolder { tcx: tcx,
|
|
|
|
substs: substs,
|
|
|
|
span: span,
|
|
|
|
root_ty: None,
|
|
|
|
ty_stack_depth: 0 };
|
|
|
|
(*self).fold_with(&mut folder)
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-12 16:12:51 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// The actual substitution engine itself is a type folder.
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
struct SubstFolder<'a> {
|
2014-03-05 21:07:47 -06:00
|
|
|
tcx: &'a ty::ctxt,
|
2014-02-02 04:53:23 -06:00
|
|
|
substs: &'a ty::substs,
|
|
|
|
|
|
|
|
// The location for which the substitution is performed, if available.
|
|
|
|
span: Option<Span>,
|
|
|
|
|
|
|
|
// The root type that is being substituted, if available.
|
2014-05-12 16:12:51 -05:00
|
|
|
root_ty: Option<ty::t>,
|
|
|
|
|
|
|
|
// Depth of type stack
|
|
|
|
ty_stack_depth: uint,
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> TypeFolder for SubstFolder<'a> {
|
2014-03-05 21:07:47 -06:00
|
|
|
fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
|
2013-10-29 04:25:18 -05:00
|
|
|
|
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
2014-05-12 16:12:51 -05:00
|
|
|
// Note: This routine only handles regions that are bound on
|
|
|
|
// type declarations and other outer declarations, not those
|
|
|
|
// bound in *fn types*. Region substitution of the bound
|
|
|
|
// regions that appear in a function signature is done using
|
|
|
|
// the specialized routine
|
|
|
|
// `middle::typeck::check::regionmanip::replace_late_regions_in_fn_sig()`.
|
|
|
|
match r {
|
|
|
|
ty::ReEarlyBound(_, i, _) => {
|
|
|
|
match self.substs.regions {
|
|
|
|
ty::ErasedRegions => ty::ReStatic,
|
|
|
|
ty::NonerasedRegions(ref regions) => *regions.get(i),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => r
|
|
|
|
}
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, t: ty::t) -> ty::t {
|
|
|
|
if !ty::type_needs_subst(t) {
|
|
|
|
return t;
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
}
|
|
|
|
|
2014-05-12 16:12:51 -05:00
|
|
|
// track the root type we were asked to substitute
|
|
|
|
let depth = self.ty_stack_depth;
|
|
|
|
if depth == 0 {
|
|
|
|
self.root_ty = Some(t);
|
|
|
|
}
|
|
|
|
self.ty_stack_depth += 1;
|
|
|
|
|
|
|
|
let t1 = match ty::get(t).sty {
|
2013-04-09 14:33:18 -05:00
|
|
|
ty::ty_param(p) => {
|
2014-05-12 16:12:51 -05:00
|
|
|
// FIXME -- This...really shouldn't happen. We should
|
|
|
|
// never be substituting without knowing what's in
|
|
|
|
// scope and knowing that the indices will line up!
|
2014-02-02 04:53:23 -06:00
|
|
|
if p.idx < self.substs.tps.len() {
|
2014-03-08 14:36:22 -06:00
|
|
|
*self.substs.tps.get(p.idx)
|
2014-02-02 04:53:23 -06:00
|
|
|
} else {
|
|
|
|
let root_msg = match self.root_ty {
|
|
|
|
Some(root) => format!(" in the substitution of `{}`",
|
|
|
|
root.repr(self.tcx)),
|
2014-05-25 05:17:19 -05:00
|
|
|
None => "".to_string()
|
2014-02-02 04:53:23 -06:00
|
|
|
};
|
2014-04-16 17:32:00 -05:00
|
|
|
let m = format!("can't use type parameters from outer \
|
|
|
|
function{}; try using a local type \
|
2014-05-16 12:45:16 -05:00
|
|
|
parameter instead",
|
|
|
|
root_msg);
|
2014-02-02 04:53:23 -06:00
|
|
|
match self.span {
|
2014-05-16 12:45:16 -05:00
|
|
|
Some(span) => {
|
|
|
|
self.tcx.sess.span_err(span, m.as_slice())
|
|
|
|
}
|
|
|
|
None => self.tcx.sess.err(m.as_slice())
|
2014-02-02 04:53:23 -06:00
|
|
|
}
|
|
|
|
ty::mk_err()
|
|
|
|
}
|
2013-04-09 14:33:18 -05:00
|
|
|
}
|
|
|
|
ty::ty_self(_) => {
|
2014-02-02 04:53:23 -06:00
|
|
|
match self.substs.self_ty {
|
|
|
|
Some(ty) => ty,
|
|
|
|
None => {
|
|
|
|
let root_msg = match self.root_ty {
|
|
|
|
Some(root) => format!(" in the substitution of `{}`",
|
|
|
|
root.repr(self.tcx)),
|
2014-05-25 05:17:19 -05:00
|
|
|
None => "".to_string()
|
2014-02-02 04:53:23 -06:00
|
|
|
};
|
2014-05-16 12:45:16 -05:00
|
|
|
let m = format!("missing `Self` type param{}",
|
|
|
|
root_msg);
|
2014-02-02 04:53:23 -06:00
|
|
|
match self.span {
|
2014-05-16 12:45:16 -05:00
|
|
|
Some(span) => {
|
|
|
|
self.tcx.sess.span_err(span, m.as_slice())
|
|
|
|
}
|
|
|
|
None => self.tcx.sess.err(m.as_slice())
|
2014-02-02 04:53:23 -06:00
|
|
|
}
|
|
|
|
ty::mk_err()
|
|
|
|
}
|
|
|
|
}
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
}
|
2014-02-02 04:53:23 -06:00
|
|
|
_ => ty_fold::super_fold_ty(self, t)
|
|
|
|
};
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
|
2014-05-12 16:12:51 -05:00
|
|
|
assert_eq!(depth + 1, self.ty_stack_depth);
|
|
|
|
self.ty_stack_depth -= 1;
|
|
|
|
if depth == 0 {
|
|
|
|
self.root_ty = None;
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
}
|
|
|
|
|
2014-05-12 16:12:51 -05:00
|
|
|
t1
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
}
|
|
|
|
}
|