Skip to content

Commit 15ef2c2

Browse files
committed
Convert astconv to request bounds through the AstConv interface
rather than poking through the `TypeParameterDef` directly.
1 parent e033a23 commit 15ef2c2

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

src/librustc/middle/ty.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use middle::region;
5555
use middle::resolve_lifetime;
5656
use middle::infer;
5757
use middle::stability;
58-
use middle::subst::{self, Subst, Substs, VecPerParamSpace};
58+
use middle::subst::{self, ParamSpace, Subst, Substs, VecPerParamSpace};
5959
use middle::traits;
6060
use middle::ty;
6161
use middle::ty_fold::{self, TypeFoldable, TypeFolder};
@@ -2996,6 +2996,13 @@ impl<'tcx> TyS<'tcx> {
29962996
_ => None,
29972997
}
29982998
}
2999+
3000+
pub fn is_param(&self, space: ParamSpace, index: u32) -> bool {
3001+
match self.sty {
3002+
ty::ty_param(ref data) => data.space == space && data.idx == index,
3003+
_ => false,
3004+
}
3005+
}
29993006
}
30003007

30013008
pub fn walk_ty<'tcx, F>(ty_root: Ty<'tcx>, mut f: F)

src/librustc_typeck/astconv.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use middle::const_eval;
5353
use middle::def;
5454
use middle::resolve_lifetime as rl;
5555
use middle::privacy::{AllPublic, LastMod};
56-
use middle::subst::{FnSpace, TypeSpace, SelfSpace, Subst, Substs};
56+
use middle::subst::{FnSpace, ParamSpace, TypeSpace, SelfSpace, Subst, Substs};
5757
use middle::traits;
5858
use middle::ty::{self, RegionEscape, ToPolyTraitRef, Ty};
5959
use rscope::{self, UnelidableRscope, RegionScope, ElidableRscope,
@@ -77,6 +77,8 @@ pub trait AstConv<'tcx> {
7777

7878
fn get_trait_def(&self, id: ast::DefId) -> Rc<ty::TraitDef<'tcx>>;
7979

80+
fn get_type_parameter_bounds(&self, space: ParamSpace, index: u32) -> Vec<ty::PolyTraitRef<'tcx>>;
81+
8082
/// Return an (optional) substitution to convert bound type parameters that
8183
/// are in scope into free ones. This function should only return Some
8284
/// within a fn body.
@@ -1011,7 +1013,9 @@ fn associated_path_def_to_ty<'tcx>(this: &AstConv<'tcx>,
10111013

10121014
// FIXME(#20300) -- search where clauses, not bounds
10131015
suitable_bounds =
1014-
traits::transitive_bounds(tcx, &ty_param_def.bounds.trait_bounds)
1016+
traits::transitive_bounds(tcx,
1017+
&this.get_type_parameter_bounds(ty_param_def.space,
1018+
ty_param_def.index))
10151019
.filter(|b| trait_defines_associated_type_named(this, b.def_id(), assoc_name))
10161020
.collect();
10171021
}

src/librustc_typeck/check/mod.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ use middle::subst::{self, Subst, Substs, VecPerParamSpace, ParamSpace, TypeSpace
9797
use middle::traits;
9898
use middle::ty::{FnSig, GenericPredicates, VariantInfo, TypeScheme};
9999
use middle::ty::{Disr, ParamTy, ParameterEnvironment};
100-
use middle::ty::{self, HasProjectionTypes, RegionEscape, Ty};
100+
use middle::ty::{self, HasProjectionTypes, RegionEscape, ToPolyTraitRef, Ty};
101101
use middle::ty::liberate_late_bound_regions;
102102
use middle::ty::{MethodCall, MethodCallee, MethodMap, ObjectCastMap};
103103
use middle::ty_fold::{TypeFolder, TypeFoldable};
@@ -1218,6 +1218,30 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
12181218
Some(&self.inh.param_env.free_substs)
12191219
}
12201220

1221+
fn get_type_parameter_bounds(&self,
1222+
space: ParamSpace,
1223+
index: u32)
1224+
-> Vec<ty::PolyTraitRef<'tcx>>
1225+
{
1226+
self.inh.param_env.caller_bounds
1227+
.iter()
1228+
.filter_map(|predicate| {
1229+
match *predicate {
1230+
ty::Predicate::Trait(ref data) => {
1231+
if data.0.self_ty().is_param(space, index) {
1232+
Some(data.to_poly_trait_ref())
1233+
} else {
1234+
None
1235+
}
1236+
}
1237+
_ => {
1238+
None
1239+
}
1240+
}
1241+
})
1242+
.collect()
1243+
}
1244+
12211245
fn ty_infer(&self, _span: Span) -> Ty<'tcx> {
12221246
self.infcx().next_ty_var()
12231247
}

src/librustc_typeck/collect.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ struct CrateCtxt<'a,'tcx:'a> {
145145
tcx: &'a ty::ctxt<'tcx>,
146146
}
147147

148-
#[allow(dead_code)] // just temporary, for generics
149148
struct ItemCtxt<'a,'tcx:'a> {
150149
ccx: &'a CrateCtxt<'a,'tcx>,
151150
generics: &'a ty::Generics<'tcx>,
@@ -241,6 +240,19 @@ impl<'a, 'tcx> AstConv<'tcx> for ItemCtxt<'a, 'tcx> {
241240
get_trait_def(self.ccx, id)
242241
}
243242

243+
fn get_type_parameter_bounds(&self,
244+
param: subst::ParamSpace,
245+
index: u32)
246+
-> Vec<ty::PolyTraitRef<'tcx>>
247+
{
248+
// TODO out of range indices can occur when you have something
249+
// like fn foo<T:U::X,U>() { }
250+
match self.generics.types.opt_get(param, index as usize) {
251+
Some(def) => def.bounds.trait_bounds.clone(),
252+
None => Vec::new(),
253+
}
254+
}
255+
244256
fn ty_infer(&self, span: Span) -> Ty<'tcx> {
245257
span_err!(self.tcx().sess, span, E0121,
246258
"the type placeholder `_` is not allowed within types on item signatures");
@@ -1596,7 +1608,7 @@ fn ty_generics<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
15961608

15971609
// Now create the real type parameters.
15981610
for (i, param) in types.iter().enumerate() {
1599-
let def = get_or_create_type_parameter_def(ccx, space, param, i as u32, where_clause);
1611+
let def = get_or_create_type_parameter_def(ccx, &result, space, param, i as u32);
16001612
debug!("ty_generics: def for type param: {:?}, {:?}", def, space);
16011613
result.types.push(space, def);
16021614
}
@@ -1605,6 +1617,7 @@ fn ty_generics<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
16051617
}
16061618

16071619
fn get_or_create_type_parameter_def<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
1620+
generics_so_far: &ty::Generics<'tcx>,
16081621
space: subst::ParamSpace,
16091622
param: &ast::TyParam,
16101623
index: u32,
@@ -1619,7 +1632,7 @@ fn get_or_create_type_parameter_def<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
16191632

16201633
let param_ty = ty::ParamTy::new(space, index, param.ident.name);
16211634
let bounds = compute_bounds(ccx,
1622-
&ty::Generics::empty(),
1635+
generics_so_far,
16231636
param_ty.to_ty(ccx.tcx),
16241637
&param.bounds,
16251638
SizedByDefault::Yes,

0 commit comments

Comments
 (0)