Skip to content

Commit fba1fe2

Browse files
committed
Remove hir::GenericParam::is_*_param
1 parent c818a1d commit fba1fe2

File tree

4 files changed

+106
-102
lines changed

4 files changed

+106
-102
lines changed

src/librustc/hir/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -497,20 +497,6 @@ pub struct GenericParam {
497497
}
498498

499499
impl GenericParam {
500-
pub fn is_lifetime_param(&self) -> bool {
501-
match self.kind {
502-
GenericParamKind::Lifetime { .. } => true,
503-
_ => false,
504-
}
505-
}
506-
507-
pub fn is_type_param(&self) -> bool {
508-
match self.kind {
509-
GenericParamKind::Type { .. } => true,
510-
_ => false,
511-
}
512-
}
513-
514500
pub fn name(&self) -> Name {
515501
match self.kind {
516502
GenericParamKind::Lifetime { name, .. } => name.name(),

src/librustc/middle/reachable.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use util::nodemap::{NodeSet, FxHashSet};
2929
use rustc_target::spec::abi::Abi;
3030
use syntax::ast;
3131
use syntax::attr;
32-
use hir;
32+
use hir::{self, GenericParamKind};
3333
use hir::def_id::LOCAL_CRATE;
3434
use hir::intravisit::{Visitor, NestedVisitorMap};
3535
use hir::itemlikevisit::ItemLikeVisitor;
@@ -38,7 +38,13 @@ use hir::intravisit;
3838
// Returns true if the given set of generics implies that the item it's
3939
// associated with must be inlined.
4040
fn generics_require_inlining(generics: &hir::Generics) -> bool {
41-
generics.params.iter().any(|param| param.is_type_param())
41+
for param in &generics.params {
42+
match param.kind {
43+
GenericParamKind::Lifetime { .. } => {}
44+
GenericParamKind::Type { .. } => return true,
45+
}
46+
}
47+
false
4248
}
4349

4450
// Returns true if the given item must be inlined because it may be

src/librustc/middle/resolve_lifetime.rs

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -532,21 +532,21 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
532532
} else {
533533
0
534534
};
535-
let mut next_early_index = index;
535+
let mut type_count = 0;
536536
let lifetimes = generics.params.iter().filter_map(|param| {
537537
match param.kind {
538538
GenericParamKind::Lifetime { .. } => {
539539
Some(Region::early(&self.tcx.hir, &mut index, param))
540540
}
541541
GenericParamKind::Type { .. } => {
542-
next_early_index += 1;
542+
type_count += 1;
543543
None
544544
}
545545
}
546546
}).collect();
547547
let scope = Scope::Binder {
548548
lifetimes,
549-
next_early_index,
549+
next_early_index: index + type_count,
550550
abstract_type_parent: true,
551551
track_lifetime_uses,
552552
s: ROOT_SCOPE,
@@ -698,7 +698,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
698698

699699
let mut elision = None;
700700
let mut lifetimes = FxHashMap();
701-
let mut next_early_index = index;
701+
let mut type_count = 0;
702702
for param in &generics.params {
703703
match param.kind {
704704
GenericParamKind::Lifetime { .. } => {
@@ -712,10 +712,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
712712
}
713713
}
714714
GenericParamKind::Type { .. } => {
715-
next_early_index += 1;
715+
type_count += 1;
716716
}
717717
}
718718
}
719+
let next_early_index = index + type_count;
719720

720721
if let Some(elision_region) = elision {
721722
let scope = Scope::Elision {
@@ -773,7 +774,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
773774
let generics = &trait_item.generics;
774775
let mut index = self.next_early_index();
775776
debug!("visit_ty: index = {}", index);
776-
let mut next_early_index = index;
777+
let mut type_count = 0;
777778
let lifetimes = generics.params
778779
.iter()
779780
.filter_map(|param| {
@@ -782,7 +783,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
782783
Some(Region::early(&self.tcx.hir, &mut index, param))
783784
}
784785
GenericParamKind::Type { .. } => {
785-
next_early_index += 1;
786+
type_count += 1;
786787
None
787788
}
788789
}
@@ -791,7 +792,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
791792

792793
let scope = Scope::Binder {
793794
lifetimes,
794-
next_early_index,
795+
next_early_index: index + type_count,
795796
s: self.scope,
796797
track_lifetime_uses: true,
797798
abstract_type_parent: true,
@@ -896,7 +897,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
896897
}
897898

898899
fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
899-
900900
check_mixed_explicit_and_in_band_defs(
901901
self.tcx,
902902
&generics.params.iter().filter_map(|param| {
@@ -925,21 +925,21 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
925925
ref bound_generic_params,
926926
..
927927
}) => {
928-
if bound_generic_params.iter().any(|p| p.is_lifetime_param()) {
928+
let lifetimes: FxHashMap<_, _> = bound_generic_params.iter()
929+
.filter_map(|param| {
930+
match param.kind {
931+
GenericParamKind::Lifetime { .. } => {
932+
Some(Region::late(&self.tcx.hir, param))
933+
}
934+
_ => None,
935+
}
936+
})
937+
.collect();
938+
if !lifetimes.is_empty() {
929939
self.trait_ref_hack = true;
930940
let next_early_index = self.next_early_index();
931941
let scope = Scope::Binder {
932-
lifetimes: bound_generic_params
933-
.iter()
934-
.filter_map(|param| {
935-
match param.kind {
936-
GenericParamKind::Lifetime { .. } => {
937-
Some(Region::late(&self.tcx.hir, param))
938-
}
939-
_ => None,
940-
}
941-
})
942-
.collect(),
942+
lifetimes,
943943
s: self.scope,
944944
next_early_index,
945945
track_lifetime_uses: true,
@@ -990,7 +990,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
990990
|| trait_ref
991991
.bound_generic_params
992992
.iter()
993-
.any(|p| p.is_lifetime_param())
993+
.any(|param| {
994+
match param.kind {
995+
GenericParamKind::Lifetime { .. } => true,
996+
_ => false,
997+
}
998+
})
994999
{
9951000
if self.trait_ref_hack {
9961001
span_err!(
@@ -1259,10 +1264,15 @@ fn compute_object_lifetime_defaults(
12591264
let mut j = 0;
12601265
generics.params.iter().find(|param| {
12611266
match param.kind {
1262-
GenericParamKind::Lifetime { .. } => j += 1,
1267+
GenericParamKind::Lifetime { .. } => {
1268+
if i == j {
1269+
return true;
1270+
}
1271+
j += 1;
1272+
}
12631273
_ => {}
12641274
}
1265-
i == j
1275+
false
12661276
}).unwrap()
12671277
.name()
12681278
.to_string()
@@ -1530,25 +1540,23 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
15301540
}
15311541
}
15321542

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
1543+
let mut type_count = 0;
1544+
let lifetimes = generics.params.iter().filter_map(|param| {
1545+
match param.kind {
1546+
GenericParamKind::Lifetime { .. } => {
1547+
if self.map.late_bound.contains(&param.id) {
1548+
Some(Region::late(&self.tcx.hir, param))
1549+
} else {
1550+
Some(Region::early(&self.tcx.hir, &mut index, param))
15481551
}
15491552
}
1550-
})
1551-
.collect();
1553+
GenericParamKind::Type { .. } => {
1554+
type_count += 1;
1555+
None
1556+
}
1557+
}
1558+
}).collect();
1559+
let next_early_index = index + type_count;
15521560

15531561
let scope = Scope::Binder {
15541562
lifetimes,

src/librustc_lint/types.rs

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -810,51 +810,55 @@ impl LintPass for VariantSizeDifferences {
810810

811811
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VariantSizeDifferences {
812812
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
813-
if let hir::ItemEnum(ref enum_definition, ref gens) = it.node {
814-
if gens.params.iter().all(|param| param.is_lifetime_param()) {
815-
// sizes only make sense for non-generic types
816-
let item_def_id = cx.tcx.hir.local_def_id(it.id);
817-
let t = cx.tcx.type_of(item_def_id);
818-
let ty = cx.tcx.erase_regions(&t);
819-
let layout = cx.layout_of(ty).unwrap_or_else(|e| {
820-
bug!("failed to get layout for `{}`: {}", t, e)
821-
});
822-
823-
if let layout::Variants::Tagged { ref variants, ref tag, .. } = layout.variants {
824-
let discr_size = tag.value.size(cx.tcx).bytes();
825-
826-
debug!("enum `{}` is {} bytes large with layout:\n{:#?}",
827-
t, layout.size.bytes(), layout);
828-
829-
let (largest, slargest, largest_index) = enum_definition.variants
830-
.iter()
831-
.zip(variants)
832-
.map(|(variant, variant_layout)| {
833-
// Subtract the size of the enum discriminant
834-
let bytes = variant_layout.size.bytes()
835-
.saturating_sub(discr_size);
836-
837-
debug!("- variant `{}` is {} bytes large", variant.node.name, bytes);
838-
bytes
839-
})
840-
.enumerate()
841-
.fold((0, 0, 0), |(l, s, li), (idx, size)| if size > l {
842-
(size, l, idx)
843-
} else if size > s {
844-
(l, size, li)
845-
} else {
846-
(l, s, li)
847-
});
848-
849-
// we only warn if the largest variant is at least thrice as large as
850-
// the second-largest.
851-
if largest > slargest * 3 && slargest > 0 {
852-
cx.span_lint(VARIANT_SIZE_DIFFERENCES,
853-
enum_definition.variants[largest_index].span,
854-
&format!("enum variant is more than three times larger \
855-
({} bytes) than the next largest",
856-
largest));
857-
}
813+
if let hir::ItemEnum(ref enum_definition, ref generics) = it.node {
814+
for param in &generics.params {
815+
match param.kind {
816+
hir::GenericParamKind::Lifetime { .. } => {},
817+
hir::GenericParamKind::Type { .. } => return,
818+
}
819+
}
820+
// Sizes only make sense for non-generic types.
821+
let item_def_id = cx.tcx.hir.local_def_id(it.id);
822+
let t = cx.tcx.type_of(item_def_id);
823+
let ty = cx.tcx.erase_regions(&t);
824+
let layout = cx.layout_of(ty).unwrap_or_else(|e| {
825+
bug!("failed to get layout for `{}`: {}", t, e)
826+
});
827+
828+
if let layout::Variants::Tagged { ref variants, ref tag, .. } = layout.variants {
829+
let discr_size = tag.value.size(cx.tcx).bytes();
830+
831+
debug!("enum `{}` is {} bytes large with layout:\n{:#?}",
832+
t, layout.size.bytes(), layout);
833+
834+
let (largest, slargest, largest_index) = enum_definition.variants
835+
.iter()
836+
.zip(variants)
837+
.map(|(variant, variant_layout)| {
838+
// Subtract the size of the enum discriminant.
839+
let bytes = variant_layout.size.bytes()
840+
.saturating_sub(discr_size);
841+
842+
debug!("- variant `{}` is {} bytes large", variant.node.name, bytes);
843+
bytes
844+
})
845+
.enumerate()
846+
.fold((0, 0, 0), |(l, s, li), (idx, size)| if size > l {
847+
(size, l, idx)
848+
} else if size > s {
849+
(l, size, li)
850+
} else {
851+
(l, s, li)
852+
});
853+
854+
// We only warn if the largest variant is at least thrice as large as
855+
// the second-largest.
856+
if largest > slargest * 3 && slargest > 0 {
857+
cx.span_lint(VARIANT_SIZE_DIFFERENCES,
858+
enum_definition.variants[largest_index].span,
859+
&format!("enum variant is more than three times larger \
860+
({} bytes) than the next largest",
861+
largest));
858862
}
859863
}
860864
}

0 commit comments

Comments
 (0)