Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup rustc/ty part 2 #54743

Merged
merged 5 commits into from Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/librustc/ty/fold.rs
Expand Up @@ -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
}
Expand Down
24 changes: 11 additions & 13 deletions src/librustc/ty/mod.rs
Expand Up @@ -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")
}
}
}
Expand Down
39 changes: 18 additions & 21 deletions src/librustc/ty/relate.rs
Expand Up @@ -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))
Expand Down Expand Up @@ -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");
Expand Down
20 changes: 7 additions & 13 deletions src/librustc/ty/structural_impls.rs
Expand Up @@ -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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this can also be unified with the FnPtrShim | CloneShim arm?

Copy link
Contributor Author

@ljedrz ljedrz Oct 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it at first, but it doesn't typecheck: expected reference, found enum std::option::Option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. (One could argue that the fact that code that looks like its all the same types in fact is not, is itself another cleanup opportunity ...)

did.visit_with(visitor) || ty.visit_with(visitor)
},
ClosureOnceShim { call_once } => call_once.visit_with(visitor),
}
}
}
Expand Down
18 changes: 5 additions & 13 deletions src/librustc/ty/sty.rs
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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))
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/librustc/ty/subst.rs
Expand Up @@ -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))
})
}

Expand Down
6 changes: 2 additions & 4 deletions src/librustc/ty/trait_def.rs
Expand Up @@ -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);
}
}
}
Expand Down
40 changes: 17 additions & 23 deletions src/librustc/ty/util.rs
Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/walk.rs
Expand Up @@ -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();
Expand Down