Skip to content

Commit c818a1d

Browse files
committed
Remove specific parameter iterators from hir::Generics
1 parent 82dba3d commit c818a1d

File tree

9 files changed

+214
-136
lines changed

9 files changed

+214
-136
lines changed

src/librustc/hir/lowering.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -379,22 +379,29 @@ impl<'a> LoweringContext<'a> {
379379
let item_lifetimes = match self.lctx.items.get(&item.id).unwrap().node {
380380
hir::Item_::ItemImpl(_, _, _, ref generics, ..)
381381
| hir::Item_::ItemTrait(_, _, ref generics, ..) => {
382-
generics.lifetimes().cloned().collect::<Vec<_>>()
382+
generics.params
383+
.iter()
384+
.filter_map(|param| match param.kind {
385+
hir::GenericParamKind::Lifetime { .. } => {
386+
Some(param.clone())
387+
}
388+
_ => None,
389+
})
390+
.collect::<Vec<_>>()
383391
}
384392
_ => Vec::new(),
385393
};
386394

387-
self.lctx
388-
.with_parent_impl_lifetime_defs(&item_lifetimes, |this| {
389-
let this = &mut ItemLowerer { lctx: this };
390-
if let ItemKind::Impl(_, _, _, _, ref opt_trait_ref, _, _) = item.node {
391-
this.with_trait_impl_ref(opt_trait_ref, |this| {
392-
visit::walk_item(this, item)
393-
});
394-
} else {
395-
visit::walk_item(this, item);
396-
}
397-
});
395+
self.lctx.with_parent_impl_lifetime_defs(&item_lifetimes, |this| {
396+
let this = &mut ItemLowerer { lctx: this };
397+
if let ItemKind::Impl(_, _, _, _, ref opt_trait_ref, _, _) = item.node {
398+
this.with_trait_impl_ref(opt_trait_ref, |this| {
399+
visit::walk_item(this, item)
400+
});
401+
} else {
402+
visit::walk_item(this, item);
403+
}
404+
});
398405
}
399406
}
400407

src/librustc/hir/mod.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ impl GenericParam {
519519
}
520520
}
521521

522+
pub struct GenericParamCount {
523+
pub lifetimes: usize,
524+
pub types: usize,
525+
}
526+
522527
/// Represents lifetimes and type parameters attached to a declaration
523528
/// of a function, enum, trait, etc.
524529
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
@@ -540,40 +545,23 @@ impl Generics {
540545
}
541546
}
542547

543-
pub fn is_lt_parameterized(&self) -> bool {
544-
self.params.iter().any(|param| {
545-
match param.kind {
546-
GenericParamKind::Lifetime { .. } => true,
547-
_ => false,
548-
}
549-
})
550-
}
551-
552-
pub fn is_type_parameterized(&self) -> bool {
553-
self.params.iter().any(|param| {
554-
match param.kind {
555-
GenericParamKind::Type { .. } => true,
556-
_ => false,
557-
}
558-
})
559-
}
548+
pub fn own_counts(&self) -> GenericParamCount {
549+
// We could cache this as a property of `GenericParamCount`, but
550+
// the aim is to refactor this away entirely eventually and the
551+
// presence of this method will be a constant reminder.
552+
let mut own_counts = GenericParamCount {
553+
lifetimes: 0,
554+
types: 0,
555+
};
560556

561-
pub fn lifetimes<'a>(&'a self) -> impl DoubleEndedIterator<Item = &'a GenericParam> {
562-
self.params.iter().filter(|param| {
557+
for param in &self.params {
563558
match param.kind {
564-
GenericParamKind::Lifetime { .. } => true,
565-
_ => false,
566-
}
567-
})
568-
}
559+
GenericParamKind::Lifetime { .. } => own_counts.lifetimes += 1,
560+
GenericParamKind::Type { .. } => own_counts.types += 1,
561+
};
562+
}
569563

570-
pub fn ty_params<'a>(&'a self) -> impl DoubleEndedIterator<Item = &'a GenericParam> {
571-
self.params.iter().filter(|param| {
572-
match param.kind {
573-
GenericParamKind::Type { .. } => true,
574-
_ => false,
575-
}
576-
})
564+
own_counts
577565
}
578566
}
579567

src/librustc/middle/resolve_lifetime.rs

