Skip to content

Substitute type params in default type params using them. #11982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 128 additions & 43 deletions src/librustc/middle/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@
use middle::ty;
use middle::ty_fold;
use middle::ty_fold::TypeFolder;
use util::ppaux::Repr;

use std::rc::Rc;
use syntax::codemap::Span;
use syntax::opt_vec::OptVec;

///////////////////////////////////////////////////////////////////////////
// Public trait `Subst`
//
// Just call `foo.subst(tcx, substs)` to perform a substitution across
// `foo`.
// Or use `foo.subst_spanned(tcx, substs, Some(span))` when there is more
// information available (for better errors).

pub trait Subst {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> Self;
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> Self {
self.subst_spanned(tcx, substs, None)
}
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> Self;
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -36,19 +45,32 @@ pub trait Subst {
// our current method/trait matching algorithm. - Niko

impl Subst for ty::t {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::t {
if ty::substs_is_noop(substs) {
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ty::t {
if ty::substs_is_noop(substs) && !ty::type_has_params(*self) {
*self
} else {
let mut folder = SubstFolder {tcx: tcx, substs: substs};
let mut folder = SubstFolder {
tcx: tcx,
substs: substs,
span: span,
root_ty: Some(*self)
};
folder.fold_ty(*self)
}
}
}

struct SubstFolder<'a> {
tcx: ty::ctxt,
substs: &'a ty::substs
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.
root_ty: Option<ty::t>
}

impl<'a> TypeFolder for SubstFolder<'a> {
Expand All @@ -65,14 +87,42 @@ impl<'a> TypeFolder for SubstFolder<'a> {

match ty::get(t).sty {
ty::ty_param(p) => {
self.substs.tps[p.idx]
if p.idx < self.substs.tps.len() {
self.substs.tps[p.idx]
} else {
let root_msg = match self.root_ty {
Some(root) => format!(" in the substitution of `{}`",
root.repr(self.tcx)),
None => ~""
};
let m = format!("missing type param `{}`{}",
t.repr(self.tcx), root_msg);
match self.span {
Some(span) => self.tcx.sess.span_err(span, m),
None => self.tcx.sess.err(m)
}
ty::mk_err()
}
}
ty::ty_self(_) => {
self.substs.self_ty.expect("ty_self not found in substs")
}
_ => {
ty_fold::super_fold_ty(self, t)
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)),
None => ~""
};
let m = format!("missing `Self` type param{}", root_msg);
match self.span {
Some(span) => self.tcx.sess.span_err(span, m),
None => self.tcx.sess.err(m)
}
ty::mk_err()
}
}
}
_ => ty_fold::super_fold_ty(self, t)
}
}
}
Expand All @@ -81,112 +131,145 @@ impl<'a> TypeFolder for SubstFolder<'a> {
// Other types

impl<T:Subst> Subst for ~[T] {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ~[T] {
self.map(|t| t.subst(tcx, substs))
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ~[T] {
self.map(|t| t.subst_spanned(tcx, substs, span))
}
}
impl<T:Subst> Subst for Rc<T> {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> Rc<T> {
Rc::new(self.borrow().subst(tcx, substs))
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> Rc<T> {
Rc::new(self.borrow().subst_spanned(tcx, substs, span))
}
}

impl<T:Subst> Subst for OptVec<T> {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> OptVec<T> {
self.map(|t| t.subst(tcx, substs))
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> OptVec<T> {
self.map(|t| t.subst_spanned(tcx, substs, span))
}
}

impl<T:Subst + 'static> Subst for @T {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> @T {
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> @T {
match self {
t => @(**t).subst(tcx, substs)
t => @(**t).subst_spanned(tcx, substs, span)
}
}
}

impl<T:Subst> Subst for Option<T> {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> Option<T> {
self.as_ref().map(|t| t.subst(tcx, substs))
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> Option<T> {
self.as_ref().map(|t| t.subst_spanned(tcx, substs, span))
}
}

impl Subst for ty::TraitRef {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::TraitRef {
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ty::TraitRef {
ty::TraitRef {
def_id: self.def_id,
substs: self.substs.subst(tcx, substs)
substs: self.substs.subst_spanned(tcx, substs, span)
}
}
}

impl Subst for ty::substs {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::substs {
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ty::substs {
ty::substs {
regions: self.regions.subst(tcx, substs),
self_ty: self.self_ty.map(|typ| typ.subst(tcx, substs)),
tps: self.tps.map(|typ| typ.subst(tcx, substs))
regions: self.regions.subst_spanned(tcx, substs, span),
self_ty: self.self_ty.map(|typ| typ.subst_spanned(tcx, substs, span)),
tps: self.tps.map(|typ| typ.subst_spanned(tcx, substs, span))
}
}
}

impl Subst for ty::RegionSubsts {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::RegionSubsts {
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ty::RegionSubsts {
match *self {
ty::ErasedRegions => {
ty::ErasedRegions
}
ty::NonerasedRegions(ref regions) => {
ty::NonerasedRegions(regions.subst(tcx, substs))
ty::NonerasedRegions(regions.subst_spanned(tcx, substs, span))
}
}
}
}

impl Subst for ty::BareFnTy {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::BareFnTy {
let mut folder = SubstFolder {tcx: tcx, substs: substs};
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ty::BareFnTy {
let mut folder = SubstFolder {
tcx: tcx,
substs: substs,
span: span,
root_ty: None
};
folder.fold_bare_fn_ty(self)
}
}

impl Subst for ty::ParamBounds {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::ParamBounds {
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ty::ParamBounds {
ty::ParamBounds {
builtin_bounds: self.builtin_bounds,
trait_bounds: self.trait_bounds.subst(tcx, substs)
trait_bounds: self.trait_bounds.subst_spanned(tcx, substs, span)
}
}
}

impl Subst for ty::TypeParameterDef {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::TypeParameterDef {
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ty::TypeParameterDef {
ty::TypeParameterDef {
ident: self.ident,
def_id: self.def_id,
bounds: self.bounds.subst(tcx, substs),
default: self.default.map(|x| x.subst(tcx, substs))
bounds: self.bounds.subst_spanned(tcx, substs, span),
default: self.default.map(|x| x.subst_spanned(tcx, substs, span))
}
}
}

impl Subst for ty::Generics {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::Generics {
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ty::Generics {
ty::Generics {
type_param_defs: self.type_param_defs.subst(tcx, substs),
region_param_defs: self.region_param_defs.subst(tcx, substs),
type_param_defs: self.type_param_defs.subst_spanned(tcx, substs, span),
region_param_defs: self.region_param_defs.subst_spanned(tcx, substs, span),
}
}
}

impl Subst for ty::RegionParameterDef {
fn subst(&self, _: ty::ctxt, _: &ty::substs) -> ty::RegionParameterDef {
fn subst_spanned(&self, _: ty::ctxt,
_: &ty::substs,
_: Option<Span>) -> ty::RegionParameterDef {
*self
}
}

impl Subst for ty::Region {
fn subst(&self, _tcx: ty::ctxt, substs: &ty::substs) -> ty::Region {
fn subst_spanned(&self, _tcx: ty::ctxt,
substs: &ty::substs,
_: Option<Span>) -> ty::Region {
// Note: This routine only handles regions that are bound on
// type declarationss and other outer declarations, not those
// bound in *fn types*. Region substitution of the bound
Expand All @@ -206,10 +289,12 @@ impl Subst for ty::Region {
}

impl Subst for ty::ty_param_bounds_and_ty {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::ty_param_bounds_and_ty {
fn subst_spanned(&self, tcx: ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> ty::ty_param_bounds_and_ty {
ty::ty_param_bounds_and_ty {
generics: self.generics.subst(tcx, substs),
ty: self.ty.subst(tcx, substs)
generics: self.generics.subst_spanned(tcx, substs, span),
ty: self.ty.subst_spanned(tcx, substs, span)
}
}
}
16 changes: 12 additions & 4 deletions src/librustc/middle/typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

use middle::const_eval;
use middle::lint;
use middle::subst::Subst;
use middle::ty::{substs};
use middle::ty::{ty_param_substs_and_ty};
use middle::ty;
Expand Down Expand Up @@ -228,16 +229,23 @@ fn ast_path_substs<AC:AstConv,RS:RegionScope>(
~"provided type arguments with defaults");
}

let defaults = decl_generics.type_param_defs().slice_from(supplied_ty_param_count)
.iter().map(|&x| x.default.unwrap());
let tps = path.segments.iter().flat_map(|s| s.types.iter())
.map(|&a_t| ast_ty_to_ty(this, rscope, a_t))
.chain(defaults).collect();
substs {
.collect();

let mut substs = substs {
regions: ty::NonerasedRegions(regions),
self_ty: self_ty,
tps: tps
};

for param in decl_generics.type_param_defs()
.slice_from(supplied_ty_param_count).iter() {
let ty = param.default.unwrap().subst_spanned(tcx, &substs, Some(path.span));
substs.tps.push(ty);
}

substs
}

pub fn ast_path_to_substs_and_ty<AC:AstConv,
Expand Down
Loading