Skip to content

Commit fddcd31

Browse files
committed
Auto merge of #54743 - ljedrz:cleanup_ty_p2, r=zackmdavis
Cleanup rustc/ty part 2 The second part of cleanups and minor improvements for rustc/ty. - improve allocations - calculate span after a possible early continue - simplify some patterns - mark a comment as FIXME - whitespace fixes The PR is independent from from the first part.
2 parents 2155f27 + 04b99bc commit fddcd31

14 files changed

+146
-178
lines changed

src/librustc/ty/fold.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -782,11 +782,10 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
782782
}
783783

784784
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
785-
match *r {
786-
ty::ReLateBound(debruijn, br) if debruijn == self.current_index => {
785+
if let ty::ReLateBound(debruijn, br) = *r {
786+
if debruijn == self.current_index {
787787
self.regions.insert(br);
788788
}
789-
_ => { }
790789
}
791790
false
792791
}

src/librustc/ty/instance.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ impl<'a, 'b, 'tcx> Instance<'tcx> {
231231
}
232232

233233
pub fn resolve_closure(
234-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
235-
def_id: DefId,
236-
substs: ty::ClosureSubsts<'tcx>,
237-
requested_kind: ty::ClosureKind)
238-
-> Instance<'tcx>
234+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
235+
def_id: DefId,
236+
substs: ty::ClosureSubsts<'tcx>,
237+
requested_kind: ty::ClosureKind)
238+
-> Instance<'tcx>
239239
{
240240
let actual_kind = substs.closure_kind(def_id, tcx);
241241

@@ -255,8 +255,8 @@ fn resolve_associated_item<'a, 'tcx>(
255255
) -> Option<Instance<'tcx>> {
256256
let def_id = trait_item.def_id;
257257
debug!("resolve_associated_item(trait_item={:?}, \
258-
trait_id={:?}, \
259-
rcvr_substs={:?})",
258+
trait_id={:?}, \
259+
rcvr_substs={:?})",
260260
def_id, trait_id, rcvr_substs);
261261

