if $c:expr { Some($r:expr) } else { None } =>> $c.then(|| $r)

This commit is contained in:
Maybe Waffle 2023-02-15 11:43:41 +00:00
parent af3c8b2726
commit 8751fa1a9a
54 changed files with 159 additions and 281 deletions

View file

@ -1062,7 +1062,7 @@ impl<'hir> Map<'hir> {
}
pub fn span_if_local(self, id: DefId) -> Option<Span> {
if id.is_local() { Some(self.tcx.def_span(id)) } else { None }
id.is_local().then(|| self.tcx.def_span(id))
}
pub fn res_span(self, res: Res) -> Option<Span> {

View file

@ -1113,13 +1113,11 @@ impl<'tcx> TyCtxt<'tcx> {
ty::FnDef(_, _) => {
let sig = ret_ty.fn_sig(self);
let output = self.erase_late_bound_regions(sig.output());
if output.is_impl_trait() {
output.is_impl_trait().then(|| {
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
let fn_decl = self.hir().fn_decl_by_hir_id(hir_id).unwrap();
Some((output, fn_decl.output.span()))
} else {
None
}
(output, fn_decl.output.span())
})
}
_ => None,
}
@ -1225,13 +1223,12 @@ macro_rules! nop_lift {
impl<'a, 'tcx> Lift<'tcx> for $ty {
type Lifted = $lifted;
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
if tcx.interners.$set.contains_pointer_to(&InternedInSet(&*self.0.0)) {
tcx.interners
.$set
.contains_pointer_to(&InternedInSet(&*self.0.0))
// SAFETY: `self` is interned and therefore valid
// for the entire lifetime of the `TyCtxt`.
Some(unsafe { mem::transmute(self) })
} else {
None
}
.then(|| unsafe { mem::transmute(self) })
}
}
};
@ -1246,13 +1243,13 @@ impl<'a, 'tcx> Lift<'tcx> for &'a List<Ty<'a>> {
if self.is_empty() {
return Some(List::empty());
}
if tcx.interners.substs.contains_pointer_to(&InternedInSet(self.as_substs())) {
tcx.interners
.substs
.contains_pointer_to(&InternedInSet(self.as_substs()))
// SAFETY: `self` is interned and therefore valid
// for the entire lifetime of the `TyCtxt`.
Some(unsafe { mem::transmute::<&'a List<Ty<'a>>, &'tcx List<Ty<'tcx>>>(self) })
} else {
None
}
.then(|| unsafe { mem::transmute::<&'a List<Ty<'a>>, &'tcx List<Ty<'tcx>>>(self) })
}
}
@ -1264,11 +1261,10 @@ macro_rules! nop_list_lift {
if self.is_empty() {
return Some(List::empty());
}
if tcx.interners.$set.contains_pointer_to(&InternedInSet(self)) {
Some(unsafe { mem::transmute(self) })
} else {
None
}
tcx.interners
.$set
.contains_pointer_to(&InternedInSet(self))
.then(|| unsafe { mem::transmute(self) })
}
}
};

View file

@ -584,7 +584,7 @@ impl<'tcx> Instance<'tcx> {
/// this function returns `None`, then the MIR body does not require substitution during
/// codegen.
fn substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>> {
if self.def.has_polymorphic_mir_body() { Some(self.substs) } else { None }
self.def.has_polymorphic_mir_body().then(|| self.substs)
}
pub fn subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: &T) -> T

View file

@ -267,13 +267,11 @@ pub type SubstsRef<'tcx> = &'tcx InternalSubsts<'tcx>;
impl<'tcx> InternalSubsts<'tcx> {
/// Checks whether all elements of this list are types, if so, transmute.
pub fn try_as_type_list(&'tcx self) -> Option<&'tcx List<Ty<'tcx>>> {
if self.iter().all(|arg| matches!(arg.unpack(), GenericArgKind::Type(_))) {
self.iter().all(|arg| matches!(arg.unpack(), GenericArgKind::Type(_))).then(|| {
assert_eq!(TYPE_TAG, 0);
// SAFETY: All elements are types, see `List<Ty<'tcx>>::as_substs`.
Some(unsafe { &*(self as *const List<GenericArg<'tcx>> as *const List<Ty<'tcx>>) })
} else {
None
}
unsafe { &*(self as *const List<GenericArg<'tcx>> as *const List<Ty<'tcx>>) }
})
}
/// Interpret these substitutions as the substitutions of a closure type.