From 187bcb9bd18069896761d350e264953ede1eea04 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 2 Oct 2018 10:52:43 +0200 Subject: [PATCH 1/5] rustc/ty: whitespace fixes --- src/librustc/ty/instance.rs | 31 ++++++++++++----------- src/librustc/ty/item_path.rs | 6 ++--- src/librustc/ty/layout.rs | 39 +++++++++++++++-------------- src/librustc/ty/mod.rs | 20 +++++++-------- src/librustc/ty/outlives.rs | 1 - src/librustc/ty/relate.rs | 7 +++--- src/librustc/ty/structural_impls.rs | 13 +++++----- src/librustc/ty/subst.rs | 4 +-- src/librustc/ty/trait_def.rs | 6 ++--- src/librustc/ty/util.rs | 20 ++++++++------- src/librustc/ty/wf.rs | 4 +-- 11 files changed, 75 insertions(+), 76 deletions(-) diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index a6e181a26f420..30737ec0c0a45 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -229,11 +229,11 @@ impl<'a, 'b, 'tcx> Instance<'tcx> { } pub fn resolve_closure( - tcx: TyCtxt<'a, 'tcx, 'tcx>, - def_id: DefId, - substs: ty::ClosureSubsts<'tcx>, - requested_kind: ty::ClosureKind) - -> Instance<'tcx> + tcx: TyCtxt<'a, 'tcx, 'tcx>, + def_id: DefId, + substs: ty::ClosureSubsts<'tcx>, + requested_kind: ty::ClosureKind) + -> Instance<'tcx> { let actual_kind = substs.closure_kind(def_id, tcx); @@ -253,8 +253,8 @@ fn resolve_associated_item<'a, 'tcx>( ) -> Option> { let def_id = trait_item.def_id; debug!("resolve_associated_item(trait_item={:?}, \ - trait_id={:?}, \ - rcvr_substs={:?})", + trait_id={:?}, \ + rcvr_substs={:?})", def_id, trait_id, rcvr_substs); let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs); @@ -278,7 +278,7 @@ fn resolve_associated_item<'a, 'tcx>( traits::VtableClosure(closure_data) => { let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap(); Some(Instance::resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs, - trait_closure_kind)) + trait_closure_kind)) } traits::VtableFnPointer(ref data) => { Some(Instance { @@ -308,7 +308,7 @@ fn resolve_associated_item<'a, 'tcx>( } fn needs_fn_once_adapter_shim<'a, 'tcx>(actual_closure_kind: ty::ClosureKind, - trait_closure_kind: ty::ClosureKind) + trait_closure_kind: ty::ClosureKind) -> Result { match (actual_closure_kind, trait_closure_kind) { @@ -342,13 +342,14 @@ fn needs_fn_once_adapter_shim<'a, 'tcx>(actual_closure_kind: ty::ClosureKind, } fn fn_once_adapter_instance<'a, 'tcx>( - tcx: TyCtxt<'a, 'tcx, 'tcx>, - closure_did: DefId, - substs: ty::ClosureSubsts<'tcx>, - ) -> Instance<'tcx> { + tcx: TyCtxt<'a, 'tcx, 'tcx>, + closure_did: DefId, + substs: ty::ClosureSubsts<'tcx>) + -> Instance<'tcx> +{ debug!("fn_once_adapter_shim({:?}, {:?})", - closure_did, - substs); + closure_did, + substs); let fn_once = tcx.lang_items().fn_once_trait().unwrap(); let call_once = tcx.associated_items(fn_once) .find(|it| it.kind == ty::AssociatedKind::Method) diff --git a/src/librustc/ty/item_path.rs b/src/librustc/ty/item_path.rs index c8d104e6c321f..bd4fee304a332 100644 --- a/src/librustc/ty/item_path.rs +++ b/src/librustc/ty/item_path.rs @@ -221,7 +221,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { let data = cur_def_key.disambiguated_data.data; let symbol = data.get_opt_name().map(|n| n.as_str()).unwrap_or_else(|| { - if let DefPathData::CrateRoot = data { // reexported `extern crate` (#43189) + if let DefPathData::CrateRoot = data { // reexported `extern crate` (#43189) self.original_crate_name(cur_def.krate).as_str() } else { Symbol::intern("").as_str() @@ -365,9 +365,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { if let Some(trait_ref) = impl_trait_ref { // Trait impls. - buffer.push(&format!("<{} as {}>", - self_ty, - trait_ref)); + buffer.push(&format!("<{} as {}>", self_ty, trait_ref)); return; } diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 8bfe46568eb19..671a725a1c668 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -32,11 +32,11 @@ pub trait IntegerExt { fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, signed: bool) -> Ty<'tcx>; fn from_attr(cx: C, ity: attr::IntType) -> Integer; fn repr_discr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, - ty: Ty<'tcx>, - repr: &ReprOptions, - min: i128, - max: i128) - -> (Integer, bool); + ty: Ty<'tcx>, + repr: &ReprOptions, + min: i128, + max: i128) + -> (Integer, bool); } impl IntegerExt for Integer { @@ -76,11 +76,11 @@ impl IntegerExt for Integer { /// N.B.: u128 values above i128::MAX will be treated as signed, but /// that shouldn't affect anything, other than maybe debuginfo. fn repr_discr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, - ty: Ty<'tcx>, - repr: &ReprOptions, - min: i128, - max: i128) - -> (Integer, bool) { + ty: Ty<'tcx>, + repr: &ReprOptions, + min: i128, + max: i128) + -> (Integer, bool) { // Theoretically, negative values could be larger in unsigned representation // than the unsigned representation of the signed minimum. However, if there // are any negative values, the only valid unsigned representation is u128 @@ -96,7 +96,7 @@ impl IntegerExt for Integer { let fit = if ity.is_signed() { signed_fit } else { unsigned_fit }; if discr < fit { bug!("Integer::repr_discr: `#[repr]` hint too small for \ - discriminant range of enum `{}", ty) + discriminant range of enum `{}", ty) } return (discr, ity.is_signed()); } @@ -106,7 +106,7 @@ impl IntegerExt for Integer { // WARNING: the ARM EABI has two variants; the one corresponding // to `at_least == I32` appears to be used on Linux and NetBSD, // but some systems may use the variant corresponding to no - // lower bound. However, we don't run on those yet...? + // lower bound. However, we don't run on those yet...? "arm" => min_from_extern = Some(I32), _ => min_from_extern = Some(I32), } @@ -250,6 +250,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { /// A univariant, but with a prefix of an arbitrary size & alignment (e.g. enum tag). Prefixed(Size, Align), } + let univariant_uninterned = |fields: &[TyLayout<'_>], repr: &ReprOptions, kind| { let packed = repr.packed(); if packed && repr.align > 0 { @@ -324,7 +325,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { let field = fields[i as usize]; if !sized { bug!("univariant: field #{} of `{}` comes after unsized field", - offsets.len(), ty); + offsets.len(), ty); } if field.is_unsized() { @@ -628,7 +629,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { }; univariant(&tys.iter().map(|ty| self.layout_of(ty)).collect::, _>>()?, - &ReprOptions::default(), kind)? + &ReprOptions::default(), kind)? } // SIMD vector types. @@ -640,7 +641,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { Abi::Scalar(ref scalar) => scalar.clone(), _ => { tcx.sess.fatal(&format!("monomorphising SIMD type `{}` with \ - a non-machine element type `{}`", + a non-machine element type `{}`", ty, element.ty)); } }; @@ -743,7 +744,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { // Only one variant is present. (present_second.is_none() && // Representation optimizations are allowed. - !def.repr.inhibit_enum_layout_opt()); + !def.repr.inhibit_enum_layout_opt()); if is_struct { // Struct, or univariant enum equivalent to a struct. // (Typechecking will reject discriminant-sizing attrs.) @@ -755,7 +756,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { let param_env = tcx.param_env(def.did); let last_field = def.variants[v].fields.last().unwrap(); let always_sized = tcx.type_of(last_field.did) - .is_sized(tcx.at(DUMMY_SP), param_env); + .is_sized(tcx.at(DUMMY_SP), param_env); if !always_sized { StructKind::MaybeUnsized } else { StructKind::AlwaysSized } }; @@ -1258,8 +1259,8 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { let fields: Vec<_> = variant_def.fields.iter().map(|f| f.ident.name).collect(); build_variant_info(Some(variant_def.name), - &fields, - layout.for_variant(self, i)) + &fields, + layout.for_variant(self, i)) }) .collect(); record(adt_kind.into(), adt_packed, match layout.variants { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index b2281691bd660..2685d45e279d1 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -956,7 +956,7 @@ impl<'a, 'gcx, 'tcx> Generics { } } else { tcx.generics_of(self.parent.expect("parent_count>0 but no parent?")) - .region_param(param, tcx) + .region_param(param, tcx) } } @@ -973,7 +973,7 @@ impl<'a, 'gcx, 'tcx> Generics { } } else { tcx.generics_of(self.parent.expect("parent_count>0 but no parent?")) - .type_param(param, tcx) + .type_param(param, tcx) } } } @@ -2192,7 +2192,7 @@ impl<'a, 'gcx, 'tcx> AdtDef { if !expr_did.is_local() { span_bug!(tcx.def_span(expr_did), "variant discriminant evaluation succeeded \ - in its crate but failed locally"); + in its crate but failed locally"); } None } @@ -2328,9 +2328,9 @@ impl<'a, 'gcx, 'tcx> AdtDef { debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys); adt_tys.iter() - .map(|ty| ty.subst(tcx, substs)) - .flat_map(|ty| self.sized_constraint_for_ty(tcx, ty)) - .collect() + .map(|ty| ty.subst(tcx, substs)) + .flat_map(|ty| self.sized_constraint_for_ty(tcx, ty)) + .collect() } Projection(..) | Opaque(..) => { @@ -2869,9 +2869,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { } } -fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) - -> AssociatedItem -{ +fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> AssociatedItem { let id = tcx.hir.as_local_node_id(def_id).unwrap(); let parent_id = tcx.hir.get_parent(id); let parent_def_id = tcx.hir.local_def_id(parent_id); @@ -2985,8 +2983,8 @@ pub fn is_impl_trait_defn(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) - -> ParamEnv<'tcx> { - + -> ParamEnv<'tcx> +{ // The param_env of an impl Trait type is its defining function's param_env if let Some(parent) = is_impl_trait_defn(tcx, def_id) { return param_env(tcx, parent); diff --git a/src/librustc/ty/outlives.rs b/src/librustc/ty/outlives.rs index 68f6707087687..e77080a0c9136 100644 --- a/src/librustc/ty/outlives.rs +++ b/src/librustc/ty/outlives.rs @@ -73,7 +73,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { // projection). match ty.sty { ty::Closure(def_id, ref substs) => { - for upvar_ty in substs.upvar_tys(def_id, *self) { self.compute_components(upvar_ty, out); } diff --git a/src/librustc/ty/relate.rs b/src/librustc/ty/relate.rs index 547f7cecc4e06..a88f5095f0a17 100644 --- a/src/librustc/ty/relate.rs +++ b/src/librustc/ty/relate.rs @@ -274,9 +274,10 @@ impl<'tcx> Relate<'tcx> for Vec> { if a.len() != b.len() { Err(TypeError::ProjectionBoundsLength(expected_found(relation, &a.len(), &b.len()))) } else { - a.iter().zip(b) - .map(|(a, b)| relation.relate(a, b)) - .collect() + a.iter() + .zip(b) + .map(|(a, b)| relation.relate(a, b)) + .collect() } } } diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index aab268c07c4fb..7d13c8d9ecee5 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -386,12 +386,12 @@ impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> { type Lifted = ty::GenSig<'tcx>; fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option { tcx.lift(&(self.yield_ty, self.return_ty)) - .map(|(yield_ty, return_ty)| { - ty::GenSig { - yield_ty, - return_ty, - } - }) + .map(|(yield_ty, return_ty)| { + ty::GenSig { + yield_ty, + return_ty, + } + }) } } @@ -452,7 +452,6 @@ impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> { CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)), ProjectionMismatched(x) => ProjectionMismatched(x), ProjectionBoundsLength(x) => ProjectionBoundsLength(x), - Sorts(ref x) => return tcx.lift(x).map(Sorts), OldStyleLUB(ref x) => return tcx.lift(x).map(OldStyleLUB), ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch) diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index 4b3a70e525ec3..75b9a95b32f2c 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -361,7 +361,7 @@ impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Substs<'tcx> {} pub trait Subst<'tcx> : Sized { fn subst<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, - substs: &[Kind<'tcx>]) -> Self { + substs: &[Kind<'tcx>]) -> Self { self.subst_spanned(tcx, substs, None) } @@ -489,7 +489,7 @@ impl<'a, 'gcx, 'tcx> SubstFolder<'a, 'gcx, 'tcx> { span_bug!( span, "Type parameter `{:?}` ({:?}/{}) out of range \ - when substituting (root type={:?}) substs={:?}", + when substituting (root type={:?}) substs={:?}", p, source_ty, p.idx, diff --git a/src/librustc/ty/trait_def.rs b/src/librustc/ty/trait_def.rs index 22277128383b5..fe19b6096c5d8 100644 --- a/src/librustc/ty/trait_def.rs +++ b/src/librustc/ty/trait_def.rs @@ -173,9 +173,9 @@ pub(super) fn trait_impls_of_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, fast_reject::simplify_type(tcx, impl_self_ty, false) { impls.non_blanket_impls - .entry(simplified_self_ty) - .or_default() - .push(impl_def_id); + .entry(simplified_self_ty) + .or_default() + .push(impl_def_id); } else { impls.blanket_impls.push(impl_def_id); } diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index 93fc77359e43a..380f7601401ad 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -119,17 +119,17 @@ pub trait IntTypeExt { impl IntTypeExt for attr::IntType { fn to_ty<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> { match *self { - SignedInt(ast::IntTy::I8) => tcx.types.i8, - SignedInt(ast::IntTy::I16) => tcx.types.i16, - SignedInt(ast::IntTy::I32) => tcx.types.i32, - SignedInt(ast::IntTy::I64) => tcx.types.i64, + SignedInt(ast::IntTy::I8) => tcx.types.i8, + SignedInt(ast::IntTy::I16) => tcx.types.i16, + SignedInt(ast::IntTy::I32) => tcx.types.i32, + SignedInt(ast::IntTy::I64) => tcx.types.i64, SignedInt(ast::IntTy::I128) => tcx.types.i128, - SignedInt(ast::IntTy::Isize) => tcx.types.isize, + SignedInt(ast::IntTy::Isize) => tcx.types.isize, UnsignedInt(ast::UintTy::U8) => tcx.types.u8, UnsignedInt(ast::UintTy::U16) => tcx.types.u16, UnsignedInt(ast::UintTy::U32) => tcx.types.u32, UnsignedInt(ast::UintTy::U64) => tcx.types.u64, - UnsignedInt(ast::UintTy::U128) => tcx.types.u128, + UnsignedInt(ast::UintTy::U128) => tcx.types.u128, UnsignedInt(ast::UintTy::Usize) => tcx.types.usize, } } @@ -513,7 +513,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { false } } - }).map(|(&item_param, _)| item_param).collect(); + }) + .map(|(&item_param, _)| item_param) + .collect(); debug!("destructor_constraint({:?}) = {:?}", def.did, result); result } @@ -674,8 +676,8 @@ impl<'a, 'tcx> ty::TyS<'tcx> { pub fn is_representable(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span) - -> Representability { - + -> Representability + { // Iterate until something non-representable is found fn fold_repr>(iter: It) -> Representability { iter.fold(Representability::Representable, |r1, r2| { diff --git a/src/librustc/ty/wf.rs b/src/librustc/ty/wf.rs index 63206a660df41..a5a0be21f2e5f 100644 --- a/src/librustc/ty/wf.rs +++ b/src/librustc/ty/wf.rs @@ -223,8 +223,8 @@ impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> { let predicate = ty::Predicate::ConstEvaluatable(def_id, substs); let cause = self.cause(traits::MiscObligation); self.out.push(traits::Obligation::new(cause, - self.param_env, - predicate)); + self.param_env, + predicate)); } } From f8caccacf49520d85ce790a83b9899f85bdd74c5 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 2 Oct 2018 10:56:36 +0200 Subject: [PATCH 2/5] rustc/ty: simplify some patterns --- src/librustc/ty/fold.rs | 5 ++-- src/librustc/ty/mod.rs | 24 ++++++++--------- src/librustc/ty/relate.rs | 39 +++++++++++++--------------- src/librustc/ty/structural_impls.rs | 20 +++++---------- src/librustc/ty/sty.rs | 18 ++++--------- src/librustc/ty/subst.rs | 7 +++-- src/librustc/ty/trait_def.rs | 6 ++--- src/librustc/ty/util.rs | 40 ++++++++++++----------------- 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 6bf493b496c8b..dd7099a406446 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 2685d45e279d1..380f5a5866550 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 a88f5095f0a17..082c1bd5fea44 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 7d13c8d9ecee5..729a05eb5d43a 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 6c40dd8923916..bc5186b08b864 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 75b9a95b32f2c..72d720b138aac 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 fe19b6096c5d8..af678c3e9924b 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 380f7601401ad..4e2c9cceab634 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 cf87c2d457471..540765dfaafcf 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(); From 774881d78ef39069df2374586e28d1eb82507fe1 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 2 Oct 2018 10:58:50 +0200 Subject: [PATCH 3/5] rustc/ty: mark a comment as FIXME --- src/librustc/ty/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 380f5a5866550..41db7c5f0390f 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -1373,7 +1373,7 @@ impl<'tcx> Predicate<'tcx> { } }; - // The only reason to collect into a vector here is that I was + // FIXME: The only reason to collect into a vector here is that I was // too lazy to make the full (somewhat complicated) iterator // type that would be needed here. But I wanted this fn to // return an iterator conceptually, rather than a `Vec`, so as From db171649d3e2298859740d164f92e99c8bd3cf4b Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 2 Oct 2018 11:00:11 +0200 Subject: [PATCH 4/5] rustc/ty: calculate span after a possible early continue --- src/librustc/ty/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index 4e2c9cceab634..e4141211a4fe5 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -205,11 +205,11 @@ impl<'tcx> ty::ParamEnv<'tcx> { let mut infringing = Vec::new(); for variant in &adt.variants { for field in &variant.fields { - let span = tcx.def_span(field.did); let ty = field.ty(tcx, substs); if ty.references_error() { continue; } + let span = tcx.def_span(field.did); let cause = ObligationCause { span, ..ObligationCause::dummy() }; let ctx = traits::FulfillmentContext::new(); match traits::fully_normalize(&infcx, ctx, cause, self, &ty) { From 04b99bc10390514832197b30c34a3993cd028092 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 2 Oct 2018 11:00:41 +0200 Subject: [PATCH 5/5] rustc/ty: improve allocations --- src/librustc/ty/outlives.rs | 6 +----- src/librustc/ty/subst.rs | 1 + src/librustc/ty/wf.rs | 1 + 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/librustc/ty/outlives.rs b/src/librustc/ty/outlives.rs index e77080a0c9136..350af6b93057e 100644 --- a/src/librustc/ty/outlives.rs +++ b/src/librustc/ty/outlives.rs @@ -180,9 +180,5 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { } fn push_region_constraints<'tcx>(out: &mut Vec>, regions: Vec>) { - for r in regions { - if !r.is_late_bound() { - out.push(Component::Region(r)); - } - } + out.extend(regions.iter().filter(|&r| !r.is_late_bound()).map(|r| Component::Region(r))); } diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index 72d720b138aac..c0a42fd585482 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -229,6 +229,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> { mk_kind: &mut F) where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Kind<'tcx> { + substs.reserve(defs.params.len()); for param in &defs.params { let kind = mk_kind(param, substs); assert_eq!(param.index as usize, substs.len()); diff --git a/src/librustc/ty/wf.rs b/src/librustc/ty/wf.rs index a5a0be21f2e5f..e2cc55a693f3d 100644 --- a/src/librustc/ty/wf.rs +++ b/src/librustc/ty/wf.rs @@ -493,6 +493,7 @@ impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> { let explicit_bound = region; + self.out.reserve(implicit_bounds.len()); for implicit_bound in implicit_bounds { let cause = self.cause(traits::ObjectTypeBound(ty, explicit_bound)); let outlives = ty::Binder::dummy(