1
Fork 0

Rollup merge of #101019 - compiler-errors:return-closure-suggestion, r=cjgillot

Suggest returning closure as `impl Fn`

Fixes #100936
This commit is contained in:
Dylan DPC 2022-08-30 11:26:49 +05:30 committed by GitHub
commit aa9bfdee32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 106 additions and 24 deletions

View file

@ -1536,6 +1536,34 @@ pub trait PrettyPrinter<'tcx>:
}
Ok(self)
}
fn pretty_closure_as_impl(
mut self,
closure: ty::ClosureSubsts<'tcx>,
) -> Result<Self::Const, Self::Error> {
let sig = closure.sig();
let kind = closure.kind_ty().to_opt_closure_kind().unwrap_or(ty::ClosureKind::Fn);
write!(self, "impl ")?;
self.wrap_binder(&sig, |sig, mut cx| {
define_scoped_cx!(cx);
p!(print(kind), "(");
for (i, arg) in sig.inputs()[0].tuple_fields().iter().enumerate() {
if i > 0 {
p!(", ");
}
p!(print(arg));
}
p!(")");
if !sig.output().is_unit() {
p!(" -> ", print(sig.output()));
}
Ok(cx)
})
}
}
// HACK(eddyb) boxed to avoid moving around a large struct by-value.
@ -2450,6 +2478,11 @@ impl<'tcx> ty::PolyTraitPredicate<'tcx> {
}
}
#[derive(Debug, Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
pub struct PrintClosureAsImpl<'tcx> {
pub closure: ty::ClosureSubsts<'tcx>,
}
forward_display_to_print! {
ty::Region<'tcx>,
Ty<'tcx>,
@ -2542,6 +2575,10 @@ define_print_and_forward_display! {
p!(print(self.0.trait_ref.print_only_trait_path()));
}
PrintClosureAsImpl<'tcx> {
p!(pretty_closure_as_impl(self.closure))
}
ty::ParamTy {
p!(write("{}", self.name))
}

View file

@ -325,6 +325,10 @@ impl<'tcx> ClosureSubsts<'tcx> {
_ => bug!("closure_sig_as_fn_ptr_ty is not a fn-ptr: {:?}", ty.kind()),
}
}
pub fn print_as_impl_trait(self) -> ty::print::PrintClosureAsImpl<'tcx> {
ty::print::PrintClosureAsImpl { closure: self }
}
}
/// Similar to `ClosureSubsts`; see the above documentation for more.