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-08 22:54:49 -07: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.
|
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::ParamSpace::*;
|
|
|
|
pub use self::RegionSubsts::*;
|
|
|
|
|
2014-09-13 21:09:25 +03:00
|
|
|
use middle::ty::{mod, Ty};
|
2014-11-15 16:47:59 -05:00
|
|
|
use middle::ty_fold::{mod, TypeFoldable, TypeFolder};
|
2014-02-02 12:53:23 +02:00
|
|
|
use util::ppaux::Repr;
|
2014-02-01 15:57:59 +11:00
|
|
|
|
2014-07-12 00:51:55 -04:00
|
|
|
use std::fmt;
|
2014-10-25 22:27:15 +03:00
|
|
|
use std::slice::Items;
|
2014-05-13 11:35:42 -04:00
|
|
|
use std::vec::Vec;
|
2014-05-31 18:53:13 -04:00
|
|
|
use syntax::codemap::{Span, DUMMY_SP};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2014-05-13 11:35:42 -04:00
|
|
|
|
2014-11-24 20:06:06 -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`).
|
2014-07-12 00:51:55 -04:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct Substs<'tcx> {
|
|
|
|
pub types: VecPerParamSpace<Ty<'tcx>>,
|
2014-05-31 18:53:13 -04:00
|
|
|
pub regions: RegionSubsts,
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:06:06 -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-07-12 00:51:55 -04:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
|
2014-05-13 11:35:42 -04:00
|
|
|
pub enum RegionSubsts {
|
|
|
|
ErasedRegions,
|
2014-05-31 18:53:13 -04:00
|
|
|
NonerasedRegions(VecPerParamSpace<ty::Region>)
|
2014-05-13 11:35:42 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> Substs<'tcx> {
|
|
|
|
pub fn new(t: VecPerParamSpace<Ty<'tcx>>,
|
2014-05-31 18:53:13 -04:00
|
|
|
r: VecPerParamSpace<ty::Region>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> Substs<'tcx>
|
2014-05-31 18:53:13 -04:00
|
|
|
{
|
|
|
|
Substs { types: t, regions: NonerasedRegions(r) }
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn new_type(t: Vec<Ty<'tcx>>,
|
2014-05-31 18:53:13 -04:00
|
|
|
r: Vec<ty::Region>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> Substs<'tcx>
|
2014-05-31 18:53:13 -04:00
|
|
|
{
|
2014-10-29 21:29:16 -04:00
|
|
|
Substs::new(VecPerParamSpace::new(t, Vec::new(), Vec::new(), Vec::new()),
|
|
|
|
VecPerParamSpace::new(r, Vec::new(), Vec::new(), Vec::new()))
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn new_trait(t: Vec<Ty<'tcx>>,
|
2014-05-31 18:53:13 -04:00
|
|
|
r: Vec<ty::Region>,
|
2014-09-29 22:11:30 +03:00
|
|
|
a: Vec<Ty<'tcx>>,
|
|
|
|
s: Ty<'tcx>)
|
|
|
|
-> Substs<'tcx>
|
2014-05-31 18:53:13 -04:00
|
|
|
{
|
2014-10-31 06:36:07 -04:00
|
|
|
Substs::new(VecPerParamSpace::new(t, vec!(s), a, Vec::new()),
|
2014-10-29 21:29:16 -04:00
|
|
|
VecPerParamSpace::new(r, Vec::new(), Vec::new(), Vec::new()))
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn erased(t: VecPerParamSpace<Ty<'tcx>>) -> Substs<'tcx>
|
2014-05-31 18:53:13 -04:00
|
|
|
{
|
|
|
|
Substs { types: t, regions: ErasedRegions }
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn empty() -> Substs<'tcx> {
|
2014-05-13 11:35:42 -04:00
|
|
|
Substs {
|
2014-05-31 18:53:13 -04:00
|
|
|
types: VecPerParamSpace::empty(),
|
|
|
|
regions: NonerasedRegions(VecPerParamSpace::empty()),
|
2014-05-13 11:35:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn trans_empty() -> Substs<'tcx> {
|
2014-05-14 20:53:48 -04:00
|
|
|
Substs {
|
2014-05-31 18:53:13 -04:00
|
|
|
types: VecPerParamSpace::empty(),
|
2014-05-14 20:53:48 -04:00
|
|
|
regions: ErasedRegions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 11:35:42 -04:00
|
|
|
pub fn is_noop(&self) -> bool {
|
|
|
|
let regions_is_noop = match self.regions {
|
|
|
|
ErasedRegions => false, // may be used to canonicalize
|
2014-05-31 18:53:13 -04:00
|
|
|
NonerasedRegions(ref regions) => regions.is_empty(),
|
2014-05-13 11:35:42 -04:00
|
|
|
};
|
|
|
|
|
2014-05-31 18:53:13 -04:00
|
|
|
regions_is_noop && self.types.is_empty()
|
|
|
|
}
|
|
|
|
|
2014-11-08 06:59:10 -05:00
|
|
|
pub fn type_for_def(&self, ty_param_def: &ty::TypeParameterDef) -> Ty<'tcx> {
|
|
|
|
*self.types.get(ty_param_def.space, ty_param_def.index)
|
|
|
|
}
|
|
|
|
|
2014-11-15 17:25:05 -05:00
|
|
|
pub fn has_regions_escaping_depth(&self, depth: uint) -> bool {
|
|
|
|
self.types.iter().any(|&t| ty::type_escapes_depth(t, depth)) || {
|
|
|
|
match self.regions {
|
|
|
|
ErasedRegions =>
|
|
|
|
false,
|
|
|
|
NonerasedRegions(ref regions) =>
|
|
|
|
regions.iter().any(|r| r.escapes_depth(depth)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn self_ty(&self) -> Option<Ty<'tcx>> {
|
2014-05-31 18:53:13 -04:00
|
|
|
self.types.get_self().map(|&t| t)
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn with_self_ty(&self, self_ty: Ty<'tcx>) -> Substs<'tcx> {
|
2014-05-31 18:53:13 -04:00
|
|
|
assert!(self.self_ty().is_none());
|
|
|
|
let mut s = (*self).clone();
|
|
|
|
s.types.push(SelfSpace, self_ty);
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn erase_regions(self) -> Substs<'tcx> {
|
2014-10-06 13:36:53 +13:00
|
|
|
let Substs { types, regions: _ } = self;
|
2014-09-12 11:42:58 -04:00
|
|
|
Substs { types: types, regions: ErasedRegions }
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// 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.
|
2014-05-31 18:53:13 -04:00
|
|
|
pub fn regions<'a>(&'a self) -> &'a VecPerParamSpace<ty::Region> {
|
|
|
|
match self.regions {
|
2014-10-09 15:17:22 -04:00
|
|
|
ErasedRegions => panic!("Erased regions only expected in trans"),
|
2014-05-31 18:53:13 -04:00
|
|
|
NonerasedRegions(ref r) => r
|
|
|
|
}
|
2014-05-13 11:35:42 -04:00
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// 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.
|
2014-05-31 18:53:13 -04:00
|
|
|
pub fn mut_regions<'a>(&'a mut self) -> &'a mut VecPerParamSpace<ty::Region> {
|
|
|
|
match self.regions {
|
2014-10-09 15:17:22 -04:00
|
|
|
ErasedRegions => panic!("Erased regions only expected in trans"),
|
2014-05-31 18:53:13 -04:00
|
|
|
NonerasedRegions(ref mut r) => r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_method(self,
|
2014-09-29 22:11:30 +03:00
|
|
|
m_types: Vec<Ty<'tcx>>,
|
2014-05-31 18:53:13 -04:00
|
|
|
m_regions: Vec<ty::Region>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> Substs<'tcx>
|
2014-05-31 18:53:13 -04:00
|
|
|
{
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2014-11-15 17:25:05 -05:00
|
|
|
|
|
|
|
pub fn is_erased(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
ErasedRegions => true,
|
|
|
|
NonerasedRegions(_) => false,
|
|
|
|
}
|
|
|
|
}
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// ParamSpace
|
|
|
|
|
|
|
|
#[deriving(PartialOrd, Ord, PartialEq, Eq,
|
|
|
|
Clone, Hash, Encodable, Decodable, Show)]
|
|
|
|
pub enum ParamSpace {
|
2014-10-29 21:29:16 -04:00
|
|
|
TypeSpace, // Type parameters attached to a type definition, trait, or impl
|
|
|
|
SelfSpace, // Self parameter on a trait
|
|
|
|
AssocSpace, // Assoc types defined in a trait/impl
|
|
|
|
FnSpace, // Type parameters attached to a method or fn
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
impl Copy for ParamSpace {}
|
|
|
|
|
2014-05-31 18:53:13 -04:00
|
|
|
impl ParamSpace {
|
2014-10-29 21:29:16 -04:00
|
|
|
pub fn all() -> [ParamSpace, ..4] {
|
|
|
|
[TypeSpace, SelfSpace, AssocSpace, FnSpace]
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_uint(self) -> uint {
|
|
|
|
match self {
|
|
|
|
TypeSpace => 0,
|
|
|
|
SelfSpace => 1,
|
2014-10-29 21:29:16 -04:00
|
|
|
AssocSpace => 2,
|
|
|
|
FnSpace => 3,
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_uint(u: uint) -> ParamSpace {
|
|
|
|
match u {
|
|
|
|
0 => TypeSpace,
|
|
|
|
1 => SelfSpace,
|
2014-10-29 21:29:16 -04:00
|
|
|
2 => AssocSpace,
|
|
|
|
3 => FnSpace,
|
2014-10-09 15:17:22 -04:00
|
|
|
_ => panic!("Invalid ParamSpace: {}", u)
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:06:06 -05:00
|
|
|
/// Vector of things sorted by param space. Used to keep
|
|
|
|
/// the set of things declared on the type, self, or method
|
|
|
|
/// distinct.
|
2014-05-31 18:53:13 -04:00
|
|
|
#[deriving(PartialEq, Eq, Clone, Hash, Encodable, Decodable)]
|
|
|
|
pub struct VecPerParamSpace<T> {
|
2014-07-04 18:47:59 +02: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:
|
|
|
|
//
|
2014-10-29 21:29:16 -04:00
|
|
|
// AF(self) = (self.content[..self.type_limit],
|
|
|
|
// self.content[self.type_limit..self.self_limit],
|
|
|
|
// self.content[self.self_limit..self.assoc_limit],
|
|
|
|
// self.content[self.assoc_limit..])
|
2014-07-04 18:47:59 +02:00
|
|
|
type_limit: uint,
|
|
|
|
self_limit: uint,
|
2014-10-29 21:29:16 -04:00
|
|
|
assoc_limit: uint,
|
2014-07-04 18:47:59 +02:00
|
|
|
content: Vec<T>,
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2014-11-24 20:06:06 -05:00
|
|
|
/// The `split` function converts one `VecPerParamSpace` into this
|
|
|
|
/// `SeparateVecsPerParamSpace` structure.
|
2014-11-03 17:36:30 -05:00
|
|
|
pub struct SeparateVecsPerParamSpace<T> {
|
|
|
|
pub types: Vec<T>,
|
|
|
|
pub selfs: Vec<T>,
|
|
|
|
pub assocs: Vec<T>,
|
|
|
|
pub fns: Vec<T>,
|
|
|
|
}
|
|
|
|
|
2014-07-12 00:51:55 -04:00
|
|
|
impl<T:fmt::Show> fmt::Show for VecPerParamSpace<T> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
try!(write!(fmt, "VecPerParamSpace {{"));
|
|
|
|
for space in ParamSpace::all().iter() {
|
|
|
|
try!(write!(fmt, "{}: {}, ", *space, self.get_slice(*space)));
|
|
|
|
}
|
|
|
|
try!(write!(fmt, "}}"));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 18:53:13 -04:00
|
|
|
impl<T> VecPerParamSpace<T> {
|
2014-07-04 18:47:59 +02:00
|
|
|
fn limits(&self, space: ParamSpace) -> (uint, uint) {
|
|
|
|
match space {
|
|
|
|
TypeSpace => (0, self.type_limit),
|
|
|
|
SelfSpace => (self.type_limit, self.self_limit),
|
2014-10-29 21:29:16 -04:00
|
|
|
AssocSpace => (self.self_limit, self.assoc_limit),
|
|
|
|
FnSpace => (self.assoc_limit, self.content.len()),
|
2014-07-04 18:47:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 18:53:13 -04:00
|
|
|
pub fn empty() -> VecPerParamSpace<T> {
|
|
|
|
VecPerParamSpace {
|
2014-07-04 18:47:59 +02:00
|
|
|
type_limit: 0,
|
|
|
|
self_limit: 0,
|
2014-10-29 21:29:16 -04:00
|
|
|
assoc_limit: 0,
|
2014-07-04 18:47:59 +02:00
|
|
|
content: Vec::new()
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn params_from_type(types: Vec<T>) -> VecPerParamSpace<T> {
|
|
|
|
VecPerParamSpace::empty().with_vec(TypeSpace, types)
|
|
|
|
}
|
|
|
|
|
2014-05-28 22:26:56 -07:00
|
|
|
/// `t` is the type space.
|
|
|
|
/// `s` is the self space.
|
2014-10-29 21:29:16 -04:00
|
|
|
/// `a` is the assoc space.
|
2014-05-28 22:26:56 -07:00
|
|
|
/// `f` is the fn space.
|
2014-10-29 21:29:16 -04:00
|
|
|
pub fn new(t: Vec<T>, s: Vec<T>, a: Vec<T>, f: Vec<T>) -> VecPerParamSpace<T> {
|
2014-07-04 18:47:59 +02:00
|
|
|
let type_limit = t.len();
|
2014-10-29 21:29:16 -04:00
|
|
|
let self_limit = type_limit + s.len();
|
|
|
|
let assoc_limit = self_limit + a.len();
|
|
|
|
|
2014-07-04 18:47:59 +02:00
|
|
|
let mut content = t;
|
2014-10-14 23:05:01 -07:00
|
|
|
content.extend(s.into_iter());
|
2014-10-29 21:29:16 -04:00
|
|
|
content.extend(a.into_iter());
|
2014-10-14 23:05:01 -07:00
|
|
|
content.extend(f.into_iter());
|
2014-10-29 21:29:16 -04:00
|
|
|
|
2014-05-31 18:53:13 -04:00
|
|
|
VecPerParamSpace {
|
2014-07-04 18:47:59 +02:00
|
|
|
type_limit: type_limit,
|
|
|
|
self_limit: self_limit,
|
2014-10-29 21:29:16 -04:00
|
|
|
assoc_limit: assoc_limit,
|
2014-07-04 18:47:59 +02:00
|
|
|
content: content,
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 21:29:16 -04:00
|
|
|
fn new_internal(content: Vec<T>, type_limit: uint, self_limit: uint, assoc_limit: uint)
|
2014-09-12 10:53:35 -04:00
|
|
|
-> VecPerParamSpace<T>
|
|
|
|
{
|
|
|
|
VecPerParamSpace {
|
|
|
|
type_limit: type_limit,
|
|
|
|
self_limit: self_limit,
|
2014-10-29 21:29:16 -04:00
|
|
|
assoc_limit: assoc_limit,
|
2014-09-12 10:53:35 -04:00
|
|
|
content: content,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 18:47:59 +02: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 18:53:13 -04:00
|
|
|
pub fn push(&mut self, space: ParamSpace, value: T) {
|
2014-07-04 18:47:59 +02:00
|
|
|
let (_, limit) = self.limits(space);
|
|
|
|
match space {
|
2014-10-29 21:29:16 -04:00
|
|
|
TypeSpace => { self.type_limit += 1; self.self_limit += 1; self.assoc_limit += 1; }
|
|
|
|
SelfSpace => { self.self_limit += 1; self.assoc_limit += 1; }
|
|
|
|
AssocSpace => { self.assoc_limit += 1; }
|
|
|
|
FnSpace => { }
|
2014-07-04 18:47:59 +02:00
|
|
|
}
|
|
|
|
self.content.insert(limit, value);
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 16:39:28 +02:00
|
|
|
pub fn pop(&mut self, space: ParamSpace) -> Option<T> {
|
2014-07-04 18:47:59 +02:00
|
|
|
let (start, limit) = self.limits(space);
|
|
|
|
if start == limit {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
match space {
|
2014-10-29 21:29:16 -04:00
|
|
|
TypeSpace => { self.type_limit -= 1; self.self_limit -= 1; self.assoc_limit -= 1; }
|
|
|
|
SelfSpace => { self.self_limit -= 1; self.assoc_limit -= 1; }
|
|
|
|
AssocSpace => { self.assoc_limit -= 1; }
|
|
|
|
FnSpace => {}
|
2014-07-04 18:47:59 +02:00
|
|
|
}
|
|
|
|
self.content.remove(limit - 1)
|
|
|
|
}
|
2014-07-04 16:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn truncate(&mut self, space: ParamSpace, len: uint) {
|
2014-07-04 18:47:59 +02: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 16:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn replace(&mut self, space: ParamSpace, elems: Vec<T>) {
|
2014-07-04 18:47:59 +02:00
|
|
|
// FIXME (#15435): slow; O(n^2); could enhance vec to make it O(n).
|
|
|
|
self.truncate(space, 0);
|
2014-09-14 20:27:36 -07:00
|
|
|
for t in elems.into_iter() {
|
2014-07-04 18:47:59 +02:00
|
|
|
self.push(space, t);
|
|
|
|
}
|
2014-07-04 16:39:28 +02:00
|
|
|
}
|
|
|
|
|
2014-05-31 18:53:13 -04:00
|
|
|
pub fn get_self<'a>(&'a self) -> Option<&'a T> {
|
2014-07-04 18:47:59 +02:00
|
|
|
let v = self.get_slice(SelfSpace);
|
2014-05-31 18:53:13 -04:00
|
|
|
assert!(v.len() <= 1);
|
2014-07-04 18:47:59 +02:00
|
|
|
if v.len() == 0 { None } else { Some(&v[0]) }
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self, space: ParamSpace) -> uint {
|
2014-07-04 18:47:59 +02:00
|
|
|
self.get_slice(space).len()
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 16:39:28 +02:00
|
|
|
pub fn is_empty_in(&self, space: ParamSpace) -> bool {
|
2014-07-04 18:47:59 +02:00
|
|
|
self.len(space) == 0
|
2014-07-04 16:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_slice<'a>(&'a self, space: ParamSpace) -> &'a [T] {
|
2014-07-04 18:47:59 +02:00
|
|
|
let (start, limit) = self.limits(space);
|
|
|
|
self.content.slice(start, limit)
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2014-08-06 11:59:40 +02:00
|
|
|
pub fn get_mut_slice<'a>(&'a mut self, space: ParamSpace) -> &'a mut [T] {
|
2014-07-04 18:47:59 +02:00
|
|
|
let (start, limit) = self.limits(space);
|
2014-09-14 20:27:36 -07:00
|
|
|
self.content.slice_mut(start, limit)
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn opt_get<'a>(&'a self,
|
|
|
|
space: ParamSpace,
|
|
|
|
index: uint)
|
|
|
|
-> Option<&'a T> {
|
2014-07-04 18:47:59 +02:00
|
|
|
let v = self.get_slice(space);
|
|
|
|
if index < v.len() { Some(&v[index]) } else { None }
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get<'a>(&'a self, space: ParamSpace, index: uint) -> &'a T {
|
2014-07-04 18:47:59 +02:00
|
|
|
&self.get_slice(space)[index]
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 18:47:59 +02:00
|
|
|
pub fn iter<'a>(&'a self) -> Items<'a,T> {
|
|
|
|
self.content.iter()
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2014-11-15 17:25:05 -05:00
|
|
|
pub fn iter_enumerated<'a>(&'a self) -> EnumeratedItems<'a,T> {
|
|
|
|
EnumeratedItems::new(self)
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
pub fn as_slice(&self) -> &[T] {
|
|
|
|
self.content.as_slice()
|
|
|
|
}
|
|
|
|
|
2014-07-04 16:39:28 +02:00
|
|
|
pub fn all_vecs(&self, pred: |&[T]| -> bool) -> bool {
|
2014-07-04 18:47:59 +02:00
|
|
|
let spaces = [TypeSpace, SelfSpace, FnSpace];
|
|
|
|
spaces.iter().all(|&space| { pred(self.get_slice(space)) })
|
2014-05-31 18:53:13 -04: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-09-12 10:53:35 -04:00
|
|
|
let result = self.iter().map(pred).collect();
|
|
|
|
VecPerParamSpace::new_internal(result,
|
|
|
|
self.type_limit,
|
2014-10-29 21:29:16 -04:00
|
|
|
self.self_limit,
|
|
|
|
self.assoc_limit)
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-11-15 17:25:05 -05:00
|
|
|
pub fn map_enumerated<U>(&self, pred: |(ParamSpace, uint, &T)| -> U) -> VecPerParamSpace<U> {
|
|
|
|
let result = self.iter_enumerated().map(pred).collect();
|
|
|
|
VecPerParamSpace::new_internal(result,
|
|
|
|
self.type_limit,
|
|
|
|
self.self_limit,
|
|
|
|
self.assoc_limit)
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:53:35 -04:00
|
|
|
pub fn map_move<U>(self, pred: |T| -> U) -> VecPerParamSpace<U> {
|
2014-11-03 17:36:30 -05:00
|
|
|
let SeparateVecsPerParamSpace {
|
|
|
|
types: t,
|
|
|
|
selfs: s,
|
|
|
|
assocs: a,
|
|
|
|
fns: f
|
|
|
|
} = self.split();
|
|
|
|
|
2014-09-14 20:27:36 -07:00
|
|
|
VecPerParamSpace::new(t.into_iter().map(|p| pred(p)).collect(),
|
|
|
|
s.into_iter().map(|p| pred(p)).collect(),
|
2014-10-29 21:29:16 -04:00
|
|
|
a.into_iter().map(|p| pred(p)).collect(),
|
2014-09-14 20:27:36 -07:00
|
|
|
f.into_iter().map(|p| pred(p)).collect())
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2014-11-03 17:36:30 -05:00
|
|
|
pub fn split(self) -> SeparateVecsPerParamSpace<T> {
|
2014-10-29 21:29:16 -04:00
|
|
|
let VecPerParamSpace { type_limit, self_limit, assoc_limit, content } = self;
|
2014-07-04 18:47:59 +02:00
|
|
|
|
2014-10-29 21:29:16 -04:00
|
|
|
let mut content_iter = content.into_iter();
|
2014-07-04 18:47:59 +02:00
|
|
|
|
2014-11-03 17:36:30 -05:00
|
|
|
SeparateVecsPerParamSpace {
|
|
|
|
types: content_iter.by_ref().take(type_limit).collect(),
|
|
|
|
selfs: content_iter.by_ref().take(self_limit - type_limit).collect(),
|
|
|
|
assocs: content_iter.by_ref().take(assoc_limit - self_limit).collect(),
|
|
|
|
fns: content_iter.collect()
|
|
|
|
}
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_vec(mut self, space: ParamSpace, vec: Vec<T>)
|
|
|
|
-> VecPerParamSpace<T>
|
|
|
|
{
|
2014-07-04 18:47:59 +02:00
|
|
|
assert!(self.is_empty_in(space));
|
|
|
|
self.replace(space, vec);
|
2014-05-31 18:53:13 -04:00
|
|
|
self
|
2014-05-13 11:35:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 17:25:05 -05:00
|
|
|
pub struct EnumeratedItems<'a,T:'a> {
|
|
|
|
vec: &'a VecPerParamSpace<T>,
|
|
|
|
space_index: uint,
|
|
|
|
elem_index: uint
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,T> EnumeratedItems<'a,T> {
|
|
|
|
fn new(v: &'a VecPerParamSpace<T>) -> EnumeratedItems<'a,T> {
|
|
|
|
let mut result = EnumeratedItems { vec: v, space_index: 0, elem_index: 0 };
|
|
|
|
result.adjust_space();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn adjust_space(&mut self) {
|
|
|
|
let spaces = ParamSpace::all();
|
|
|
|
while
|
|
|
|
self.space_index < spaces.len() &&
|
|
|
|
self.elem_index >= self.vec.len(spaces[self.space_index])
|
|
|
|
{
|
|
|
|
self.space_index += 1;
|
|
|
|
self.elem_index = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,T> Iterator<(ParamSpace, uint, &'a T)> for EnumeratedItems<'a,T> {
|
|
|
|
fn next(&mut self) -> Option<(ParamSpace, uint, &'a T)> {
|
|
|
|
let spaces = ParamSpace::all();
|
|
|
|
if self.space_index < spaces.len() {
|
|
|
|
let space = spaces[self.space_index];
|
|
|
|
let index = self.elem_index;
|
|
|
|
let item = self.vec.get(space, index);
|
|
|
|
|
|
|
|
self.elem_index += 1;
|
|
|
|
self.adjust_space();
|
|
|
|
|
|
|
|
Some((space, index, item))
|
|
|
|
} else {
|
|
|
|
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-08 22:54:49 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Public trait `Subst`
|
|
|
|
//
|
|
|
|
// Just call `foo.subst(tcx, substs)` to perform a substitution across
|
2014-05-12 17:12:51 -04: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-08 22:54:49 -07:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub trait Subst<'tcx> {
|
|
|
|
fn subst(&self, tcx: &ty::ctxt<'tcx>, substs: &Substs<'tcx>) -> Self {
|
2014-02-02 12:53:23 +02:00
|
|
|
self.subst_spanned(tcx, substs, None)
|
|
|
|
}
|
2014-05-12 17:12:51 -04:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn subst_spanned(&self, tcx: &ty::ctxt<'tcx>,
|
|
|
|
substs: &Substs<'tcx>,
|
2014-05-12 17:12:51 -04: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-08 22:54:49 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx, T:TypeFoldable<'tcx>> Subst<'tcx> for T {
|
2014-05-12 17:12:51 -04:00
|
|
|
fn subst_spanned(&self,
|
2014-09-29 22:11:30 +03:00
|
|
|
tcx: &ty::ctxt<'tcx>,
|
|
|
|
substs: &Substs<'tcx>,
|
2014-05-12 17:12:51 -04:00
|
|
|
span: Option<Span>)
|
|
|
|
-> T
|
|
|
|
{
|
|
|
|
let mut folder = SubstFolder { tcx: tcx,
|
|
|
|
substs: substs,
|
|
|
|
span: span,
|
|
|
|
root_ty: None,
|
2014-11-15 17:25:05 -05:00
|
|
|
ty_stack_depth: 0,
|
|
|
|
region_binders_passed: 0 };
|
2014-05-12 17:12:51 -04:00
|
|
|
(*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-08 22:54:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-12 17:12:51 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// The actual substitution engine itself is a type folder.
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
struct SubstFolder<'a, 'tcx: 'a> {
|
|
|
|
tcx: &'a ty::ctxt<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
substs: &'a Substs<'tcx>,
|
2014-02-02 12:53:23 +02:00
|
|
|
|
|
|
|
// The location for which the substitution is performed, if available.
|
|
|
|
span: Option<Span>,
|
|
|
|
|
|
|
|
// The root type that is being substituted, if available.
|
2014-09-29 22:11:30 +03:00
|
|
|
root_ty: Option<Ty<'tcx>>,
|
2014-05-12 17:12:51 -04:00
|
|
|
|
|
|
|
// Depth of type stack
|
|
|
|
ty_stack_depth: uint,
|
2014-11-15 16:47:59 -05:00
|
|
|
|
|
|
|
// Number of region binders we have passed through while doing the substitution
|
|
|
|
region_binders_passed: uint,
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
|
|
|
|
fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> { self.tcx }
|
2013-10-29 05:25:18 -04:00
|
|
|
|
2014-11-15 16:47:59 -05:00
|
|
|
fn enter_region_binder(&mut self) {
|
|
|
|
self.region_binders_passed += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit_region_binder(&mut self) {
|
|
|
|
self.region_binders_passed -= 1;
|
|
|
|
}
|
|
|
|
|
2013-10-29 05:25:18 -04:00
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
2014-05-12 17:12:51 -04: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
|
2014-11-25 20:16:28 -05:00
|
|
|
// the specialized routine `ty::replace_late_regions()`.
|
2014-05-12 17:12:51 -04:00
|
|
|
match r {
|
2014-07-09 15:47:55 -04:00
|
|
|
ty::ReEarlyBound(_, space, i, region_name) => {
|
2014-05-12 17:12:51 -04:00
|
|
|
match self.substs.regions {
|
2014-05-13 11:35:42 -04:00
|
|
|
ErasedRegions => ty::ReStatic,
|
2014-07-09 15:47:55 -04:00
|
|
|
NonerasedRegions(ref regions) =>
|
|
|
|
match regions.opt_get(space, i) {
|
2014-11-15 16:47:59 -05:00
|
|
|
Some(&r) => {
|
|
|
|
self.shift_region_through_binders(r)
|
|
|
|
}
|
2014-07-09 15:47:55 -04:00
|
|
|
None => {
|
|
|
|
let span = self.span.unwrap_or(DUMMY_SP);
|
|
|
|
self.tcx().sess.span_bug(
|
|
|
|
span,
|
|
|
|
format!("Type parameter out of range \
|
2014-07-30 17:47:54 -07:00
|
|
|
when substituting in region {} (root type={}) \
|
|
|
|
(space={}, index={})",
|
2014-07-09 15:47:55 -04:00
|
|
|
region_name.as_str(),
|
2014-07-30 17:47:54 -07:00
|
|
|
self.root_ty.repr(self.tcx()),
|
|
|
|
space, i).as_slice());
|
2014-07-09 15:47:55 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-12 17:12:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => r
|
|
|
|
}
|
2013-10-29 05:25:18 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2013-10-29 05:25:18 -04:00
|
|
|
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-08 22:54:49 -07:00
|
|
|
}
|
|
|
|
|
2014-05-12 17:12:51 -04: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;
|
|
|
|
|
2014-10-31 10:51:16 +02:00
|
|
|
let t1 = match t.sty {
|
2013-04-09 12:33:18 -07:00
|
|
|
ty::ty_param(p) => {
|
2014-11-15 16:47:59 -05:00
|
|
|
self.ty_for_param(p, t)
|
2013-04-09 12:33:18 -07:00
|
|
|
}
|
2014-05-31 18:53:13 -04: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-08 22:54:49 -07:00
|
|
|
}
|
2014-02-02 12:53:23 +02: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-08 22:54:49 -07:00
|
|
|
|
2014-05-12 17:12:51 -04: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-08 22:54:49 -07:00
|
|
|
}
|
|
|
|
|
2014-05-31 18:53:13 -04:00
|
|
|
return t1;
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 18:53:13 -04:00
|
|
|
|
2014-11-15 16:47:59 -05:00
|
|
|
impl<'a,'tcx> SubstFolder<'a,'tcx> {
|
2014-09-29 22:11:30 +03:00
|
|
|
fn ty_for_param(&self, p: ty::ParamTy, source_ty: Ty<'tcx>) -> Ty<'tcx> {
|
2014-11-15 16:47:59 -05:00
|
|
|
// Look up the type in the substitutions. It really should be in there.
|
|
|
|
let opt_ty = self.substs.types.opt_get(p.space, p.idx);
|
|
|
|
let ty = match opt_ty {
|
|
|
|
Some(t) => *t,
|
|
|
|
None => {
|
|
|
|
let span = self.span.unwrap_or(DUMMY_SP);
|
|
|
|
self.tcx().sess.span_bug(
|
|
|
|
span,
|
|
|
|
format!("Type parameter `{}` ({}/{}/{}) out of range \
|
2014-10-29 21:29:16 -04:00
|
|
|
when substituting (root type={}) substs={}",
|
2014-11-15 16:47:59 -05:00
|
|
|
p.repr(self.tcx()),
|
|
|
|
source_ty.repr(self.tcx()),
|
|
|
|
p.space,
|
|
|
|
p.idx,
|
|
|
|
self.root_ty.repr(self.tcx()),
|
|
|
|
self.substs.repr(self.tcx())).as_slice());
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
2014-11-15 16:47:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
self.shift_regions_through_binders(ty)
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// It is sometimes necessary to adjust the debruijn indices during substitution. This occurs
|
|
|
|
/// when we are substituting a type with escaping regions into a context where we have passed
|
|
|
|
/// through region binders. That's quite a mouthful. Let's see an example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// type Func<A> = fn(A);
|
|
|
|
/// type MetaFunc = for<'a> fn(Func<&'a int>)
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The type `MetaFunc`, when fully expanded, will be
|
|
|
|
///
|
|
|
|
/// for<'a> fn(fn(&'a int))
|
|
|
|
/// ^~ ^~ ^~~
|
|
|
|
/// | | |
|
|
|
|
/// | | DebruijnIndex of 2
|
|
|
|
/// Binders
|
|
|
|
///
|
|
|
|
/// Here the `'a` lifetime is bound in the outer function, but appears as an argument of the
|
|
|
|
/// inner one. Therefore, that appearance will have a DebruijnIndex of 2, because we must skip
|
|
|
|
/// over the inner binder (remember that we count Debruijn indices from 1). However, in the
|
|
|
|
/// definition of `MetaFunc`, the binder is not visible, so the type `&'a int` will have a
|
|
|
|
/// debruijn index of 1. It's only during the substitution that we can see we must increase the
|
|
|
|
/// depth by 1 to account for the binder that we passed through.
|
|
|
|
///
|
|
|
|
/// As a second example, consider this twist:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// type FuncTuple<A> = (A,fn(A));
|
|
|
|
/// type MetaFuncTuple = for<'a> fn(FuncTuple<&'a int>)
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Here the final type will be:
|
|
|
|
///
|
|
|
|
/// for<'a> fn((&'a int, fn(&'a int)))
|
|
|
|
/// ^~~ ^~~
|
|
|
|
/// | |
|
|
|
|
/// DebruijnIndex of 1 |
|
|
|
|
/// DebruijnIndex of 2
|
|
|
|
///
|
|
|
|
/// As indicated in the diagram, here the same type `&'a int` is substituted once, but in the
|
|
|
|
/// first case we do not increase the Debruijn index and in the second case we do. The reason
|
|
|
|
/// is that only in the second case have we passed through a fn binder.
|
2014-09-29 22:11:30 +03:00
|
|
|
fn shift_regions_through_binders(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
2014-11-15 16:47:59 -05:00
|
|
|
debug!("shift_regions(ty={}, region_binders_passed={}, type_has_escaping_regions={})",
|
|
|
|
ty.repr(self.tcx()), self.region_binders_passed, ty::type_has_escaping_regions(ty));
|
|
|
|
|
|
|
|
if self.region_binders_passed == 0 || !ty::type_has_escaping_regions(ty) {
|
|
|
|
return ty;
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
2014-11-15 16:47:59 -05:00
|
|
|
|
|
|
|
let result = ty_fold::shift_regions(self.tcx(), self.region_binders_passed, &ty);
|
|
|
|
debug!("shift_regions: shifted result = {}", result.repr(self.tcx()));
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shift_region_through_binders(&self, region: ty::Region) -> ty::Region {
|
|
|
|
ty_fold::shift_region(region, self.region_binders_passed)
|
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-08 22:54:49 -07:00
|
|
|
}
|
|
|
|
}
|