1
Fork 0

Auto merge of #91318 - eggyal:reduce-boilerplate-around-infallible-folders, r=jackh726

Reduce boilerplate around infallible folders

Further to https://github.com/rust-lang/rust/pull/91230#issuecomment-981059666

r? `@jackh726`
This commit is contained in:
bors 2021-12-02 19:12:00 +00:00
commit acbe4443cc
43 changed files with 900 additions and 736 deletions

View file

@ -442,8 +442,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut eraser = TypeParamEraser(self, expr.span);
let needs_bound = self
.lookup_op_method(
eraser.fold_ty(lhs_ty).into_ok(),
&[eraser.fold_ty(rhs_ty).into_ok()],
eraser.fold_ty(lhs_ty),
&[eraser.fold_ty(rhs_ty)],
Op::Binary(op, is_assign),
)
.is_ok();
@ -1015,12 +1015,12 @@ impl TypeFolder<'tcx> for TypeParamEraser<'_, 'tcx> {
self.0.tcx
}
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
match ty.kind() {
ty::Param(_) => Ok(self.0.next_ty_var(TypeVariableOrigin {
ty::Param(_) => self.0.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span: self.1,
})),
}),
_ => ty.super_fold_with(self),
}
}

View file

@ -658,7 +658,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
T: TypeFoldable<'tcx>,
{
let mut resolver = Resolver::new(self.fcx, span, self.body);
let x = x.fold_with(&mut resolver).into_ok();
let x = x.fold_with(&mut resolver);
if cfg!(debug_assertions) && x.needs_infer() {
span_bug!(span.to_span(self.fcx.tcx), "writeback: `{:?}` has inference variables", x);
}
@ -749,15 +749,15 @@ impl<'tcx> TypeFolder<'tcx> for EraseEarlyRegions<'tcx> {
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
self.tcx
}
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
if ty.has_type_flags(ty::TypeFlags::HAS_POTENTIAL_FREE_REGIONS) {
ty.super_fold_with(self)
} else {
Ok(ty)
ty
}
}
fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
Ok(if let ty::ReLateBound(..) = r { r } else { self.tcx.lifetimes.re_erased })
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
if let ty::ReLateBound(..) = r { r } else { self.tcx.lifetimes.re_erased }
}
}
@ -766,7 +766,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> {
self.tcx
}
fn fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
match self.infcx.fully_resolve(t) {
Ok(t) => {
// Do not anonymize late-bound regions
@ -779,21 +779,18 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> {
debug!("Resolver::fold_ty: input type `{:?}` not fully resolvable", t);
self.report_type_error(t);
self.replaced_with_error = true;
Ok(self.tcx().ty_error())
self.tcx().ty_error()
}
}
}
fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
debug_assert!(!r.is_late_bound(), "Should not be resolving bound region.");
Ok(self.tcx.lifetimes.re_erased)
self.tcx.lifetimes.re_erased
}
fn fold_const(
&mut self,
ct: &'tcx ty::Const<'tcx>,
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
Ok(match self.infcx.fully_resolve(ct) {
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
match self.infcx.fully_resolve(ct) {
Ok(ct) => self.infcx.tcx.erase_regions(ct),
Err(_) => {
debug!("Resolver::fold_const: input const `{:?}` not fully resolvable", ct);
@ -801,7 +798,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> {
self.replaced_with_error = true;
self.tcx().const_error(ct.ty)
}
})
}
}
}

View file

@ -729,17 +729,17 @@ fn infer_placeholder_type<'a>(
self.tcx
}
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
if !self.success {
return Ok(ty);
return ty;
}
match ty.kind() {
ty::FnDef(def_id, _) => Ok(self.tcx.mk_fn_ptr(self.tcx.fn_sig(*def_id))),
ty::FnDef(def_id, _) => self.tcx.mk_fn_ptr(self.tcx.fn_sig(*def_id)),
// FIXME: non-capturing closures should also suggest a function pointer
ty::Closure(..) | ty::Generator(..) => {
self.success = false;
Ok(ty)
ty
}
_ => ty.super_fold_with(self),
}
@ -761,7 +761,7 @@ fn infer_placeholder_type<'a>(
// Suggesting unnameable types won't help.
let mut mk_nameable = MakeNameable::new(tcx);
let ty = mk_nameable.fold_ty(ty).into_ok();
let ty = mk_nameable.fold_ty(ty);
let sugg_ty = if mk_nameable.success { Some(ty) } else { None };
if let Some(sugg_ty) = sugg_ty {
err.span_suggestion(
@ -785,7 +785,7 @@ fn infer_placeholder_type<'a>(
if !ty.references_error() {
let mut mk_nameable = MakeNameable::new(tcx);
let ty = mk_nameable.fold_ty(ty).into_ok();
let ty = mk_nameable.fold_ty(ty);
let sugg_ty = if mk_nameable.success { Some(ty) } else { None };
if let Some(sugg_ty) = sugg_ty {
diag.span_suggestion(

View file

@ -71,11 +71,8 @@ fn diagnostic_hir_wf_check<'tcx>(
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) {
self.tcx.infer_ctxt().enter(|infcx| {
let mut fulfill = traits::FulfillmentContext::new();
let tcx_ty = self
.icx
.to_ty(ty)
.fold_with(&mut EraseAllBoundRegions { tcx: self.tcx })
.into_ok();
let tcx_ty =
self.icx.to_ty(ty).fold_with(&mut EraseAllBoundRegions { tcx: self.tcx });
let cause = traits::ObligationCause::new(
ty.span,
self.hir_id,
@ -186,7 +183,7 @@ impl<'tcx> TypeFolder<'tcx> for EraseAllBoundRegions<'tcx> {
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
self.tcx
}
fn fold_region(&mut self, r: Region<'tcx>) -> Result<Region<'tcx>, Self::Error> {
if let ty::ReLateBound(..) = r { Ok(&ty::ReErased) } else { Ok(r) }
fn fold_region(&mut self, r: Region<'tcx>) -> Region<'tcx> {
if let ty::ReLateBound(..) = r { &ty::ReErased } else { r }
}
}

View file

@ -70,7 +70,6 @@ This API is completely unstable and subject to change.
#![feature(slice_partition_dedup)]
#![feature(control_flow_enum)]
#![feature(hash_drain_filter)]
#![feature(unwrap_infallible)]
#![recursion_limit = "256"]
#[macro_use]