Skip to content

Commit d9190da

Browse files
committed
Refactor Substs methods on generic parameters
1 parent 030f10f commit d9190da

File tree

15 files changed

+393
-306
lines changed

15 files changed

+393
-306
lines changed

src/librustc/infer/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use hir::def_id::DefId;
2121
use middle::free_region::RegionRelations;
2222
use middle::region;
2323
use middle::lang_items;
24-
use ty::subst::Substs;
24+
use ty::subst::{UnpackedKind, Substs};
2525
use ty::{TyVid, IntVid, FloatVid};
26-
use ty::{self, Ty, TyCtxt};
26+
use ty::{self, Ty, TyCtxt, GenericParamDefKind};
2727
use ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
2828
use ty::fold::TypeFoldable;
2929
use ty::relate::RelateResult;
@@ -941,10 +941,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
941941
span: Span,
942942
def_id: DefId)
943943
-> &'tcx Substs<'tcx> {
944-
Substs::for_item(self.tcx, def_id, |def, _| {
945-
self.region_var_for_def(span, def)
946-
}, |def, _| {
947-
self.type_var_for_def(span, def)
944+
Substs::for_item(self.tcx, def_id, |param, _| {
945+
match param.kind {
946+
GenericParamDefKind::Lifetime => {
947+
UnpackedKind::Lifetime(self.region_var_for_def(span, param))
948+
}
949+
GenericParamDefKind::Type(_) => {
950+
UnpackedKind::Type(self.type_var_for_def(span, param))
951+
}
952+
}
948953
})
949954
}
950955

src/librustc/traits/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use hir::def_id::DefId;
2222
use infer::outlives::env::OutlivesEnvironment;
2323
use middle::region;
2424
use middle::const_val::ConstEvalErr;
25-
use ty::subst::Substs;
26-
use ty::{self, AdtKind, Slice, Ty, TyCtxt, TypeFoldable, ToPredicate};
25+
use ty::subst::{UnpackedKind, Substs};
26+
use ty::{self, AdtKind, Slice, Ty, TyCtxt, GenericParamDefKind, TypeFoldable, ToPredicate};
2727
use ty::error::{ExpectedFound, TypeError};
2828
use infer::{InferCtxt};
2929

@@ -841,10 +841,16 @@ fn vtable_methods<'a, 'tcx>(
841841
// the method may have some early-bound lifetimes, add
842842
// regions for those
843843
let substs = trait_ref.map_bound(|trait_ref| {
844-
Substs::for_item(
845-
tcx, def_id,
846-
|_, _| tcx.types.re_erased,
847-
|def, _| trait_ref.substs.type_for_def(def))
844+
Substs::for_item(tcx, def_id, |param, _| {
845+
match param.kind {
846+
GenericParamDefKind::Lifetime => {
847+
UnpackedKind::Lifetime(tcx.types.re_erased)
848+
}
849+
GenericParamDefKind::Type(_) => {
850+
UnpackedKind::Type(trait_ref.substs.type_for_def(param))
851+
}
852+
}
853+
})
848854
});
849855

850856
// the trait type may have higher-ranked lifetimes in it;

src/librustc/ty/context.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use middle::lang_items;
3232
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
3333
use middle::stability;
3434
use mir::{self, Mir, interpret};
35-
use ty::subst::{Kind, Substs, Subst};
35+
use ty::subst::{Kind, UnpackedKind, Substs, Subst};
3636
use ty::ReprOptions;
3737
use ty::Instance;
3838
use traits;
@@ -44,6 +44,7 @@ use ty::{PolyFnSig, InferTy, ParamTy, ProjectionTy, ExistentialPredicate, Predic
4444
use ty::RegionKind;
4545
use ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid};
4646
use ty::TypeVariants::*;
47+
use ty::GenericParamDefKind;
4748
use ty::layout::{LayoutDetails, TargetDataLayout};
4849
use ty::maps;
4950
use ty::steal::Steal;
@@ -2325,16 +2326,22 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23252326
pub fn mk_box(self, ty: Ty<'tcx>) -> Ty<'tcx> {
23262327
let def_id = self.require_lang_item(lang_items::OwnedBoxLangItem);
23272328
let adt_def = self.adt_def(def_id);
2328-
let substs = Substs::for_item(self, def_id, |_, _| bug!(), |def, substs| {
2329-
if def.index == 0 {
2330-
ty
2331-
} else {
2332-
match def.kind {
2333-
ty::GenericParamDefKind::Type(ty_param) => {
2334-
assert!(ty_param.has_default);
2335-
self.type_of(def.def_id).subst(self, substs)
2329+
let substs = Substs::for_item(self, def_id, |param, substs| {
2330+
match param.kind {
2331+
GenericParamDefKind::Lifetime => bug!(),
2332+
GenericParamDefKind::Type(_) => {
2333+
if param.index == 0 {
2334+
UnpackedKind::Type(ty)
2335+
} else {
2336+
match param.kind {
2337+
ty::GenericParamDefKind::Type(ty_param) => {
2338+
assert!(ty_param.has_default);
2339+
UnpackedKind::Type(
2340+
self.type_of(param.def_id).subst(self, substs))
2341+
}
2342+
_ => unreachable!()
2343+
}
23362344
}
2337-
_ => unreachable!()
23382345
}
23392346
}
23402347
});

src/librustc/ty/subst.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Type substitutions.
1212

1313
use hir::def_id::DefId;
14-
use ty::{self, Lift, Slice, Region, Ty, TyCtxt};
14+
use ty::{self, Lift, Slice, Region, Ty, TyCtxt, GenericParamDefKind};
1515
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1616

1717
use serialize::{self, Encodable, Encoder, Decodable, Decoder};
@@ -174,80 +174,80 @@ impl<'tcx> Decodable for Kind<'tcx> {
174174
}
175175
}
176176

177-
/// A substitution mapping type/region parameters to new values.
177+
/// A substitution mapping generic parameters to new values.
178178
pub type Substs<'tcx> = Slice<Kind<'tcx>>;
179179

180180
impl<'a, 'gcx, 'tcx> Substs<'tcx> {
181181
/// Creates a Substs that maps each generic parameter to itself.
182182
pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId)
183183
-> &'tcx Substs<'tcx> {
184-
Substs::for_item(tcx, def_id, |def, _| {
185-
tcx.mk_region(ty::ReEarlyBound(def.to_early_bound_region_data()))
186-
}, |def, _| tcx.mk_ty_param_from_def(def))
184+
Substs::for_item(tcx, def_id, |param, _| {
185+
match param.kind {
186+
GenericParamDefKind::Lifetime => {
187+
UnpackedKind::Lifetime(
188+
tcx.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())))
189+
}
190+
GenericParamDefKind::Type(_) => {
191+
UnpackedKind::Type(tcx.mk_ty_param_from_def(param))
192+
}
193+
}
194+
})
187195
}
188196

189197
/// Creates a Substs for generic parameter definitions,
190-
/// by calling closures to obtain each region and type.
198+
/// by calling closures to obtain each kind.
191199
/// The closures get to observe the Substs as they're
192200
/// being built, which can be used to correctly
193-
/// substitute defaults of type parameters.
194-
pub fn for_item<FR, FT>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
195-
def_id: DefId,
196-
mut mk_region: FR,
197-
mut mk_type: FT)
198-
-> &'tcx Substs<'tcx>
199-
where FR: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> ty::Region<'tcx>,
200-
FT: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Ty<'tcx> {
201+
/// substitute defaults of generic parameters.
202+
pub fn for_item<F>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
203+
def_id: DefId,
204+
mut mk_kind: F)
205+
-> &'tcx Substs<'tcx>
206+
where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> UnpackedKind<'tcx>
207+
{
201208
let defs = tcx.generics_of(def_id);
202209
let mut substs = Vec::with_capacity(defs.count());
203-
Substs::fill_item(&mut substs, tcx, defs, &mut mk_region, &mut mk_type);
210+
Substs::fill_item(&mut substs, tcx, defs, &mut mk_kind);
204211
tcx.intern_substs(&substs)
205212
}
206213

207-
pub fn extend_to<FR, FT>(&self,
208-
tcx: TyCtxt<'a, 'gcx, 'tcx>,
209-
def_id: DefId,
210-
mut mk_region: FR,
211-
mut mk_type: FT)
212-
-> &'tcx Substs<'tcx>
213-
where FR: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> ty::Region<'tcx>,
214-
FT: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Ty<'tcx>
214+
pub fn extend_to<F>(&self,
215+
tcx: TyCtxt<'a, 'gcx, 'tcx>,
216+
def_id: DefId,
217+
mut mk_kind: F)
218+
-> &'tcx Substs<'tcx>
219+
where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> UnpackedKind<'tcx>
215220
{
216221
let defs = tcx.generics_of(def_id);
217222
let mut result = Vec::with_capacity(defs.count());
218223
result.extend(self[..].iter().cloned());
219-
Substs::fill_single(&mut result, defs, &mut mk_region, &mut mk_type);
224+
Substs::fill_single(&mut result, defs, &mut mk_kind);
220225
tcx.intern_substs(&result)
221226
}
222227

223-
pub fn fill_item<FR, FT>(substs: &mut Vec<Kind<'tcx>>,
228+
pub fn fill_item<F>(substs: &mut Vec<Kind<'tcx>>,
224229
tcx: TyCtxt<'a, 'gcx, 'tcx>,
225230
defs: &ty::Generics,
226-
mk_region: &mut FR,
227-
mk_type: &mut FT)
228-
where FR: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> ty::Region<'tcx>,
229-
FT: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Ty<'tcx> {
231+
mk_kind: &mut F)
232+
where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> UnpackedKind<'tcx>
233+
{
230234

231235
if let Some(def_id) = defs.parent {
232236
let parent_defs = tcx.generics_of(def_id);
233-
Substs::fill_item(substs, tcx, parent_defs, mk_region, mk_type);
237+
Substs::fill_item(substs, tcx, parent_defs, mk_kind);
234238
}
235-
Substs::fill_single(substs, defs, mk_region, mk_type)
239+
Substs::fill_single(substs, defs, mk_kind)
236240
}
237241

238-
fn fill_single<FR, FT>(substs: &mut Vec<Kind<'tcx>>,
242+
fn fill_single<F>(substs: &mut Vec<Kind<'tcx>>,
239243
defs: &ty::Generics,
240-
mk_region: &mut FR,
241-
mk_type: &mut FT)
242-
where FR: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> ty::Region<'tcx>,
243-
FT: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Ty<'tcx> {
244+
mk_kind: &mut F)
245+
where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> UnpackedKind<'tcx>
246+
{
244247
for param in &defs.params {
245-
let kind = match param.kind {
246-
ty::GenericParamDefKind::Lifetime => mk_region(param, substs).into(),
247-
ty::GenericParamDefKind::Type(_) => mk_type(param, substs).into(),
248-
};
248+
let kind = mk_kind(param, substs);
249249
assert_eq!(param.index as usize, substs.len());
250-
substs.push(kind);
250+
substs.push(kind.pack());
251251
}
252252
}
253253

src/librustc/ty/util.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use hir;
1717
use ich::NodeIdHashingMode;
1818
use middle::const_val::ConstVal;
1919
use traits::{self, ObligationCause};
20-
use ty::{self, Ty, TyCtxt, TypeFoldable};
20+
use ty::{self, Ty, TyCtxt, GenericParamDefKind, TypeFoldable};
2121
use ty::fold::TypeVisitor;
22-
use ty::subst::UnpackedKind;
22+
use ty::subst::{Substs, UnpackedKind};
2323
use ty::maps::TyCtxtAt;
2424
use ty::TypeVariants::*;
2525
use ty::layout::{Integer, IntegerExt};
@@ -573,11 +573,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
573573

574574
/// Given the def-id of some item that has no type parameters, make
575575
/// a suitable "empty substs" for it.
576-
pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> &'tcx ty::Substs<'tcx> {
577-
ty::Substs::for_item(self, item_def_id,
578-
|_, _| self.types.re_erased,
579-
|_, _| {
580-
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
576+
pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> &'tcx Substs<'tcx> {
577+
Substs::for_item(self, item_def_id, |param, _| {
578+
match param.kind {
579+
GenericParamDefKind::Lifetime => UnpackedKind::Lifetime(self.types.re_erased),
580+
GenericParamDefKind::Type(_) => {
581+
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
582+
}
583+
}
581584
})
582585
}
583586

src/librustc_mir/monomorphize/collector.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ use rustc::hir::def_id::DefId;
196196
use rustc::middle::const_val::ConstVal;
197197
use rustc::mir::interpret::{AllocId, ConstValue};
198198
use rustc::middle::lang_items::{ExchangeMallocFnLangItem, StartFnLangItem};
199-
use rustc::ty::subst::{Substs, Kind};
200-
use rustc::ty::{self, TypeFoldable, Ty, TyCtxt};
199+
use rustc::ty::subst::{Substs, Kind, UnpackedKind};
200+
use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind};
201201
use rustc::ty::adjustment::CustomCoerceUnsized;
202202
use rustc::session::config;
203203
use rustc::mir::{self, Location, Promoted};
@@ -1112,10 +1112,16 @@ fn create_mono_items_for_default_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11121112
continue;
11131113
}
11141114

1115-
let substs = Substs::for_item(tcx,
1116-
method.def_id,
1117-
|_, _| tcx.types.re_erased,
1118-
|def, _| trait_ref.substs.type_for_def(def));
1115+
let substs = Substs::for_item(tcx, method.def_id, |param, _| {
1116+
match param.kind {
1117+
GenericParamDefKind::Lifetime => {
1118+
UnpackedKind::Lifetime(tcx.types.re_erased)
1119+
}
1120+
GenericParamDefKind::Type(_) => {
1121+
UnpackedKind::Type(trait_ref.substs.type_for_def(param))
1122+
}
1123+
}
1124+
});
11191125

11201126
let instance = ty::Instance::resolve(tcx,
11211127
ty::ParamEnv::reveal_all(),

src/librustc_mir/shim.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use rustc::hir;
1212
use rustc::hir::def_id::DefId;
1313
use rustc::infer;
1414
use rustc::mir::*;
15-
use rustc::ty::{self, Ty, TyCtxt};
16-
use rustc::ty::subst::{Kind, Subst, Substs};
15+
use rustc::ty::{self, Ty, TyCtxt, GenericParamDefKind};
16+
use rustc::ty::subst::{Kind, UnpackedKind, Subst, Substs};
1717
use rustc::ty::maps::Providers;
1818

1919
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
@@ -427,12 +427,12 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
427427
) {
428428
let tcx = self.tcx;
429429

430-
let substs = Substs::for_item(
431-
tcx,
432-
self.def_id,
433-
|_, _| tcx.types.re_erased,
434-
|_, _| ty
435-
);
430+
let substs = Substs::for_item(tcx, self.def_id, |param, _| {
431+
match param.kind {
432+
GenericParamDefKind::Lifetime => UnpackedKind::Lifetime(tcx.types.re_erased),
433+
GenericParamDefKind::Type(_) => UnpackedKind::Type(ty),
434+
}
435+
});
436436

437437
// `func == Clone::clone(&ty) -> ty`
438438
let func_ty = tcx.mk_fn_def(self.def_id, substs);

0 commit comments

Comments
 (0)