Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bd7c14f

Browse files
authoredOct 28, 2023
Rollup merge of rust-lang#117325 - Nilstrieb:pretty-macros, r=compiler-errors
Small ty::print cleanups
2 parents ba1069e + 4e2bbfe commit bd7c14f

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed
 

‎compiler/rustc_middle/src/ty/print/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ pub use self::pretty::*;
1212

1313
pub type PrintError = std::fmt::Error;
1414

15-
// FIXME(eddyb) false positive, the lifetime parameters are used with `P: Printer<...>`.
16-
#[allow(unused_lifetimes)]
1715
pub trait Print<'tcx, P> {
1816
fn print(&self, cx: &mut P) -> Result<(), PrintError>;
1917
}

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

+37-47
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ macro_rules! p {
5353
}
5454
macro_rules! define_scoped_cx {
5555
($cx:ident) => {
56-
#[allow(unused_macros)]
5756
macro_rules! scoped_cx {
5857
() => {
5958
$cx
@@ -408,8 +407,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
408407
def_id: DefId,
409408
callers: &mut Vec<DefId>,
410409
) -> Result<bool, PrintError> {
411-
define_scoped_cx!(self);
412-
413410
debug!("try_print_visible_def_path: def_id={:?}", def_id);
414411

415412
// If `def_id` is a direct or injected extern crate, return the
@@ -1868,8 +1865,6 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
18681865
def_id: DefId,
18691866
args: &'tcx [GenericArg<'tcx>],
18701867
) -> Result<(), PrintError> {
1871-
define_scoped_cx!(self);
1872-
18731868
if args.is_empty() {
18741869
match self.try_print_trimmed_def_path(def_id)? {
18751870
true => return Ok(()),
@@ -2401,8 +2396,6 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
24012396
let _ = write!(cx, "{cont}");
24022397
};
24032398

2404-
define_scoped_cx!(self);
2405-
24062399
let possible_names = ('a'..='z').rev().map(|s| Symbol::intern(&format!("'{s}")));
24072400

24082401
let mut available_names = possible_names
@@ -2630,46 +2623,6 @@ where
26302623
}
26312624
}
26322625

2633-
macro_rules! forward_display_to_print {
2634-
($($ty:ty),+) => {
2635-
// Some of the $ty arguments may not actually use 'tcx
2636-
$(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
2637-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2638-
ty::tls::with(|tcx| {
2639-
let mut cx = FmtPrinter::new(tcx, Namespace::TypeNS);
2640-
tcx.lift(*self)
2641-
.expect("could not lift for printing")
2642-
.print(&mut cx)?;
2643-
f.write_str(&cx.into_buffer())?;
2644-
Ok(())
2645-
})
2646-
}
2647-
})+
2648-
};
2649-
}
2650-
2651-
macro_rules! define_print_and_forward_display {
2652-
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
2653-
define_print!(($self, $cx): $($ty $print)*);
2654-
forward_display_to_print!($($ty),+);
2655-
};
2656-
}
2657-
2658-
macro_rules! define_print {
2659-
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
2660-
$(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty {
2661-
fn print(&$self, $cx: &mut P) -> Result<(), PrintError> {
2662-
#[allow(unused_mut)]
2663-
let mut $cx = $cx;
2664-
define_scoped_cx!($cx);
2665-
let _: () = $print;
2666-
#[allow(unreachable_code)]
2667-
Ok(())
2668-
}
2669-
})+
2670-
};
2671-
}
2672-
26732626
/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
26742627
/// the trait path. That is, it will print `Trait<U>` instead of
26752628
/// `<T as Trait<U>>`.
@@ -2744,6 +2697,43 @@ pub struct PrintClosureAsImpl<'tcx> {
27442697
pub closure: ty::ClosureArgs<'tcx>,
27452698
}
27462699

2700+
macro_rules! forward_display_to_print {
2701+
($($ty:ty),+) => {
2702+
// Some of the $ty arguments may not actually use 'tcx
2703+
$(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
2704+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2705+
ty::tls::with(|tcx| {
2706+
let mut cx = FmtPrinter::new(tcx, Namespace::TypeNS);
2707+
tcx.lift(*self)
2708+
.expect("could not lift for printing")
2709+
.print(&mut cx)?;
2710+
f.write_str(&cx.into_buffer())?;
2711+
Ok(())
2712+
})
2713+
}
2714+
})+
2715+
};
2716+
}
2717+
2718+
macro_rules! define_print {
2719+
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
2720+
$(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty {
2721+
fn print(&$self, $cx: &mut P) -> Result<(), PrintError> {
2722+
define_scoped_cx!($cx);
2723+
let _: () = $print;
2724+
Ok(())
2725+
}
2726+
})+
2727+
};
2728+
}
2729+
2730+
macro_rules! define_print_and_forward_display {
2731+
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
2732+
define_print!(($self, $cx): $($ty $print)*);
2733+
forward_display_to_print!($($ty),+);
2734+
};
2735+
}
2736+
27472737
forward_display_to_print! {
27482738
ty::Region<'tcx>,
27492739
Ty<'tcx>,

0 commit comments

Comments
 (0)
Please sign in to comment.