Skip to content

Commit 8bccfe7

Browse files
committed
Refactor counting methods
1 parent 3bcb006 commit 8bccfe7

File tree

8 files changed

+29
-36
lines changed

8 files changed

+29
-36
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,8 @@ pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v TyPar
745745
pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
746746
visitor.visit_id(param.id);
747747
match param.kind {
748-
GenericParamKind::Lifetime { ref bounds, ref lifetime_deprecated, .. } => {
749-
match lifetime_deprecated.name {
748+
GenericParamKind::Lifetime { ref bounds, ref lifetime, .. } => {
749+
match lifetime.name {
750750
LifetimeName::Name(name) => {
751751
visitor.visit_name(param.span, name);
752752
}

src/librustc/hir/lowering.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ impl<'a> LoweringContext<'a> {
705705
name: hir_name,
706706
bounds: vec![].into(),
707707
in_band: true,
708-
lifetime_deprecated: hir::Lifetime {
708+
lifetime: hir::Lifetime {
709709
id: def_node_id,
710710
span,
711711
name: hir_name,
@@ -1424,7 +1424,7 @@ impl<'a> LoweringContext<'a> {
14241424
name,
14251425
bounds: vec![].into(),
14261426
in_band: false,
1427-
lifetime_deprecated: hir::Lifetime {
1427+
lifetime: hir::Lifetime {
14281428
id: def_node_id,
14291429
span: lifetime.span,
14301430
name,
@@ -1947,7 +1947,7 @@ impl<'a> LoweringContext<'a> {
19471947
itctx: ImplTraitContext)
19481948
-> hir::GenericParam {
19491949
match param.kind {
1950-
GenericParamKind::Lifetime { ref bounds, ref lifetime, .. } => {
1950+
GenericParamKind::Lifetime { ref bounds, ref lifetime } => {
19511951
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
19521952
self.is_collecting_in_band_lifetimes = false;
19531953

@@ -1960,7 +1960,7 @@ impl<'a> LoweringContext<'a> {
19601960
name: lifetime.name,
19611961
bounds: bounds.iter().map(|lt| self.lower_lifetime(lt)).collect(),
19621962
in_band: false,
1963-
lifetime_deprecated: lifetime,
1963+
lifetime,
19641964
}
19651965
};
19661966

src/librustc/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ pub enum GenericParamKind {
462462
// `fn foo(x: &'a u8) -> &'a u8 { x }`
463463
in_band: bool,
464464
// We keep a `Lifetime` around for now just so we can `visit_lifetime`.
465-
lifetime_deprecated: Lifetime,
465+
lifetime: Lifetime,
466466
},
467467
Type {
468468
name: Name,

src/librustc/ich/impls_hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::GenericParamKind {
208208
mem::discriminant(self).hash_stable(hcx, hasher);
209209
match self {
210210
hir::GenericParamKind::Lifetime { name, ref bounds, in_band,
211-
ref lifetime_deprecated } => {
211+
ref lifetime } => {
212212
name.hash_stable(hcx, hasher);
213213
bounds.hash_stable(hcx, hasher);
214214
in_band.hash_stable(hcx, hasher);
215-
lifetime_deprecated.hash_stable(hcx, hasher);
215+
lifetime.hash_stable(hcx, hasher);
216216
}
217217
hir::GenericParamKind::Type { name, ref bounds, ref default, synthetic, attrs } => {
218218
name.hash_stable(hcx, hasher);

src/librustc/middle/resolve_lifetime.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,20 +1220,17 @@ fn compute_object_lifetime_defaults(
12201220
.map(|set| match *set {
12211221
Set1::Empty => "BaseDefault".to_string(),
12221222
Set1::One(Region::Static) => "'static".to_string(),
1223-
Set1::One(Region::EarlyBound(i, _, _)) => {
1224-
let mut j = 0;
1225-
generics.params.iter().find(|param| match param.kind {
1223+
Set1::One(Region::EarlyBound(mut i, _, _)) => {
1224+
generics.params.iter().find_map(|param| match param.kind {
12261225
GenericParamKind::Lifetime { .. } => {
1227-
if i == j {
1228-
return true;
1226+
if i == 0 {
1227+
return Some(param.name().to_string());
12291228
}
1230-
j += 1;
1231-
false
1229+
i -= 1;
1230+
None
12321231
}
1233-
_ => false,
1232+
_ => None,
12341233
}).unwrap()
1235-
.name()
1236-
.to_string()
12371234
}
12381235
Set1::One(_) => bug!(),
12391236
Set1::Many => "Ambiguous".to_string(),

src/librustc_typeck/astconv.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,14 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
274274
let substs = Substs::for_item(tcx, def_id, |param, substs| {
275275
match param.kind {
276276
GenericParamDefKind::Lifetime => {
277-
let i = param.index as usize - own_self;
278-
let mut j = 0;
277+
let mut i = param.index as usize - own_self;
279278
for arg in &generic_args.args {
280279
match arg {
281280
GenericArg::Lifetime(lt) => {
282-
if i == j {
281+
if i == 0 {
283282
return self.ast_region_to_region(lt, Some(param)).into();
284283
}
285-
j += 1;
284+
i -= 1;
286285
}
287286
_ => {}
288287
}
@@ -297,17 +296,16 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
297296
return ty.into();
298297
}
299298

300-
let i = i - (lt_accepted + own_self);
299+
let mut i = i - (lt_accepted + own_self);
301300
if i < ty_provided {
302301
// A provided type parameter.
303-
let mut j = 0;
304302
for arg in &generic_args.args {
305303
match arg {
306304
GenericArg::Type(ty) => {
307-
if i == j {
305+
if i == 0 {
308306
return self.ast_ty_to_ty(ty).into();
309307
}
310-
j += 1;
308+
i -= 1;
311309
}
312310
_ => {}
313311
}

src/librustc_typeck/check/method/confirm.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,21 +325,20 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
325325
let provided = &segment.args;
326326
let own_counts = method_generics.own_counts();
327327
Substs::for_item(self.tcx, pick.item.def_id, |param, _| {
328-
let i = param.index as usize;
328+
let mut i = param.index as usize;
329329
if i < parent_substs.len() {
330330
parent_substs[i]
331331
} else {
332332
match param.kind {
333333
GenericParamDefKind::Lifetime => {
334334
if let Some(lifetime) = provided.as_ref().and_then(|data| {
335-
let mut j = 0;
336335
for arg in &data.args {
337336
match arg {
338337
GenericArg::Lifetime(lt) => {
339-
if i - parent_substs.len() == j {
338+
if i == parent_substs.len() {
340339
return Some(lt);
341340
}
342-
j += 1;
341+
i -= 1;
343342
}
344343
_ => {}
345344
}
@@ -352,14 +351,13 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
352351
}
353352
GenericParamDefKind::Type {..} => {
354353
if let Some(ast_ty) = provided.as_ref().and_then(|data| {
355-
let mut j = 0;
356354
for arg in &data.args {
357355
match arg {
358356
GenericArg::Type(ty) => {
359-
if i - parent_substs.len() - own_counts.lifetimes == j {
357+
if i == parent_substs.len() + own_counts.lifetimes {
360358
return Some(ty);
361359
}
362-
j += 1;
360+
i -= 1;
363361
}
364362
_ => {}
365363
}

src/libsyntax/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,12 @@ pub fn walk_ty_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a TyPar
492492

493493
pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
494494
match param.kind {
495-
GenericParamKind::Lifetime { ref bounds, ref lifetime, .. } => {
495+
GenericParamKind::Lifetime { ref bounds, ref lifetime } => {
496496
visitor.visit_ident(param.ident);
497497
walk_list!(visitor, visit_lifetime, bounds);
498498
walk_list!(visitor, visit_attribute, param.attrs.iter());
499499
}
500-
GenericParamKind::Type { ref bounds, ref default, .. } => {
500+
GenericParamKind::Type { ref bounds, ref default } => {
501501
visitor.visit_ident(t.ident);
502502
walk_list!(visitor, visit_ty_param_bound, bounds);
503503
walk_list!(visitor, visit_ty, default);

0 commit comments

Comments
 (0)