diff options
| author | ljedrz <ljedrz@gmail.com> | 2018-10-02 10:56:36 +0200 |
|---|---|---|
| committer | ljedrz <ljedrz@gmail.com> | 2018-10-03 09:04:02 +0200 |
| commit | f8caccacf49520d85ce790a83b9899f85bdd74c5 (patch) | |
| tree | 97c4778b1fb10ae48a45724dae06772b680372d3 | |
| parent | 187bcb9bd18069896761d350e264953ede1eea04 (diff) | |
| download | rust-f8caccacf49520d85ce790a83b9899f85bdd74c5.tar.gz rust-f8caccacf49520d85ce790a83b9899f85bdd74c5.zip | |
rustc/ty: simplify some patterns
| -rw-r--r-- | src/librustc/ty/fold.rs | 5 | ||||
| -rw-r--r-- | src/librustc/ty/mod.rs | 24 | ||||
| -rw-r--r-- | src/librustc/ty/relate.rs | 39 | ||||
| -rw-r--r-- | src/librustc/ty/structural_impls.rs | 20 | ||||
| -rw-r--r-- | src/librustc/ty/sty.rs | 18 | ||||
| -rw-r--r-- | src/librustc/ty/subst.rs | 7 | ||||
| -rw-r--r-- | src/librustc/ty/trait_def.rs | 6 | ||||
| -rw-r--r-- | src/librustc/ty/util.rs | 40 | ||||
| -rw-r--r-- | src/librustc/ty/walk.rs | 2 |
9 files changed, 66 insertions, 95 deletions
diff --git a/src/librustc/ty/fold.rs b/src/librustc/ty/fold.rs index 6bf493b496c..dd7099a4064 100644 --- a/src/librustc/ty/fold.rs +++ b/src/librustc/ty/fold.rs @@ -780,11 +780,10 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector { } fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool { - match *r { - ty::ReLateBound(debruijn, br) if debruijn == self.current_index => { + if let ty::ReLateBound(debruijn, br) = *r { + if debruijn == self.current_index { self.regions.insert(br); } - _ => { } } false } diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 2685d45e279..380f5a58665 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -861,24 +861,22 @@ pub struct GenericParamDef { impl GenericParamDef { pub fn to_early_bound_region_data(&self) -> ty::EarlyBoundRegion { - match self.kind { - GenericParamDefKind::Lifetime => { - ty::EarlyBoundRegion { - def_id: self.def_id, - index: self.index, - name: self.name, - } + if let GenericParamDefKind::Lifetime = self.kind { + ty::EarlyBoundRegion { + def_id: self.def_id, + index: self.index, + name: self.name, } - _ => bug!("cannot convert a non-lifetime parameter def to an early bound region") + } else { + bug!("cannot convert a non-lifetime parameter def to an early bound region") } } pub fn to_bound_region(&self) -> ty::BoundRegion { - match self.kind { - GenericParamDefKind::Lifetime => { - self.to_early_bound_region_data().to_bound_region() - } - _ => bug!("cannot convert a non-lifetime parameter def to an early bound region") + if let GenericParamDefKind::Lifetime = self.kind { + self.to_early_bound_region_data().to_bound_region() + } else { + bug!("cannot convert a non-lifetime parameter def to an early bound region") } } } diff --git a/src/librustc/ty/relate.rs b/src/librustc/ty/relate.rs index a88f5095f0a..082c1bd5fea 100644 --- a/src/librustc/ty/relate.rs +++ b/src/librustc/ty/relate.rs @@ -332,7 +332,7 @@ impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> { -> RelateResult<'tcx, GeneratorWitness<'tcx>> where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a { - assert!(a.0.len() == b.0.len()); + assert_eq!(a.0.len(), b.0.len()); let tcx = relation.tcx(); let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?; Ok(GeneratorWitness(types)) @@ -479,27 +479,24 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R, ConstValue::Unevaluated(def_id, substs) => { // FIXME(eddyb) get the right param_env. let param_env = ty::ParamEnv::empty(); - match tcx.lift_to_global(&substs) { - Some(substs) => { - let instance = ty::Instance::resolve( - tcx.global_tcx(), - param_env, - def_id, - substs, - ); - if let Some(instance) = instance { - let cid = GlobalId { - instance, - promoted: None - }; - if let Some(s) = tcx.const_eval(param_env.and(cid)) - .ok() - .map(|c| c.unwrap_usize(tcx)) { - return Ok(s) - } + if let Some(substs) = tcx.lift_to_global(&substs) { + let instance = ty::Instance::resolve( + tcx.global_tcx(), + param_env, + def_id, + substs, + ); + if let Some(instance) = instance { + let cid = GlobalId { + instance, + promoted: None + }; + if let Some(s) = tcx.const_eval(param_env.and(cid)) + .ok() + .map(|c| c.unwrap_usize(tcx)) { + return Ok(s) } - }, - None => {} + } } tcx.sess.delay_span_bug(tcx.def_span(def_id), "array length could not be evaluated"); diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index 7d13c8d9ece..729a05eb5d4 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -816,22 +816,16 @@ impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> { use ty::InstanceDef::*; self.substs.visit_with(visitor) || match self.def { - Item(did) => did.visit_with(visitor), - Intrinsic(did) => did.visit_with(visitor), - FnPtrShim(did, ty) => { - did.visit_with(visitor) || - ty.visit_with(visitor) + Item(did) | Intrinsic(did) | Virtual(did, _) => { + did.visit_with(visitor) }, - Virtual(did, _) => did.visit_with(visitor), - ClosureOnceShim { call_once } => call_once.visit_with(visitor), - DropGlue(did, ty) => { - did.visit_with(visitor) || - ty.visit_with(visitor) + FnPtrShim(did, ty) | CloneShim(did, ty) => { + did.visit_with(visitor) || ty.visit_with(visitor) }, - CloneShim(did, ty) => { - did.visit_with(visitor) || - ty.visit_with(visitor) + DropGlue(did, ty) => { + did.visit_with(visitor) || ty.visit_with(visitor) }, + ClosureOnceShim { call_once } => call_once.visit_with(visitor), } } } diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 6c40dd89239..bc5186b08b8 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -987,11 +987,7 @@ impl<'a, 'gcx, 'tcx> ParamTy { // FIXME(#50125): Ignoring `Self` with `idx != 0` might lead to weird behavior elsewhere, // but this should only be possible when using `-Z continue-parse-after-error` like // `compile-fail/issue-36638.rs`. - if self.name == keywords::SelfType.name().as_str() && self.idx == 0 { - true - } else { - false - } + self.name == keywords::SelfType.name().as_str() && self.idx == 0 } } @@ -2021,18 +2017,14 @@ impl<'tcx> Const<'tcx> { tcx: TyCtxt<'_, '_, '_>, ty: ParamEnvAnd<'tcx, Ty<'tcx>>, ) -> u128 { - match self.assert_bits(tcx, ty) { - Some(val) => val, - None => bug!("expected bits of {}, got {:#?}", ty.value, self), - } + self.assert_bits(tcx, ty).unwrap_or_else(|| + bug!("expected bits of {}, got {:#?}", ty.value, self)) } #[inline] pub fn unwrap_usize(&self, tcx: TyCtxt<'_, '_, '_>) -> u64 { - match self.assert_usize(tcx) { - Some(val) => val, - None => bug!("expected constant usize, got {:#?}", self), - } + self.assert_usize(tcx).unwrap_or_else(|| + bug!("expected constant usize, got {:#?}", self)) } } diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index 75b9a95b32f..72d720b138a 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -205,10 +205,9 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> { where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Kind<'tcx> { Substs::for_item(tcx, def_id, |param, substs| { - match self.get(param.index as usize) { - Some(&kind) => kind, - None => mk_kind(param, substs), - } + self.get(param.index as usize) + .cloned() + .unwrap_or_else(|| mk_kind(param, substs)) }) } diff --git a/src/librustc/ty/trait_def.rs b/src/librustc/ty/trait_def.rs index fe19b6096c5..af678c3e992 100644 --- a/src/librustc/ty/trait_def.rs +++ b/src/librustc/ty/trait_def.rs @@ -138,10 +138,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { } } } else { - for v in impls.non_blanket_impls.values() { - for &impl_def_id in v { - f(impl_def_id); - } + for &impl_def_id in impls.non_blanket_impls.values().flatten() { + f(impl_def_id); } } } diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index 380f7601401..4e2c9cceab6 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -257,16 +257,13 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> { impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { pub fn has_error_field(self, ty: Ty<'tcx>) -> bool { - match ty.sty { - ty::Adt(def, substs) => { - for field in def.all_fields() { - let field_ty = field.ty(self, substs); - if let Error = field_ty.sty { - return true; - } + if let ty::Adt(def, substs) = ty.sty { + for field in def.all_fields() { + let field_ty = field.ty(self, substs); + if let Error = field_ty.sty { + return true; } } - _ => (), } false } @@ -421,7 +418,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { let ty = self.type_of(adt_did); self.for_each_relevant_impl(drop_trait, ty, |impl_did| { if let Some(item) = self.associated_items(impl_did).next() { - if let Ok(()) = validate(self, impl_did) { + if validate(self, impl_did).is_ok() { dtor_did = Some(item.def_id); } } @@ -906,20 +903,17 @@ fn needs_drop_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, let (param_env, ty) = query.into_parts(); let needs_drop = |ty: Ty<'tcx>| -> bool { - match tcx.try_needs_drop_raw(DUMMY_SP, param_env.and(ty)) { - Ok(v) => v, - Err(mut bug) => { - // Cycles should be reported as an error by `check_representable`. - // - // Consider the type as not needing drop in the meanwhile to - // avoid further errors. - // - // In case we forgot to emit a bug elsewhere, delay our - // diagnostic to get emitted as a compiler bug. - bug.delay_as_bug(); - false - } - } + tcx.try_needs_drop_raw(DUMMY_SP, param_env.and(ty)).unwrap_or_else(|mut bug| { + // Cycles should be reported as an error by `check_representable`. + // + // Consider the type as not needing drop in the meanwhile to + // avoid further errors. + // + // In case we forgot to emit a bug elsewhere, delay our + // diagnostic to get emitted as a compiler bug. + bug.delay_as_bug(); + false + }) }; assert!(!ty.needs_infer()); diff --git a/src/librustc/ty/walk.rs b/src/librustc/ty/walk.rs index cf87c2d4574..540765dfaaf 100644 --- a/src/librustc/ty/walk.rs +++ b/src/librustc/ty/walk.rs @@ -54,7 +54,7 @@ impl<'tcx> Iterator for TypeWalker<'tcx> { debug!("next(): stack={:?}", self.stack); match self.stack.pop() { None => { - return None; + None } Some(ty) => { self.last_subtree = self.stack.len(); |
