Skip to content

Commit 9200bde

Browse files
committed
Refactor generic params loops
1 parent 18f77e2 commit 9200bde

File tree

20 files changed

+95
-165
lines changed

20 files changed

+95
-165
lines changed

src/librustc/hir/lowering.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ impl<'a> LoweringContext<'a> {
374374

375375
if item_lowered {
376376
let item_lifetimes = match self.lctx.items.get(&item.id).unwrap().node {
377-
hir::Item_::ItemImpl(_, _, _, ref generics, .. ) |
378-
hir::Item_::ItemTrait(_, _, ref generics, .. ) => {
377+
hir::Item_::ItemImpl(_, _, _, ref generics, ..)
378+
| hir::Item_::ItemTrait(_, _, ref generics, ..) => {
379379
generics.lifetimes().cloned().collect::<Vec<_>>()
380380
}
381381
_ => Vec::new(),
@@ -1895,13 +1895,11 @@ impl<'a> LoweringContext<'a> {
18951895
GenericParam::Lifetime(ref lifetime_def) => {
18961896
hir::GenericParam::Lifetime(self.lower_lifetime_def(lifetime_def))
18971897
}
1898-
GenericParam::Type(ref ty_param) => {
1899-
hir::GenericParam::Type(self.lower_ty_param(
1900-
ty_param,
1901-
add_bounds.get(&ty_param.id).map_or(&[][..], |x| &x),
1902-
itctx,
1903-
))
1904-
}
1898+
GenericParam::Type(ref ty_param) => hir::GenericParam::Type(self.lower_ty_param(
1899+
ty_param,
1900+
add_bounds.get(&ty_param.id).map_or(&[][..], |x| &x),
1901+
itctx,
1902+
)),
19051903
})
19061904
.collect()
19071905
}

src/librustc/hir/print.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,10 +2043,7 @@ impl<'a> State<'a> {
20432043
Ok(())
20442044
}
20452045

2046-
pub fn print_generic_params(&mut self,
2047-
generic_params: &[hir::GenericParam])
2048-
-> io::Result<()>
2049-
{
2046+
pub fn print_generic_params(&mut self, generic_params: &[hir::GenericParam]) -> io::Result<()> {
20502047
if !generic_params.is_empty() {
20512048
self.s.word("<")?;
20522049

src/librustc/infer/anon_types/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
313313
// `['a]` for the first impl trait and `'b` for the
314314
// second.
315315
let mut least_region = None;
316-
for index in abstract_type_generics.params.iter().filter_map(|param| {
317-
if let GenericParamDefKind::Lifetime(_) = param.kind {
318-
// Find the index of this region in the list of substitutions.
319-
Some(param.index as usize)
320-
} else {
321-
None
316+
for param in &abstract_type_generics.params {
317+
match param.kind {
318+
GenericParamDefKind::Lifetime(_) => {}
319+
_ => continue
322320
}
323-
}) {
324321
// Get the value supplied for this region from the substs.
325-
let subst_arg = anon_defn.substs.region_at(index);
322+
let subst_arg = anon_defn.substs.region_at(param.index as usize);
326323

327324
// Compute the least upper bound of it with the other regions.
328325
debug!("constrain_anon_types: least_region={:?}", least_region);

src/librustc/traits/auto_trait.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,14 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
222222
});
223223

224224
let names_map: FxHashSet<String> = generics
225-
.regions
225+
.params
226226
.iter()
227-
.map(|l| l.name.to_string())
227+
.filter_map(|param| {
228+
match param.kind {
229+
ty::GenericParamDefKind::Lifetime(_) => Some(param.name.to_string()),
230+
_ => None
231+
}
232+
})
228233
.collect();
229234

230235
let body_ids: FxHashSet<_> = infcx

src/librustc/traits/error_reporting.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,16 +379,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
379379
flags.push(("_Self".to_string(), Some(self.tcx.type_of(def.did).to_string())));
380380
}
381381

382-
for param in generics.params.iter().filter(|param| {
383-
match param.kind {
384-
GenericParamDefKind::Type(_) => true,
385-
GenericParamDefKind::Lifetime(_) => false,
386-
}
387-
}) {
382+
for param in generics.params.iter() {
388383
let name = param.name.to_string();
389-
let ty = trait_ref.substs.type_for_def(&param);
390-
let ty_str = ty.to_string();
391-
flags.push((name.clone(), Some(ty_str.clone())));
384+
let value = match param.kind {
385+
GenericParamDefKind::Type(_) => {
386+
let ty = trait_ref.substs.type_for_def(&param);
387+
ty.to_string()
388+
},
389+
GenericParamDefKind::Lifetime(_) => continue,
390+
};
391+
flags.push((name.clone(), Some(value.clone())));
392392
}
393393

394394
if let Some(true) = self_ty.ty_to_def_id().map(|def_id| def_id.is_local()) {

src/librustc/traits/on_unimplemented.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,12 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString {
254254
Position::ArgumentNamed(s) if s == name => (),
255255
// So is `{A}` if A is a type parameter
256256
Position::ArgumentNamed(s) => match generics.params.iter().find(|param| {
257-
match param.kind {
258-
GenericParamDefKind::Type(_) => param.name == s,
259-
GenericParamDefKind::Lifetime(_) => false,
260-
}
257+
param.name == s
261258
}) {
262259
Some(_) => (),
263260
None => {
264261
span_err!(tcx.sess, span, E0230,
265-
"there is no type parameter \
262+
"there is no parameter \
266263
{} on trait {}",
267264
s, name);
268265
result = Err(ErrorReported);

src/librustc/ty/context.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use mir::{self, Mir, interpret};
3535
use ty::subst::{Kind, Substs, Subst};
3636
use ty::ReprOptions;
3737
use ty::Instance;
38-
use ty::GenericParamDefKind;
3938
use traits;
4039
use traits::{Clause, Clauses, Goal, Goals};
4140
use ty::{self, Ty, TypeAndMut};
@@ -2326,20 +2325,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23262325
pub fn mk_box(self, ty: Ty<'tcx>) -> Ty<'tcx> {
23272326
let def_id = self.require_lang_item(lang_items::OwnedBoxLangItem);
23282327
let adt_def = self.adt_def(def_id);
2329-
let generics = self.generics_of(def_id);
2330-
let mut substs = vec![Kind::from(ty)];
2331-
// Add defaults for other generic params if there are some.
2332-
for (def_id, has_default) in generics.params.iter().filter_map(|param| {
2333-
match param.kind {
2334-
GenericParamDefKind::Type(ty) => Some((param.def_id, ty.has_default)),
2335-
GenericParamDefKind::Lifetime(_) => None
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)
2336+
}
2337+
_ => unreachable!()
2338+
}
23362339
}
2337-
}).skip(1) {
2338-
assert!(has_default);
2339-
let ty = self.type_of(def_id).subst(self, &substs);
2340-
substs.push(ty.into());
2341-
}
2342-
let substs = self.mk_substs(substs.into_iter());
2340+
});
23432341
self.mk_ty(TyAdt(adt_def, substs))
23442342
}
23452343

src/librustc/ty/mod.rs

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ pub struct Generics {
804804
pub parent_count: usize,
805805
pub params: Vec<GenericParamDef>,
806806

807-
/// Reverse map to the `index` field of each `GenericParamDef`'s inner type
807+
/// Reverse map to the `index` field of each `GenericParamDef`
808808
pub param_def_id_to_index: FxHashMap<DefId, u32>,
809809

810810
pub has_self: bool,
@@ -836,13 +836,11 @@ impl<'a, 'gcx, 'tcx> Generics {
836836
}
837837

838838
pub fn requires_monomorphization(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool {
839-
if self.params.iter().any(|param| {
839+
for param in self.params.iter() {
840840
match param.kind {
841-
GenericParamDefKind::Type(_) => true,
842-
GenericParamDefKind::Lifetime(_) => false
841+
GenericParamDefKind::Type(_) => return true,
842+
GenericParamDefKind::Lifetime(_) => {}
843843
}
844-
}) {
845-
return true;
846844
}
847845
if let Some(parent_def_id) = self.parent {
848846
let parent = tcx.generics_of(parent_def_id);
@@ -858,7 +856,7 @@ impl<'a, 'gcx, 'tcx> Generics {
858856
-> &'tcx GenericParamDef
859857
{
860858
if let Some(index) = param.index.checked_sub(self.parent_count as u32) {
861-
let ref param = self.params[index as usize];
859+
let param = &self.params[index as usize];
862860
match param.kind {
863861
ty::GenericParamDefKind::Lifetime(_) => param,
864862
_ => bug!("expected region parameter, but found another generic parameter")
@@ -875,53 +873,7 @@ impl<'a, 'gcx, 'tcx> Generics {
875873
tcx: TyCtxt<'a, 'gcx, 'tcx>)
876874
-> &'tcx GenericParamDef {
877875
if let Some(index) = param.idx.checked_sub(self.parent_count as u32) {
878-
// non-Self type parameters are always offset by exactly
879-
// `self.regions.len()`. In the absence of a Self, this is obvious,
880-
// but even in the presence of a `Self` we just have to "compensate"
881-
// for the regions:
882-
//
883-
// Without a `Self` (or in a nested generics that doesn't have
884-
// a `Self` in itself, even through it parent does), for example
885-
// for `fn foo<'a, T1, T2>()`, the situation is:
886-
// Substs:
887-
// 0 1 2
888-
// 'a T1 T2
889-
// generics.types:
890-
// 0 1
891-
// T1 T2
892-
//
893-
// And with a `Self`, for example for `trait Foo<'a, 'b, T1, T2>`, the
894-
// situation is:
895-
// Substs:
896-
// 0 1 2 3 4
897-
// Self 'a 'b T1 T2
898-
// generics.types:
899-
// 0 1 2
900-
// Self T1 T2
901-
//
902-
// And it can be seen that in both cases, to move from a substs
903-
// offset to a generics offset you just have to offset by the
904-
// number of regions.
905-
let type_param_offset = self.param_counts().lifetimes;
906-
907-
let has_self = self.has_self && self.parent.is_none();
908-
let is_separated_self = type_param_offset != 0 && index == 0 && has_self;
909-
910-
if let Some(_) = (index as usize).checked_sub(type_param_offset) {
911-
assert!(!is_separated_self, "found a Self after type_param_offset");
912-
let ref param = self.params[index as usize];
913-
match param.kind {
914-
ty::GenericParamDefKind::Type(_) => param,
915-
_ => bug!("expected type parameter, but found another generic parameter")
916-
}
917-
} else {
918-
assert!(is_separated_self, "non-Self param before type_param_offset");
919-
let ref param = self.params[type_param_offset];
920-
match param.kind {
921-
ty::GenericParamDefKind::Type(_) => param,
922-
_ => bug!("expected type parameter, but found another generic parameter")
923-
}
924-
}
876+
&self.params[index as usize]
925877
} else {
926878
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
927879
.type_param(param, tcx)

src/librustc/ty/sty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ struct SplitGeneratorSubsts<'tcx> {
366366
impl<'tcx> GeneratorSubsts<'tcx> {
367367
fn split(self, def_id: DefId, tcx: TyCtxt<'_, '_, '_>) -> SplitGeneratorSubsts<'tcx> {
368368
let generics = tcx.generics_of(def_id);
369-
let parent_len = generics.parent_count();
369+
let parent_len = generics.parent_count;
370370
SplitGeneratorSubsts {
371371
yield_ty: self.substs.type_at(parent_len),
372372
return_ty: self.substs.type_at(parent_len + 1),

src/librustc/util/ppaux.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,10 @@ impl PrintContext {
351351
let zipped = iter::once((last_ty, types.next().unwrap()))
352352
.chain(type_params.zip(types));
353353
for ((def_id, has_default), actual) in zipped {
354-
if !has_default ||
355-
tcx.type_of(def_id).subst(tcx, substs) != actual {
354+
if !has_default {
355+
break;
356+
}
357+
if tcx.type_of(def_id).subst(tcx, substs) != actual {
356358
break;
357359
}
358360
num_supplied_defaults += 1;
@@ -604,7 +606,7 @@ define_print! {
604606
impl fmt::Debug for ty::GenericParamDef {
605607
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
606608
let type_name = match self.kind {
607-
ty::GenericParamDefKind::Lifetime(_) => "Region",
609+
ty::GenericParamDefKind::Lifetime(_) => "Lifetime",
608610
ty::GenericParamDefKind::Type(_) => "Type",
609611
};
610612
write!(f, "{}({}, {:?}, {})",

0 commit comments

Comments
 (0)