Skip to content

Commit

Permalink
Auto merge of #54743 - ljedrz:cleanup_ty_p2, r=zackmdavis
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Oct 5, 2018
2 parents 2155f27 + 04b99bc commit fddcd31
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 178 deletions.
5 changes: 2 additions & 3 deletions src/librustc/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,11 +782,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
}
Expand Down
31 changes: 16 additions & 15 deletions src/librustc/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,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);

Expand All @@ -255,8 +255,8 @@ fn resolve_associated_item<'a, 'tcx>(
) -> Option<Instance<'tcx>> {
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);
Expand All @@ -280,7 +280,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 {
Expand Down Expand Up @@ -310,7 +310,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<bool, ()>
{
match (actual_closure_kind, trait_closure_kind) {
Expand Down Expand Up @@ -344,13 +344,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)
Expand Down
6 changes: 2 additions & 4 deletions src/librustc/ty/item_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<unnamed>").as_str()
Expand Down Expand Up @@ -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;
}

Expand Down
39 changes: 20 additions & 19 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: HasDataLayout>(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 {
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
Expand All @@ -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),
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -628,7 +629,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
};

univariant(&tys.iter().map(|ty| self.layout_of(ty)).collect::<Result<Vec<_>, _>>()?,
&ReprOptions::default(), kind)?
&ReprOptions::default(), kind)?
}

// SIMD vector types.
Expand All @@ -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));
}
};
Expand Down Expand Up @@ -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.)
Expand All @@ -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 }
};
Expand Down Expand Up @@ -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 {
Expand Down
46 changes: 21 additions & 25 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,24 +862,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")
}
}
}
Expand Down Expand Up @@ -957,7 +955,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)
}
}

Expand All @@ -974,7 +972,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)
}
}
}
Expand Down Expand Up @@ -1376,7 +1374,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
Expand Down Expand Up @@ -2224,7 +2222,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
}
Expand Down Expand Up @@ -2360,9 +2358,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(..) => {
Expand Down Expand Up @@ -2903,9 +2901,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);
Expand Down Expand Up @@ -3019,8 +3015,8 @@ pub fn is_impl_trait_defn(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<DefI
/// See `ParamEnv` struct def'n for details.
fn param_env<'a, 'tcx>(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);
Expand Down
7 changes: 1 addition & 6 deletions src/librustc/ty/outlives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -183,9 +182,5 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

fn push_region_constraints<'tcx>(out: &mut Vec<Component<'tcx>>, regions: Vec<ty::Region<'tcx>>) {
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)));
}
Loading

0 comments on commit fddcd31

Please sign in to comment.