Lines changed: 97 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,18 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
532532
} else {
533533
0
534534
};
535-
let lifetimes = generics
536-
.lifetimes()
537-
.map(|def| Region::early(&self.tcx.hir, &mut index, def))
538-
.collect();
539-
let next_early_index = index + generics.ty_params().count() as u32;
535+
let mut next_early_index = index;
536+
let lifetimes = generics.params.iter().filter_map(|param| {
537+
match param.kind {
538+
GenericParamKind::Lifetime { .. } => {
539+
Some(Region::early(&self.tcx.hir, &mut index, param))
540+
}
541+
GenericParamKind::Type { .. } => {
542+
next_early_index += 1;
543+
None
544+
}
545+
}
546+
}).collect();
540547
let scope = Scope::Binder {
541548
lifetimes,
542549
next_early_index,
@@ -691,19 +698,25 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
691698

692699
let mut elision = None;
693700
let mut lifetimes = FxHashMap();
694-
for lt_def in generics.lifetimes() {
695-
let (lt_name, region) = Region::early(&self.tcx.hir, &mut index, &lt_def);
696-
if let hir::LifetimeName::Underscore = lt_name {
697-
// Pick the elided lifetime "definition" if one exists and use it to make
698-
// an elision scope.
699-
elision = Some(region);
700-
} else {
701-
lifetimes.insert(lt_name, region);
701+
let mut next_early_index = index;
702+
for param in &generics.params {
703+
match param.kind {
704+
GenericParamKind::Lifetime { .. } => {
705+
let (name, reg) = Region::early(&self.tcx.hir, &mut index, &param);
706+
if let hir::LifetimeName::Underscore = name {
707+
// Pick the elided lifetime "definition" if one exists
708+
// and use it to make an elision scope.
709+
elision = Some(reg);
710+
} else {
711+
lifetimes.insert(name, reg);
712+
}
713+
}
714+
GenericParamKind::Type { .. } => {
715+
next_early_index += 1;
716+
}
702717
}
703718
}
704719

705-
let next_early_index = index + generics.ty_params().count() as u32;
706-
707720
if let Some(elision_region) = elision {
708721
let scope = Scope::Elision {
709722
elide: Elide::Exact(elision_region),
@@ -760,12 +773,22 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
760773
let generics = &trait_item.generics;
761774
let mut index = self.next_early_index();
762775
debug!("visit_ty: index = {}", index);
763-
let lifetimes = generics
764-
.lifetimes()
765-
.map(|lt_def| Region::early(&self.tcx.hir, &mut index, lt_def))
776+
let mut next_early_index = index;
777+
let lifetimes = generics.params
778+
.iter()
779+
.filter_map(|param| {
780+
match param.kind {
781+
GenericParamKind::Lifetime { .. } => {
782+
Some(Region::early(&self.tcx.hir, &mut index, param))
783+
}
784+
GenericParamKind::Type { .. } => {
785+
next_early_index += 1;
786+
None
787+
}
788+
}
789+
})
766790
.collect();
767791

768-
let next_early_index = index + generics.ty_params().count() as u32;
769792
let scope = Scope::Binder {
770793
lifetimes,
771794
next_early_index,
@@ -806,13 +829,23 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
806829
Type(ref ty) => {
807830
let generics = &impl_item.generics;
808831
let mut index = self.next_early_index();
832+
let mut next_early_index = index;
809833
debug!("visit_ty: index = {}", index);
810-
let lifetimes = generics
811-
.lifetimes()
812-
.map(|lt_def| Region::early(&self.tcx.hir, &mut index, lt_def))
834+
let lifetimes = generics.params
835+
.iter()
836+
.filter_map(|param| {
837+
match param.kind {
838+
GenericParamKind::Lifetime { .. } => {
839+
Some(Region::early(&self.tcx.hir, &mut index, param))
840+
}
841+
GenericParamKind::Type { .. } => {
842+
next_early_index += 1;
843+
None
844+
}
845+
}
846+
})
813847
.collect();
814848

815-
let next_early_index = index + generics.ty_params().count() as u32;
816849
let scope = Scope::Binder {
817850
lifetimes,
818851
next_early_index,
@@ -863,9 +896,15 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
863896
}
864897

865898
fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
899+
866900
check_mixed_explicit_and_in_band_defs(
867901
self.tcx,
868-
&generics.lifetimes().cloned().collect::<Vec<_>>(),
902+
&generics.params.iter().filter_map(|param| {
903+
match param.kind {
904+
GenericParamKind::Lifetime { .. } => Some(param.clone()),
905+
_ => None,
906+
}
907+
}).collect::<Vec<_>>()
869908
);
870909
for param in &generics.params {
871910
match param.kind {
@@ -1216,12 +1255,18 @@ fn compute_object_lifetime_defaults(
12161255
.map(|set| match *set {
12171256
Set1::Empty => "BaseDefault".to_string(),
12181257
Set1::One(Region::Static) => "'static".to_string(),
1219-
Set1::One(Region::EarlyBound(i, _, _)) => generics
1220-
.lifetimes()
1221-
.nth(i as usize)
1222-
.unwrap()
1223-
.name()
1224-
.to_string(),
1258+
Set1::One(Region::EarlyBound(i, _, _)) => {
1259+
let mut j = 0;
1260+
generics.params.iter().find(|param| {
1261+
match param.kind {
1262+
GenericParamKind::Lifetime { .. } => j += 1,
1263+
_ => {}
1264+
}
1265+
i == j
1266+
}).unwrap()
1267+
.name()
1268+
.to_string()
1269+
}
12251270
Set1::One(_) => bug!(),
12261271
Set1::Many => "Ambiguous".to_string(),
12271272
})
@@ -1485,19 +1530,26 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14851530
}
14861531
}
14871532

1488-
let lifetimes = generics
1489-
.lifetimes()
1490-
.map(|param| {
1491-
if self.map.late_bound.contains(&param.id) {
1492-
Region::late(&self.tcx.hir, param)
1493-
} else {
1494-
Region::early(&self.tcx.hir, &mut index, param)
1533+
let mut next_early_index = index;
1534+
let lifetimes = generics.params
1535+
.iter()
1536+
.filter_map(|param| {
1537+
match param.kind {
1538+
GenericParamKind::Lifetime { .. } => {
1539+
if self.map.late_bound.contains(&param.id) {
1540+
Some(Region::late(&self.tcx.hir, param))
1541+
} else {
1542+
Some(Region::early(&self.tcx.hir, &mut index, param))
1543+
}
1544+
}
1545+
GenericParamKind::Type { .. } => {
1546+
next_early_index += 1;
1547+
None
1548+
}
14951549
}
14961550
})
14971551
.collect();
14981552

1499-
let next_early_index = index + generics.ty_params().count() as u32;
1500-
15011553
let scope = Scope::Binder {
15021554
lifetimes,
15031555
next_early_index,
@@ -2513,10 +2565,10 @@ fn insert_late_bound_lifetimes(
25132565
// - appear in the inputs
25142566
// - do not appear in the where-clauses
25152567
// - are not implicitly captured by `impl Trait`
2516-
for lifetime in generics.lifetimes() {
2517-
let name = match lifetime.kind {
2568+
for param in &generics.params {
2569+
let name = match param.kind {
25182570
GenericParamKind::Lifetime { name, .. } => name,
2519-
_ => bug!(),
2571+
_ => continue,
25202572
};
25212573

25222574
// appears in the where clauses? early-bound.
@@ -2533,10 +2585,10 @@ fn insert_late_bound_lifetimes(
25332585

25342586
debug!("insert_late_bound_lifetimes: lifetime {:?} with id {:?} is late-bound",
25352587
name,
2536-
lifetime.id);
2588+
param.id);
25372589

2538-
let inserted = map.late_bound.insert(lifetime.id);
2539-
assert!(inserted, "visited lifetime {:?} twice", lifetime.id);
2590+
let inserted = map.late_bound.insert(param.id);
2591+
assert!(inserted, "visited lifetime {:?} twice", param.id);
25402592
}
25412593

25422594
return;

src/librustc_lint/builtin.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,15 +1196,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
11961196
}
11971197
err.emit();
11981198
}
1199-
if generics.is_type_parameterized() {
1200-
let mut err = cx.struct_span_lint(NO_MANGLE_GENERIC_ITEMS,
1201-
it.span,
1202-
"functions generic over \
1203-
types must be mangled");
1204-
err.span_suggestion_short(no_mangle_attr.span,
1205-
"remove this attribute",
1206-
"".to_owned());
1207-
err.emit();
1199+
for param in &generics.params {
1200+
match param.kind {
1201+
GenericParamKind::Lifetime { .. } => {}
1202+
GenericParamKind::Type { .. } => {
1203+
let mut err = cx.struct_span_lint(NO_MANGLE_GENERIC_ITEMS,
1204+
it.span,
1205+
"functions generic over \
1206+
types must be mangled");
1207+
err.span_suggestion_short(no_mangle_attr.span,
1208+
"remove this attribute",
1209+
"".to_owned());
1210+
err.emit();
1211+
break;
1212+
}
1213+
}
12081214
}
12091215
}
12101216
}

src/librustc_metadata/encoder.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,9 +1236,14 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
12361236
}
12371237
hir::ItemConst(..) => self.encode_optimized_mir(def_id),
12381238
hir::ItemFn(_, _, constness, _, ref generics, _) => {
1239-
let has_tps = generics.ty_params().next().is_some();
1239+
let has_types = generics.params.iter().find(|param| {
1240+
match param.kind {
1241+
hir::GenericParamKind::Type { .. } => true,
1242+
_ => false,
1243+
}
1244+
}).is_some();
12401245
let needs_inline =
1241-
(has_tps || tcx.codegen_fn_attrs(def_id).requests_inline()) &&
1246+
(has_types || tcx.codegen_fn_attrs(def_id).requests_inline()) &&
12421247
!self.metadata_output_only();
12431248
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
12441249
if needs_inline || constness == hir::Constness::Const || always_encode_mir {

src/librustc_mir/monomorphize/collector.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,8 +1105,11 @@ fn create_mono_items_for_default_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11051105
ref generics,
11061106
..,
11071107
ref impl_item_refs) => {
1108-
if generics.is_type_parameterized() {
1109-
return
1108+
for param in &generics.params {
1109+
match param.kind {
1110+
hir::GenericParamKind::Lifetime { .. } => {}
1111+
hir::GenericParamKind::Type { .. } => return,
1112+
}
11101113
}
11111114

11121115
let impl_def_id = tcx.hir.local_def_id(item.id);

0 commit comments

Comments
 (0)