262262
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
@@ -280,7 +280,7 @@ fn resolve_associated_item<'a, 'tcx>(
280280
traits::VtableClosure(closure_data) => {
281281
let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
282282
Some(Instance::resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
283-
trait_closure_kind))
283+
trait_closure_kind))
284284
}
285285
traits::VtableFnPointer(ref data) => {
286286
Some(Instance {
@@ -310,7 +310,7 @@ fn resolve_associated_item<'a, 'tcx>(
310310
}
311311

312312
fn needs_fn_once_adapter_shim<'a, 'tcx>(actual_closure_kind: ty::ClosureKind,
313-
trait_closure_kind: ty::ClosureKind)
313+
trait_closure_kind: ty::ClosureKind)
314314
-> Result<bool, ()>
315315
{
316316
match (actual_closure_kind, trait_closure_kind) {
@@ -344,13 +344,14 @@ fn needs_fn_once_adapter_shim<'a, 'tcx>(actual_closure_kind: ty::ClosureKind,
344344
}
345345

346346
fn fn_once_adapter_instance<'a, 'tcx>(
347-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
348-
closure_did: DefId,
349-
substs: ty::ClosureSubsts<'tcx>,
350-
) -> Instance<'tcx> {
347+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
348+
closure_did: DefId,
349+
substs: ty::ClosureSubsts<'tcx>)
350+
-> Instance<'tcx>
351+
{
351352
debug!("fn_once_adapter_shim({:?}, {:?})",
352-
closure_did,
353-
substs);
353+
closure_did,
354+
substs);
354355
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
355356
let call_once = tcx.associated_items(fn_once)
356357
.find(|it| it.kind == ty::AssociatedKind::Method)

src/librustc/ty/item_path.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
221221

222222
let data = cur_def_key.disambiguated_data.data;
223223
let symbol = data.get_opt_name().map(|n| n.as_str()).unwrap_or_else(|| {
224-
if let DefPathData::CrateRoot = data { // reexported `extern crate` (#43189)
224+
if let DefPathData::CrateRoot = data { // reexported `extern crate` (#43189)
225225
self.original_crate_name(cur_def.krate).as_str()
226226
} else {
227227
Symbol::intern("<unnamed>").as_str()
@@ -365,9 +365,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
365365

366366
if let Some(trait_ref) = impl_trait_ref {
367367
// Trait impls.
368-
buffer.push(&format!("<{} as {}>",
369-
self_ty,
370-
trait_ref));
368+
buffer.push(&format!("<{} as {}>", self_ty, trait_ref));
371369
return;
372370
}
373371

src/librustc/ty/layout.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ pub trait IntegerExt {
3232
fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, signed: bool) -> Ty<'tcx>;
3333
fn from_attr<C: HasDataLayout>(cx: C, ity: attr::IntType) -> Integer;
3434
fn repr_discr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
35-
ty: Ty<'tcx>,
36-
repr: &ReprOptions,
37-
min: i128,
38-
max: i128)
39-
-> (Integer, bool);
35+
ty: Ty<'tcx>,
36+
repr: &ReprOptions,
37+
min: i128,
38+
max: i128)
39+
-> (Integer, bool);
4040
}
4141

4242
impl IntegerExt for Integer {
@@ -76,11 +76,11 @@ impl IntegerExt for Integer {
7676
/// N.B.: u128 values above i128::MAX will be treated as signed, but
7777
/// that shouldn't affect anything, other than maybe debuginfo.
7878
fn repr_discr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
79-
ty: Ty<'tcx>,
80-
repr: &ReprOptions,
81-
min: i128,
82-
max: i128)
83-
-> (Integer, bool) {
79+
ty: Ty<'tcx>,
80+
repr: &ReprOptions,
81+
min: i128,
82+
max: i128)
83+
-> (Integer, bool) {
8484
// Theoretically, negative values could be larger in unsigned representation
8585
// than the unsigned representation of the signed minimum. However, if there
8686
// are any negative values, the only valid unsigned representation is u128
@@ -96,7 +96,7 @@ impl IntegerExt for Integer {
9696
let fit = if ity.is_signed() { signed_fit } else { unsigned_fit };
9797
if discr < fit {
9898
bug!("Integer::repr_discr: `#[repr]` hint too small for \
99-
discriminant range of enum `{}", ty)
99+
discriminant range of enum `{}", ty)
100100
}
101101
return (discr, ity.is_signed());
102102
}
@@ -106,7 +106,7 @@ impl IntegerExt for Integer {
106106
// WARNING: the ARM EABI has two variants; the one corresponding
107107
// to `at_least == I32` appears to be used on Linux and NetBSD,
108108
// but some systems may use the variant corresponding to no
109-
// lower bound. However, we don't run on those yet...?
109+
// lower bound. However, we don't run on those yet...?
110110
"arm" => min_from_extern = Some(I32),
111111
_ => min_from_extern = Some(I32),
112112
}
@@ -250,6 +250,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
250250
/// A univariant, but with a prefix of an arbitrary size & alignment (e.g. enum tag).
251251
Prefixed(Size, Align),
252252
}
253+
253254
let univariant_uninterned = |fields: &[TyLayout<'_>], repr: &ReprOptions, kind| {
254255
let packed = repr.packed();
255256
if packed && repr.align > 0 {
@@ -324,7 +325,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
324325
let field = fields[i as usize];
325326
if !sized {
326327
bug!("univariant: field #{} of `{}` comes after unsized field",
327-
offsets.len(), ty);
328+
offsets.len(), ty);
328329
}
329330

330331
if field.is_unsized() {
@@ -628,7 +629,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
628629
};
629630

630631
univariant(&tys.iter().map(|ty| self.layout_of(ty)).collect::<Result<Vec<_>, _>>()?,
631-
&ReprOptions::default(), kind)?
632+
&ReprOptions::default(), kind)?
632633
}
633634

634635
// SIMD vector types.
@@ -640,7 +641,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
640641
Abi::Scalar(ref scalar) => scalar.clone(),
641642
_ => {
642643
tcx.sess.fatal(&format!("monomorphising SIMD type `{}` with \
643-
a non-machine element type `{}`",
644+
a non-machine element type `{}`",
644645
ty, element.ty));
645646
}
646647
};
@@ -743,7 +744,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
743744
// Only one variant is present.
744745
(present_second.is_none() &&
745746
// Representation optimizations are allowed.
746-
!def.repr.inhibit_enum_layout_opt());
747+
!def.repr.inhibit_enum_layout_opt());
747748
if is_struct {
748749
// Struct, or univariant enum equivalent to a struct.
749750
// (Typechecking will reject discriminant-sizing attrs.)
@@ -755,7 +756,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
755756
let param_env = tcx.param_env(def.did);
756757
let last_field = def.variants[v].fields.last().unwrap();
757758
let always_sized = tcx.type_of(last_field.did)
758-
.is_sized(tcx.at(DUMMY_SP), param_env);
759+
.is_sized(tcx.at(DUMMY_SP), param_env);
759760
if !always_sized { StructKind::MaybeUnsized }
760761
else { StructKind::AlwaysSized }
761762
};
@@ -1258,8 +1259,8 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
12581259
let fields: Vec<_> =
12591260
variant_def.fields.iter().map(|f| f.ident.name).collect();
12601261
build_variant_info(Some(variant_def.name),
1261-
&fields,
1262-
layout.for_variant(self, i))
1262+
&fields,
1263+
layout.for_variant(self, i))
12631264
})
12641265
.collect();
12651266
record(adt_kind.into(), adt_packed, match layout.variants {

src/librustc/ty/mod.rs

+21-25
Original file line numberDiff line numberDiff line change
@@ -862,24 +862,22 @@ pub struct GenericParamDef {
862862

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

877876
pub fn to_bound_region(&self) -> ty::BoundRegion {
878-
match self.kind {
879-
GenericParamDefKind::Lifetime => {
880-
self.to_early_bound_region_data().to_bound_region()
881-
}
882-
_ => bug!("cannot convert a non-lifetime parameter def to an early bound region")
877+
if let GenericParamDefKind::Lifetime = self.kind {
878+
self.to_early_bound_region_data().to_bound_region()
879+
} else {
880+
bug!("cannot convert a non-lifetime parameter def to an early bound region")
883881
}
884882
}
885883
}
@@ -957,7 +955,7 @@ impl<'a, 'gcx, 'tcx> Generics {
957955
}
958956
} else {
959957
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
960-
.region_param(param, tcx)
958+
.region_param(param, tcx)
961959
}
962960
}
963961

@@ -974,7 +972,7 @@ impl<'a, 'gcx, 'tcx> Generics {
974972
}
975973
} else {
976974
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
977-
.type_param(param, tcx)
975+
.type_param(param, tcx)
978976
}
979977
}
980978
}
@@ -1376,7 +1374,7 @@ impl<'tcx> Predicate<'tcx> {
13761374
}
13771375
};
13781376

