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-05-31 17:53:13 -05:00
|
|
|
use std::mem;
|
|
|
|
use std::raw;
|
|
|
|
use std::slice::{Items, MutItems};
|
2014-05-13 10:35:42 -05:00
|
|
|
use std::vec::Vec;
|
2014-05-31 17:53:13 -05:00
|
|
|
use syntax::codemap::{Span, DUMMY_SP};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// HomogeneousTuple3 trait
|
|
|
|
//
|
|
|
|
// This could be moved into standard library at some point.
|
|
|
|
|
|
|
|
trait HomogeneousTuple3<T> {
|
|
|
|
fn len(&self) -> uint;
|
|
|
|
fn as_slice<'a>(&'a self) -> &'a [T];
|
|
|
|
fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T];
|
|
|
|
fn iter<'a>(&'a self) -> Items<'a, T>;
|
|
|
|
fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T>;
|
|
|
|
fn get<'a>(&'a self, index: uint) -> Option<&'a T>;
|
|
|
|
fn get_mut<'a>(&'a mut self, index: uint) -> Option<&'a mut T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> HomogeneousTuple3<T> for (T, T, T) {
|
|
|
|
fn len(&self) -> uint {
|
|
|
|
3
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_slice<'a>(&'a self) -> &'a [T] {
|
|
|
|
unsafe {
|
2014-06-25 14:47:34 -05:00
|
|
|
let ptr: *const T = mem::transmute(self);
|
2014-05-31 17:53:13 -05:00
|
|
|
let slice = raw::Slice { data: ptr, len: 3 };
|
|
|
|
mem::transmute(slice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
|
|
|
|
unsafe {
|
2014-06-25 14:47:34 -05:00
|
|
|
let ptr: *const T = mem::transmute(self);
|
2014-05-31 17:53:13 -05:00
|
|
|
let slice = raw::Slice { data: ptr, len: 3 };
|
|
|
|
mem::transmute(slice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iter<'a>(&'a self) -> Items<'a, T> {
|
|
|
|
let slice: &'a [T] = self.as_slice();
|
|
|
|
slice.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
|
|
|
|
self.as_mut_slice().mut_iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get<'a>(&'a self, index: uint) -> Option<&'a T> {
|
|
|
|
self.as_slice().get(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_mut<'a>(&'a mut self, index: uint) -> Option<&'a mut T> {
|
|
|
|
Some(&mut self.as_mut_slice()[index]) // wrong: fallible
|
|
|
|
}
|
|
|
|
}
|
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-13 10:35:42 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
/**
|
|
|
|
* A substitution mapping type/region parameters to new values. We
|
|
|
|
* identify each in-scope parameter by an *index* and a *parameter
|
|
|
|
* space* (which indices where the parameter is defined; see
|
|
|
|
* `ParamSpace`).
|
|
|
|
*/
|
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct Substs {
|
|
|
|
pub types: VecPerParamSpace<ty::t>,
|
|
|
|
pub regions: RegionSubsts,
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:35:42 -05:00
|
|
|
/**
|
|
|
|
* Represents the values to use when substituting lifetime parameters.
|
|
|
|
* If the value is `ErasedRegions`, then this subst is occurring during
|
|
|
|
* trans, and all region parameters will be replaced with `ty::ReStatic`. */
|
2014-05-14 14:31:30 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash)]
|
2014-05-13 10:35:42 -05:00
|
|
|
pub enum RegionSubsts {
|
|
|
|
ErasedRegions,
|
2014-05-31 17:53:13 -05:00
|
|
|
NonerasedRegions(VecPerParamSpace<ty::Region>)
|
2014-05-13 10:35:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Substs {
|
2014-05-31 17:53:13 -05:00
|
|
|
pub fn new(t: VecPerParamSpace<ty::t>,
|
|
|
|
r: VecPerParamSpace<ty::Region>)
|
|
|
|
-> Substs
|
|
|
|
{
|
|
|
|
Substs { types: t, regions: NonerasedRegions(r) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_type(t: Vec<ty::t>,
|
|
|
|
r: Vec<ty::Region>)
|
|
|
|
-> Substs
|
|
|
|
{
|
|
|
|
Substs::new(VecPerParamSpace::new(t, Vec::new(), Vec::new()),
|
|
|
|
VecPerParamSpace::new(r, Vec::new(), Vec::new()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_trait(t: Vec<ty::t>,
|
|
|
|
r: Vec<ty::Region>,
|
|
|
|
s: ty::t)
|
|
|
|
-> Substs
|
|
|
|
{
|
|
|
|
Substs::new(VecPerParamSpace::new(t, vec!(s), Vec::new()),
|
|
|
|
VecPerParamSpace::new(r, Vec::new(), Vec::new()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn erased(t: VecPerParamSpace<ty::t>) -> Substs
|
|
|
|
{
|
|
|
|
Substs { types: t, regions: ErasedRegions }
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:35:42 -05:00
|
|
|
pub fn empty() -> Substs {
|
|
|
|
Substs {
|
2014-05-31 17:53:13 -05:00
|
|
|
types: VecPerParamSpace::empty(),
|
|
|
|
regions: NonerasedRegions(VecPerParamSpace::empty()),
|
2014-05-13 10:35:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 19:53:48 -05:00
|
|
|
pub fn trans_empty() -> Substs {
|
|
|
|
Substs {
|
2014-05-31 17:53:13 -05:00
|
|
|
types: VecPerParamSpace::empty(),
|
2014-05-14 19:53:48 -05:00
|
|
|
regions: ErasedRegions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:35:42 -05:00
|
|
|
pub fn is_noop(&self) -> bool {
|
|
|
|
let regions_is_noop = match self.regions {
|
|
|
|
ErasedRegions => false, // may be used to canonicalize
|
2014-05-31 17:53:13 -05:00
|
|
|
NonerasedRegions(ref regions) => regions.is_empty(),
|
2014-05-13 10:35:42 -05:00
|
|
|
};
|
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
regions_is_noop && self.types.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn self_ty(&self) -> Option<ty::t> {
|
|
|
|
self.types.get_self().map(|&t| t)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_self_ty(&self, self_ty: ty::t) -> Substs {
|
|
|
|
assert!(self.self_ty().is_none());
|
|
|
|
let mut s = (*self).clone();
|
|
|
|
s.types.push(SelfSpace, self_ty);
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn regions<'a>(&'a self) -> &'a VecPerParamSpace<ty::Region> {
|
|
|
|
/*!
|
|
|
|
* Since ErasedRegions are only to be used in trans, most of
|
|
|
|
* the compiler can use this method to easily access the set
|
|
|
|
* of region substitutions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
match self.regions {
|
|
|
|
ErasedRegions => fail!("Erased regions only expected in trans"),
|
|
|
|
NonerasedRegions(ref r) => r
|
|
|
|
}
|
2014-05-13 10:35:42 -05:00
|
|
|
}
|
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
pub fn mut_regions<'a>(&'a mut self) -> &'a mut VecPerParamSpace<ty::Region> {
|
|
|
|
/*!
|
|
|
|
* Since ErasedRegions are only to be used in trans, most of
|
|
|
|
* the compiler can use this method to easily access the set
|
|
|
|
* of region substitutions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
match self.regions {
|
|
|
|
ErasedRegions => fail!("Erased regions only expected in trans"),
|
|
|
|
NonerasedRegions(ref mut r) => r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_method_from(self, substs: &Substs) -> Substs {
|
2014-07-04 11:47:59 -05:00
|
|
|
self.with_method(Vec::from_slice(substs.types.get_slice(FnSpace)),
|
|
|
|
Vec::from_slice(substs.regions().get_slice(FnSpace)))
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_method(self,
|
|
|
|
m_types: Vec<ty::t>,
|
|
|
|
m_regions: Vec<ty::Region>)
|
|
|
|
-> Substs
|
|
|
|
{
|
|
|
|
let Substs { types, regions } = self;
|
|
|
|
let types = types.with_vec(FnSpace, m_types);
|
|
|
|
let regions = regions.map(m_regions,
|
|
|
|
|r, m_regions| r.with_vec(FnSpace, m_regions));
|
|
|
|
Substs { types: types, regions: regions }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RegionSubsts {
|
|
|
|
fn map<A>(self,
|
|
|
|
a: A,
|
|
|
|
op: |VecPerParamSpace<ty::Region>, A| -> VecPerParamSpace<ty::Region>)
|
|
|
|
-> RegionSubsts {
|
|
|
|
match self {
|
|
|
|
ErasedRegions => ErasedRegions,
|
|
|
|
NonerasedRegions(r) => NonerasedRegions(op(r, a))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// ParamSpace
|
|
|
|
|
|
|
|
#[deriving(PartialOrd, Ord, PartialEq, Eq,
|
|
|
|
Clone, Hash, Encodable, Decodable, Show)]
|
|
|
|
pub enum ParamSpace {
|
|
|
|
TypeSpace, // Type parameters attached to a type definition, trait, or impl
|
|
|
|
SelfSpace, // Self parameter on a trait
|
|
|
|
FnSpace, // Type parameters attached to a method or fn
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParamSpace {
|
|
|
|
pub fn all() -> [ParamSpace, ..3] {
|
|
|
|
[TypeSpace, SelfSpace, FnSpace]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_uint(self) -> uint {
|
|
|
|
match self {
|
|
|
|
TypeSpace => 0,
|
|
|
|
SelfSpace => 1,
|
|
|
|
FnSpace => 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_uint(u: uint) -> ParamSpace {
|
|
|
|
match u {
|
|
|
|
0 => TypeSpace,
|
|
|
|
1 => SelfSpace,
|
|
|
|
2 => FnSpace,
|
|
|
|
_ => fail!("Invalid ParamSpace: {}", u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Vector of things sorted by param space. Used to keep
|
|
|
|
* the set of things declared on the type, self, or method
|
|
|
|
* distinct.
|
|
|
|
*/
|
|
|
|
#[deriving(PartialEq, Eq, Clone, Hash, Encodable, Decodable)]
|
|
|
|
pub struct VecPerParamSpace<T> {
|
2014-07-04 11:47:59 -05:00
|
|
|
// This was originally represented as a tuple with one Vec<T> for
|
|
|
|
// each variant of ParamSpace, and that remains the abstraction
|
|
|
|
// that it provides to its clients.
|
|
|
|
//
|
|
|
|
// Here is how the representation corresponds to the abstraction
|
|
|
|
// i.e. the "abstraction function" AF:
|
|
|
|
//
|
|
|
|
// AF(self) = (self.content.slice_to(self.type_limit),
|
|
|
|
// self.content.slice(self.type_limit, self.self_limit),
|
|
|
|
// self.content.slice_from(self.self_limit))
|
|
|
|
type_limit: uint,
|
|
|
|
self_limit: uint,
|
|
|
|
content: Vec<T>,
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
2014-07-04 09:39:28 -05:00
|
|
|
impl<T:Clone> VecPerParamSpace<T> {
|
|
|
|
pub fn push_all(&mut self, space: ParamSpace, values: &[T]) {
|
2014-07-04 11:47:59 -05:00
|
|
|
// FIXME (#15435): slow; O(n^2); could enhance vec to make it O(n).
|
|
|
|
for t in values.iter() {
|
|
|
|
self.push(space, t.clone());
|
|
|
|
}
|
2014-07-04 09:39:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
impl<T> VecPerParamSpace<T> {
|
2014-07-04 11:47:59 -05:00
|
|
|
fn limits(&self, space: ParamSpace) -> (uint, uint) {
|
|
|
|
match space {
|
|
|
|
TypeSpace => (0, self.type_limit),
|
|
|
|
SelfSpace => (self.type_limit, self.self_limit),
|
|
|
|
FnSpace => (self.self_limit, self.content.len()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
pub fn empty() -> VecPerParamSpace<T> {
|
|
|
|
VecPerParamSpace {
|
2014-07-04 11:47:59 -05:00
|
|
|
type_limit: 0,
|
|
|
|
self_limit: 0,
|
|
|
|
content: Vec::new()
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn params_from_type(types: Vec<T>) -> VecPerParamSpace<T> {
|
|
|
|
VecPerParamSpace::empty().with_vec(TypeSpace, types)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(t: Vec<T>, s: Vec<T>, f: Vec<T>) -> VecPerParamSpace<T> {
|
2014-07-04 11:47:59 -05:00
|
|
|
let type_limit = t.len();
|
|
|
|
let self_limit = t.len() + s.len();
|
|
|
|
let mut content = t;
|
|
|
|
content.push_all_move(s);
|
|
|
|
content.push_all_move(f);
|
2014-05-31 17:53:13 -05:00
|
|
|
VecPerParamSpace {
|
2014-07-04 11:47:59 -05:00
|
|
|
type_limit: type_limit,
|
|
|
|
self_limit: self_limit,
|
|
|
|
content: content,
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sort(t: Vec<T>, space: |&T| -> ParamSpace) -> VecPerParamSpace<T> {
|
|
|
|
let mut result = VecPerParamSpace::empty();
|
|
|
|
for t in t.move_iter() {
|
|
|
|
result.push(space(&t), t);
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2014-07-04 11:47:59 -05:00
|
|
|
/// Appends `value` to the vector associated with `space`.
|
|
|
|
///
|
|
|
|
/// Unlike the `push` method in `Vec`, this should not be assumed
|
|
|
|
/// to be a cheap operation (even when amortized over many calls).
|
2014-05-31 17:53:13 -05:00
|
|
|
pub fn push(&mut self, space: ParamSpace, value: T) {
|
2014-07-04 11:47:59 -05:00
|
|
|
let (_, limit) = self.limits(space);
|
|
|
|
match space {
|
|
|
|
TypeSpace => { self.type_limit += 1; self.self_limit += 1; }
|
|
|
|
SelfSpace => { self.self_limit += 1; }
|
|
|
|
FnSpace => {}
|
|
|
|
}
|
|
|
|
self.content.insert(limit, value);
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
2014-07-04 09:39:28 -05:00
|
|
|
pub fn pop(&mut self, space: ParamSpace) -> Option<T> {
|
2014-07-04 11:47:59 -05:00
|
|
|
let (start, limit) = self.limits(space);
|
|
|
|
if start == limit {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
match space {
|
|
|
|
TypeSpace => { self.type_limit -= 1; self.self_limit -= 1; }
|
|
|
|
SelfSpace => { self.self_limit -= 1; }
|
|
|
|
FnSpace => {}
|
|
|
|
}
|
|
|
|
self.content.remove(limit - 1)
|
|
|
|
}
|
2014-07-04 09:39:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn truncate(&mut self, space: ParamSpace, len: uint) {
|
2014-07-04 11:47:59 -05:00
|
|
|
// FIXME (#15435): slow; O(n^2); could enhance vec to make it O(n).
|
|
|
|
while self.len(space) > len {
|
|
|
|
self.pop(space);
|
|
|
|
}
|
2014-07-04 09:39:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn replace(&mut self, space: ParamSpace, elems: Vec<T>) {
|
2014-07-04 11:47:59 -05:00
|
|
|
// FIXME (#15435): slow; O(n^2); could enhance vec to make it O(n).
|
|
|
|
self.truncate(space, 0);
|
|
|
|
for t in elems.move_iter() {
|
|
|
|
self.push(space, t);
|
|
|
|
}
|
2014-07-04 09:39:28 -05:00
|
|
|
}
|
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
pub fn get_self<'a>(&'a self) -> Option<&'a T> {
|
2014-07-04 11:47:59 -05:00
|
|
|
let v = self.get_slice(SelfSpace);
|
2014-05-31 17:53:13 -05:00
|
|
|
assert!(v.len() <= 1);
|
2014-07-04 11:47:59 -05:00
|
|
|
if v.len() == 0 { None } else { Some(&v[0]) }
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self, space: ParamSpace) -> uint {
|
2014-07-04 11:47:59 -05:00
|
|
|
self.get_slice(space).len()
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
2014-07-04 09:39:28 -05:00
|
|
|
pub fn is_empty_in(&self, space: ParamSpace) -> bool {
|
2014-07-04 11:47:59 -05:00
|
|
|
self.len(space) == 0
|
2014-07-04 09:39:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_slice<'a>(&'a self, space: ParamSpace) -> &'a [T] {
|
2014-07-04 11:47:59 -05:00
|
|
|
let (start, limit) = self.limits(space);
|
|
|
|
self.content.slice(start, limit)
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
2014-07-04 11:47:59 -05:00
|
|
|
fn get_mut_slice<'a>(&'a mut self, space: ParamSpace) -> &'a mut [T] {
|
|
|
|
let (start, limit) = self.limits(space);
|
|
|
|
self.content.mut_slice(start, limit)
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn opt_get<'a>(&'a self,
|
|
|
|
space: ParamSpace,
|
|
|
|
index: uint)
|
|
|
|
-> Option<&'a T> {
|
2014-07-04 11:47:59 -05:00
|
|
|
let v = self.get_slice(space);
|
|
|
|
if index < v.len() { Some(&v[index]) } else { None }
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get<'a>(&'a self, space: ParamSpace, index: uint) -> &'a T {
|
2014-07-04 11:47:59 -05:00
|
|
|
&self.get_slice(space)[index]
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut<'a>(&'a mut self,
|
|
|
|
space: ParamSpace,
|
|
|
|
index: uint) -> &'a mut T {
|
2014-07-04 11:47:59 -05:00
|
|
|
&mut self.get_mut_slice(space)[index]
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
2014-07-04 11:47:59 -05:00
|
|
|
pub fn iter<'a>(&'a self) -> Items<'a,T> {
|
|
|
|
self.content.iter()
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
2014-07-04 09:39:28 -05:00
|
|
|
pub fn all_vecs(&self, pred: |&[T]| -> bool) -> bool {
|
2014-07-04 11:47:59 -05:00
|
|
|
let spaces = [TypeSpace, SelfSpace, FnSpace];
|
|
|
|
spaces.iter().all(|&space| { pred(self.get_slice(space)) })
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn all(&self, pred: |&T| -> bool) -> bool {
|
|
|
|
self.iter().all(pred)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn any(&self, pred: |&T| -> bool) -> bool {
|
|
|
|
self.iter().any(pred)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.all_vecs(|v| v.is_empty())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn map<U>(&self, pred: |&T| -> U) -> VecPerParamSpace<U> {
|
2014-07-04 11:47:59 -05:00
|
|
|
// FIXME (#15418): this could avoid allocating the intermediate
|
|
|
|
// Vec's, but note that the values of type_limit and self_limit
|
|
|
|
// also need to be kept in sync during construction.
|
|
|
|
VecPerParamSpace::new(
|
|
|
|
self.get_slice(TypeSpace).iter().map(|p| pred(p)).collect(),
|
|
|
|
self.get_slice(SelfSpace).iter().map(|p| pred(p)).collect(),
|
|
|
|
self.get_slice(FnSpace).iter().map(|p| pred(p)).collect())
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn map_rev<U>(&self, pred: |&T| -> U) -> VecPerParamSpace<U> {
|
|
|
|
/*!
|
|
|
|
* Executes the map but in reverse order. For hacky reasons, we rely
|
|
|
|
* on this in table.
|
|
|
|
*
|
|
|
|
* FIXME(#5527) -- order of eval becomes irrelevant with newer
|
|
|
|
* trait reform, which features an idempotent algorithm that
|
|
|
|
* can be run to a fixed point
|
|
|
|
*/
|
|
|
|
|
2014-07-04 11:47:59 -05:00
|
|
|
let mut fns: Vec<U> = self.get_slice(FnSpace).iter().rev().map(|p| pred(p)).collect();
|
2014-05-31 17:53:13 -05:00
|
|
|
|
|
|
|
// NB: Calling foo.rev().map().rev() causes the calls to map
|
|
|
|
// to occur in the wrong order. This was somewhat surprising
|
|
|
|
// to me, though it makes total sense.
|
|
|
|
fns.reverse();
|
|
|
|
|
2014-07-04 11:47:59 -05:00
|
|
|
let mut selfs: Vec<U> = self.get_slice(SelfSpace).iter().rev().map(|p| pred(p)).collect();
|
2014-05-31 17:53:13 -05:00
|
|
|
selfs.reverse();
|
2014-07-04 11:47:59 -05:00
|
|
|
let mut tys: Vec<U> = self.get_slice(TypeSpace).iter().rev().map(|p| pred(p)).collect();
|
2014-05-31 17:53:13 -05:00
|
|
|
tys.reverse();
|
|
|
|
VecPerParamSpace::new(tys, selfs, fns)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split(self) -> (Vec<T>, Vec<T>, Vec<T>) {
|
2014-07-04 11:47:59 -05:00
|
|
|
// FIXME (#15418): this does two traversals when in principle
|
|
|
|
// one would suffice. i.e. change to use `move_iter`.
|
|
|
|
let VecPerParamSpace { type_limit, self_limit, content } = self;
|
|
|
|
let mut i = 0;
|
|
|
|
let (prefix, fn_vec) = content.partition(|_| {
|
|
|
|
let on_left = i < self_limit;
|
|
|
|
i += 1;
|
|
|
|
on_left
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
let (type_vec, self_vec) = prefix.partition(|_| {
|
|
|
|
let on_left = i < type_limit;
|
|
|
|
i += 1;
|
|
|
|
on_left
|
|
|
|
});
|
|
|
|
|
|
|
|
(type_vec, self_vec, fn_vec)
|
2014-05-31 17:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_vec(mut self, space: ParamSpace, vec: Vec<T>)
|
|
|
|
-> VecPerParamSpace<T>
|
|
|
|
{
|
2014-07-04 11:47:59 -05:00
|
|
|
assert!(self.is_empty_in(space));
|
|
|
|
self.replace(space, vec);
|
2014-05-31 17:53:13 -05:00
|
|
|
self
|
2014-05-13 10:35:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-13 10:35:42 -05:00
|
|
|
fn subst(&self, tcx: &ty::ctxt, substs: &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-05-13 10:35:42 -05:00
|
|
|
substs: &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-05-13 10:35:42 -05:00
|
|
|
substs: &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-05-13 10:35:42 -05:00
|
|
|
substs: &'a Substs,
|
2014-02-02 04:53:23 -06:00
|
|
|
|
|
|
|
// 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 {
|
2014-05-31 17:53:13 -05:00
|
|
|
ty::ReEarlyBound(_, space, i, _) => {
|
2014-05-12 16:12:51 -05:00
|
|
|
match self.substs.regions {
|
2014-05-13 10:35:42 -05:00
|
|
|
ErasedRegions => ty::ReStatic,
|
2014-05-31 17:53:13 -05:00
|
|
|
NonerasedRegions(ref regions) => *regions.get(space, i),
|
2014-05-12 16:12:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => 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-31 17:53:13 -05:00
|
|
|
check(self, t, self.substs.types.opt_get(p.space, p.idx))
|
2013-04-09 14:33:18 -05:00
|
|
|
}
|
2014-05-31 17:53:13 -05: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-02-02 04:53:23 -06:00
|
|
|
};
|
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-31 17:53:13 -05:00
|
|
|
return t1;
|
|
|
|
|
|
|
|
fn check(this: &SubstFolder,
|
|
|
|
source_ty: ty::t,
|
|
|
|
opt_ty: Option<&ty::t>)
|
|
|
|
-> ty::t {
|
|
|
|
match opt_ty {
|
|
|
|
Some(t) => *t,
|
|
|
|
None => {
|
|
|
|
let span = this.span.unwrap_or(DUMMY_SP);
|
|
|
|
this.tcx().sess.span_bug(
|
|
|
|
span,
|
|
|
|
format!("Type parameter {} out of range \
|
|
|
|
when substituting (root type={})",
|
|
|
|
source_ty.repr(this.tcx()),
|
|
|
|
this.root_ty.repr(this.tcx())).as_slice());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|