Skip to content

Commit 8b9145e

Browse files
authored
Rollup merge of rust-lang#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 f66f01e + 04b99bc commit 8b9145e

14 files changed

+146
-178
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/instance.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ impl<'a, 'b, 'tcx> Instance<'tcx> {
229229
}
230230

231231
pub fn resolve_closure(
232-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
233-
def_id: DefId,
234-
substs: ty::ClosureSubsts<'tcx>,
235-
requested_kind: ty::ClosureKind)
236-
-> Instance<'tcx>
232+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
233+
def_id: DefId,
234+
substs: ty::ClosureSubsts<'tcx>,
235+
requested_kind: ty::ClosureKind)
236+
-> Instance<'tcx>
237237
{
238238
let actual_kind = substs.closure_kind(def_id, tcx);
239239

@@ -253,8 +253,8 @@ fn resolve_associated_item<'a, 'tcx>(
253253
) -> Option<Instance<'tcx>> {
254254
let def_id = trait_item.def_id;
255255
debug!("resolve_associated_item(trait_item={:?}, \
256-
trait_id={:?}, \
257-
rcvr_substs={:?})",
256+
trait_id={:?}, \
257+
rcvr_substs={:?})",
258258
def_id, trait_id, rcvr_substs);
259259

260260
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
@@ -278,7 +278,7 @@ fn resolve_associated_item<'a, 'tcx>(
278278
traits::VtableClosure(closure_data) => {
279279
let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
280280
Some(Instance::resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
281-
trait_closure_kind))
281+
trait_closure_kind))
282282
}
283283
traits::VtableFnPointer(ref data) => {
284284
Some(Instance {
@@ -308,7 +308,7 @@ fn resolve_associated_item<'a, 'tcx>(
308308
}
309309

310310
fn needs_fn_once_adapter_shim<'a, 'tcx>(actual_closure_kind: ty::ClosureKind,
311-
trait_closure_kind: ty::ClosureKind)
311+
trait_closure_kind: ty::ClosureKind)
312312
-> Result<bool, ()>
313313
{
314314
match (actual_closure_kind, trait_closure_kind) {
@@ -342,13 +342,14 @@ fn needs_fn_once_adapter_shim<'a, 'tcx>(actual_closure_kind: ty::ClosureKind,
342342
}
343343

344344
fn fn_once_adapter_instance<'a, 'tcx>(
345-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
346-
closure_did: DefId,
347-
substs: ty::ClosureSubsts<'tcx>,
348-
) -> Instance<'tcx> {
345+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
346+
closure_did: DefId,
347+
substs: ty::ClosureSubsts<'tcx>)
348+
-> Instance<'tcx>
349+
{
349350
debug!("fn_once_adapter_shim({:?}, {:?})",
350-
closure_did,
351-
substs);
351+
closure_did,
352+
substs);
352353
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
353354
let call_once = tcx.associated_items(fn_once)
354355
.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
@@ -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
}
@@ -956,7 +954,7 @@ impl<'a, 'gcx, 'tcx> Generics {
956954
}
957955
} else {
958956
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
959-
.region_param(param, tcx)
957+
.region_param(param, tcx)
960958
}
961959
}
962960

@@ -973,7 +971,7 @@ impl<'a, 'gcx, 'tcx> Generics {
973971
}
974972
} else {
975973
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
976-
.type_param(param, tcx)
974+
.type_param(param, tcx)
977975
}
978976
}
979977
}
@@ -1375,7 +1373,7 @@ impl<'tcx> Predicate<'tcx> {
13751373
}
13761374
};
13771375

1378-
// The only reason to collect into a vector here is that I was
1376+
// FIXME: The only reason to collect into a vector here is that I was
13791377
// too lazy to make the full (somewhat complicated) iterator
13801378
// type that would be needed here. But I wanted this fn to
13811379
// return an iterator conceptually, rather than a `Vec`, so as
@@ -2192,7 +2190,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
21922190
if !expr_did.is_local() {
21932191
span_bug!(tcx.def_span(expr_did),
21942192
"variant discriminant evaluation succeeded \
2195-
in its crate but failed locally");
2193+
in its crate but failed locally");
21962194
}
21972195
None
21982196
}
@@ -2328,9 +2326,9 @@ impl<'a, 'gcx, 'tcx> AdtDef {
23282326
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}",
23292327
ty, adt_tys);
23302328
adt_tys.iter()
2331-
.map(|ty| ty.subst(tcx, substs))
2332-
.flat_map(|ty| self.sized_constraint_for_ty(tcx, ty))
2333-
.collect()
2329+
.map(|ty| ty.subst(tcx, substs))
2330+
.flat_map(|ty| self.sized_constraint_for_ty(tcx, ty))
2331+
.collect()
23342332
}
23352333

23362334
Projection(..) | Opaque(..) => {
@@ -2869,9 +2867,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
28692867
}
28702868
}
28712869

2872-
fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
2873-
-> AssociatedItem
2874-
{
2870+
fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> AssociatedItem {
28752871
let id = tcx.hir.as_local_node_id(def_id).unwrap();
28762872
let parent_id = tcx.hir.get_parent(id);
28772873
let parent_def_id = tcx.hir.local_def_id(parent_id);
@@ -2985,8 +2981,8 @@ pub fn is_impl_trait_defn(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<DefI
29852981
/// See `ParamEnv` struct def'n for details.
29862982
fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
29872983
def_id: DefId)
2988-
-> ParamEnv<'tcx> {
2989-
2984+
-> ParamEnv<'tcx>
2985+
{
29902986
// The param_env of an impl Trait type is its defining function's param_env
29912987
if let Some(parent) = is_impl_trait_defn(tcx, def_id) {
29922988
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
}
@@ -181,9 +180,5 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
181180
}
182181

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

0 commit comments

Comments
 (0)