1379-
// The only reason to collect into a vector here is that I was
1377+
// FIXME: The only reason to collect into a vector here is that I was
13801378
// too lazy to make the full (somewhat complicated) iterator
13811379
// type that would be needed here. But I wanted this fn to
13821380
// return an iterator conceptually, rather than a `Vec`, so as
@@ -2224,7 +2222,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
22242222
if !expr_did.is_local() {
22252223
span_bug!(tcx.def_span(expr_did),
22262224
"variant discriminant evaluation succeeded \
2227-
in its crate but failed locally");
2225+
in its crate but failed locally");
22282226
}
22292227
None
22302228
}
@@ -2360,9 +2358,9 @@ impl<'a, 'gcx, 'tcx> AdtDef {
23602358
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}",
23612359
ty, adt_tys);
23622360
adt_tys.iter()
2363-
.map(|ty| ty.subst(tcx, substs))
2364-
.flat_map(|ty| self.sized_constraint_for_ty(tcx, ty))
2365-
.collect()
2361+
.map(|ty| ty.subst(tcx, substs))
2362+
.flat_map(|ty| self.sized_constraint_for_ty(tcx, ty))
2363+
.collect()
23662364
}
23672365

23682366
Projection(..) | Opaque(..) => {
@@ -2903,9 +2901,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
29032901
}
29042902
}
29052903

2906-
fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
2907-
-> AssociatedItem
2908-
{
2904+
fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> AssociatedItem {
29092905
let id = tcx.hir.as_local_node_id(def_id).unwrap();
29102906
let parent_id = tcx.hir.get_parent(id);
29112907
let parent_def_id = tcx.hir.local_def_id(parent_id);
@@ -3019,8 +3015,8 @@ pub fn is_impl_trait_defn(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<DefI
30193015
/// See `ParamEnv` struct def'n for details.
30203016
fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
30213017
def_id: DefId)
3022-
-> ParamEnv<'tcx> {
3023-
3018+
-> ParamEnv<'tcx>
3019+
{
30243020
// The param_env of an impl Trait type is its defining function's param_env
30253021
if let Some(parent) = is_impl_trait_defn(tcx, def_id) {
30263022
return param_env(tcx, parent);

src/librustc/ty/outlives.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
7373
// projection).
7474
match ty.sty {
7575
ty::Closure(def_id, ref substs) => {
76-
7776
for upvar_ty in substs.upvar_tys(def_id, *self) {
7877
self.compute_components(upvar_ty, out);
7978
}
@@ -183,9 +182,5 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
183182
}
184183

185184
fn push_region_constraints<'tcx>(out: &mut Vec<Component<'tcx>>, regions: Vec<ty::Region<'tcx>>) {
186-
for r in regions {
187-
if !r.is_late_bound() {
188-
out.push(Component::Region(r));
189-
}
190-
}
185+
out.extend(regions.iter().filter(|&r| !r.is_late_bound()).map(|r| Component::Region(r)));
191186
}

0 commit comments

Comments
 (0)