Skip to content

Commit f8cacca

Browse files
committed
rustc/ty: simplify some patterns
1 parent 187bcb9 commit f8cacca

File tree

9 files changed

+66
-95
lines changed

9 files changed

+66
-95
lines changed

src/librustc/ty/fold.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,10 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
780780
}
781781

782782
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
783-
match *r {
784-
ty::ReLateBound(debruijn, br) if debruijn == self.current_index => {
783+
if let ty::ReLateBound(debruijn, br) = *r {
784+
if debruijn == self.current_index {
785785
self.regions.insert(br);
786786
}
787-
_ => { }
788787
}
789788
false
790789
}

src/librustc/ty/mod.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -861,24 +861,22 @@ pub struct GenericParamDef {
861861

862862
impl GenericParamDef {
863863
pub fn to_early_bound_region_data(&self) -> ty::EarlyBoundRegion {
864-
match self.kind {
865-
GenericParamDefKind::Lifetime => {
866-
ty::EarlyBoundRegion {
867-
def_id: self.def_id,
868-
index: self.index,
869-
name: self.name,
870-
}
864+
if let GenericParamDefKind::Lifetime = self.kind {
865+
ty::EarlyBoundRegion {
866+
def_id: self.def_id,
867+
index: self.index,
868+
name: self.name,
871869
}
872-
_ => bug!("cannot convert a non-lifetime parameter def to an early bound region")
870+
} else {
871+
bug!("cannot convert a non-lifetime parameter def to an early bound region")
873872
}
874873
}
875874

876875
pub fn to_bound_region(&self) -> ty::BoundRegion {
877-
match self.kind {
878-
GenericParamDefKind::Lifetime => {
879-
self.to_early_bound_region_data().to_bound_region()
880-
}
881-
_ => bug!("cannot convert a non-lifetime parameter def to an early bound region")
876+
if let GenericParamDefKind::Lifetime = self.kind {
877+
self.to_early_bound_region_data().to_bound_region()
878+
} else {
879+
bug!("cannot convert a non-lifetime parameter def to an early bound region")
882880
}
883881
}
884882
}

src/librustc/ty/relate.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
332332
-> RelateResult<'tcx, GeneratorWitness<'tcx>>
333333
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
334334
{
335-
assert!(a.0.len() == b.0.len());
335+
assert_eq!(a.0.len(), b.0.len());
336336
let tcx = relation.tcx();
337337
let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
338338
Ok(GeneratorWitness(types))
@@ -479,27 +479,24 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
479479
ConstValue::Unevaluated(def_id, substs) => {
480480
// FIXME(eddyb) get the right param_env.
481481
let param_env = ty::ParamEnv::empty();
482-
match tcx.lift_to_global(&substs) {
483-
Some(substs) => {
484-
let instance = ty::Instance::resolve(
485-
tcx.global_tcx(),
486-
param_env,
487-
def_id,
488-
substs,
489-
);
490-
if let Some(instance) = instance {
491-
let cid = GlobalId {
492-
instance,
493-
promoted: None
494-
};
495-
if let Some(s) = tcx.const_eval(param_env.and(cid))
496-
.ok()
497-
.map(|c| c.unwrap_usize(tcx)) {
498-
return Ok(s)
499-
}
482+
if let Some(substs) = tcx.lift_to_global(&substs) {
483+
let instance = ty::Instance::resolve(
484+
tcx.global_tcx(),
485+
param_env,
486+
def_id,
487+
substs,
488+
);
489+
if let Some(instance) = instance {
490+
let cid = GlobalId {
491+
instance,
492+
promoted: None
493+
};
494+
if let Some(s) = tcx.const_eval(param_env.and(cid))
495+
.ok()
496+
.map(|c| c.unwrap_usize(tcx)) {
497+
return Ok(s)
500498
}
501-
},
502-
None => {}
499+
}
503500
}
504501
tcx.sess.delay_span_bug(tcx.def_span(def_id),
505502
"array length could not be evaluated");

src/librustc/ty/structural_impls.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -816,22 +816,16 @@ impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
816816
use ty::InstanceDef::*;
817817
self.substs.visit_with(visitor) ||
818818
match self.def {
819-
Item(did) => did.visit_with(visitor),
820-
Intrinsic(did) => did.visit_with(visitor),
821-
FnPtrShim(did, ty) => {
822-
did.visit_with(visitor) ||
823-
ty.visit_with(visitor)
819+
Item(did) | Intrinsic(did) | Virtual(did, _) => {
820+
did.visit_with(visitor)
824821
},
825-
Virtual(did, _) => did.visit_with(visitor),
826-
ClosureOnceShim { call_once } => call_once.visit_with(visitor),
827-
DropGlue(did, ty) => {
828-
did.visit_with(visitor) ||
829-
ty.visit_with(visitor)
822+
FnPtrShim(did, ty) | CloneShim(did, ty) => {
823+
did.visit_with(visitor) || ty.visit_with(visitor)
830824
},
831-
CloneShim(did, ty) => {
832-
did.visit_with(visitor) ||
833-
ty.visit_with(visitor)
825+
DropGlue(did, ty) => {
826+
did.visit_with(visitor) || ty.visit_with(visitor)
834827
},
828+
ClosureOnceShim { call_once } => call_once.visit_with(visitor),
835829
}
836830
}
837831
}

src/librustc/ty/sty.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -987,11 +987,7 @@ impl<'a, 'gcx, 'tcx> ParamTy {
987987
// FIXME(#50125): Ignoring `Self` with `idx != 0` might lead to weird behavior elsewhere,
988988
// but this should only be possible when using `-Z continue-parse-after-error` like
989989
// `compile-fail/issue-36638.rs`.
990-
if self.name == keywords::SelfType.name().as_str() && self.idx == 0 {
991-
true
992-
} else {
993-
false
994-
}
990+
self.name == keywords::SelfType.name().as_str() && self.idx == 0
995991
}
996992
}
997993

@@ -2021,18 +2017,14 @@ impl<'tcx> Const<'tcx> {
20212017
tcx: TyCtxt<'_, '_, '_>,
20222018
ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
20232019
) -> u128 {
2024-
match self.assert_bits(tcx, ty) {
2025-
Some(val) => val,
2026-
None => bug!("expected bits of {}, got {:#?}", ty.value, self),
2027-
}
2020+
self.assert_bits(tcx, ty).unwrap_or_else(||
2021+
bug!("expected bits of {}, got {:#?}", ty.value, self))
20282022
}
20292023

20302024
#[inline]
20312025
pub fn unwrap_usize(&self, tcx: TyCtxt<'_, '_, '_>) -> u64 {
2032-
match self.assert_usize(tcx) {
2033-
Some(val) => val,
2034-
None => bug!("expected constant usize, got {:#?}", self),
2035-
}
2026+
self.assert_usize(tcx).unwrap_or_else(||
2027+
bug!("expected constant usize, got {:#?}", self))
20362028
}
20372029
}
20382030

src/librustc/ty/subst.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,9 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
205205
where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Kind<'tcx>
206206
{
207207
Substs::for_item(tcx, def_id, |param, substs| {
208-
match self.get(param.index as usize) {
209-
Some(&kind) => kind,
210-
None => mk_kind(param, substs),
211-
}
208+
self.get(param.index as usize)
209+
.cloned()
210+
.unwrap_or_else(|| mk_kind(param, substs))
212211
})
213212
}
214213

src/librustc/ty/trait_def.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
138138
}
139139
}
140140
} else {
141-
for v in impls.non_blanket_impls.values() {
142-
for &impl_def_id in v {
143-
f(impl_def_id);
144-
}
141+
for &impl_def_id in impls.non_blanket_impls.values().flatten() {
142+
f(impl_def_id);
145143
}
146144
}
147145
}

src/librustc/ty/util.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,13 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
257257

258258
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
259259
pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
260-
match ty.sty {
261-
ty::Adt(def, substs) => {
262-
for field in def.all_fields() {
263-
let field_ty = field.ty(self, substs);
264-
if let Error = field_ty.sty {
265-
return true;
266-
}
260+
if let ty::Adt(def, substs) = ty.sty {
261+
for field in def.all_fields() {
262+
let field_ty = field.ty(self, substs);
263+
if let Error = field_ty.sty {
264+
return true;
267265
}
268266
}
269-
_ => (),
270267
}
271268
false
272269
}
@@ -421,7 +418,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
421418
let ty = self.type_of(adt_did);
422419
self.for_each_relevant_impl(drop_trait, ty, |impl_did| {
423420
if let Some(item) = self.associated_items(impl_did).next() {
424-
if let Ok(()) = validate(self, impl_did) {
421+
if validate(self, impl_did).is_ok() {
425422
dtor_did = Some(item.def_id);
426423
}
427424
}
@@ -906,20 +903,17 @@ fn needs_drop_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
906903
let (param_env, ty) = query.into_parts();
907904

908905
let needs_drop = |ty: Ty<'tcx>| -> bool {
909-
match tcx.try_needs_drop_raw(DUMMY_SP, param_env.and(ty)) {
910-
Ok(v) => v,
911-
Err(mut bug) => {
912-
// Cycles should be reported as an error by `check_representable`.
913-
//
914-
// Consider the type as not needing drop in the meanwhile to
915-
// avoid further errors.
916-
//
917-
// In case we forgot to emit a bug elsewhere, delay our
918-
// diagnostic to get emitted as a compiler bug.
919-
bug.delay_as_bug();
920-
false
921-
}
922-
}
906+
tcx.try_needs_drop_raw(DUMMY_SP, param_env.and(ty)).unwrap_or_else(|mut bug| {
907+
// Cycles should be reported as an error by `check_representable`.
908+
//
909+
// Consider the type as not needing drop in the meanwhile to
910+
// avoid further errors.
911+
//
912+
// In case we forgot to emit a bug elsewhere, delay our
913+
// diagnostic to get emitted as a compiler bug.
914+
bug.delay_as_bug();
915+
false
916+
})
923917
};
924918

925919
assert!(!ty.needs_infer());

src/librustc/ty/walk.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'tcx> Iterator for TypeWalker<'tcx> {
5454
debug!("next(): stack={:?}", self.stack);
5555
match self.stack.pop() {
5656
None => {
57-
return None;
57+
None
5858
}
5959
Some(ty) => {
6060
self.last_subtree = self.stack.len();

0 commit comments

Comments
 (0)