Skip to content

Commit 90c6e2c

Browse files
committed
Implement printer for closure signature
1 parent 6bb6b81 commit 90c6e2c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,32 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
19631963
})
19641964
}
19651965

1966+
fn pretty_closure_signature(
1967+
&mut self,
1968+
closure: ty::ClosureArgs<'tcx>,
1969+
) -> Result<(), PrintError> {
1970+
let sig = closure.sig();
1971+
1972+
self.wrap_binder(&sig, |sig, cx| {
1973+
define_scoped_cx!(cx);
1974+
1975+
p!(write("fn("));
1976+
for (i, arg) in sig.inputs()[0].tuple_fields().iter().enumerate() {
1977+
if i > 0 {
1978+
p!(", ");
1979+
}
1980+
p!(print(arg));
1981+
}
1982+
p!(")");
1983+
1984+
if !sig.output().is_unit() {
1985+
p!(" -> ", print(sig.output()));
1986+
}
1987+
1988+
Ok(())
1989+
})
1990+
}
1991+
19661992
fn pretty_print_bound_constness(
19671993
&mut self,
19681994
trait_ref: ty::TraitRef<'tcx>,
@@ -2973,6 +2999,11 @@ pub struct PrintClosureAsImpl<'tcx> {
29732999
pub closure: ty::ClosureArgs<'tcx>,
29743000
}
29753001

3002+
#[derive(Debug, Copy, Clone, Lift)]
3003+
pub struct PrintClosureSignature<'tcx> {
3004+
pub closure: ty::ClosureArgs<'tcx>,
3005+
}
3006+
29763007
macro_rules! forward_display_to_print {
29773008
($($ty:ty),+) => {
29783009
// Some of the $ty arguments may not actually use 'tcx
@@ -3157,6 +3188,10 @@ define_print_and_forward_display! {
31573188
p!(pretty_closure_as_impl(self.closure))
31583189
}
31593190

3191+
PrintClosureSignature<'tcx> {
3192+
p!(pretty_closure_signature(self.closure))
3193+
}
3194+
31603195
ty::ParamTy {
31613196
p!(write("{}", self.name))
31623197
}

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ impl<'tcx> ClosureArgs<'tcx> {
266266
pub fn print_as_impl_trait(self) -> ty::print::PrintClosureAsImpl<'tcx> {
267267
ty::print::PrintClosureAsImpl { closure: self }
268268
}
269+
270+
/// Prints closure signature in form of `fn(Args) -> Output` with stripped `extern "rust-call`.
271+
pub fn print_signature(self) -> ty::print::PrintClosureSignature<'tcx> {
272+
ty::print::PrintClosureSignature { closure: self }
273+
}
269274
}
270275

271276
#[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable, Lift)]

0 commit comments

Comments
 (0)