Skip to content

Commit

Permalink
Rollup merge of #117325 - Nilstrieb:pretty-macros, r=compiler-errors
Browse files Browse the repository at this point in the history
Small ty::print cleanups
  • Loading branch information
workingjubilee authored Oct 29, 2023
2 parents 771b255 + 4e2bbfe commit 505bc85
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ pub use self::pretty::*;

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

// FIXME(eddyb) false positive, the lifetime parameters are used with `P: Printer<...>`.
#[allow(unused_lifetimes)]
pub trait Print<'tcx, P> {
fn print(&self, cx: &mut P) -> Result<(), PrintError>;
}
Expand Down
84 changes: 37 additions & 47 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ macro_rules! p {
}
macro_rules! define_scoped_cx {
($cx:ident) => {
#[allow(unused_macros)]
macro_rules! scoped_cx {
() => {
$cx
Expand Down Expand Up @@ -408,8 +407,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
def_id: DefId,
callers: &mut Vec<DefId>,
) -> Result<bool, PrintError> {
define_scoped_cx!(self);

debug!("try_print_visible_def_path: def_id={:?}", def_id);

// If `def_id` is a direct or injected extern crate, return the
Expand Down Expand Up @@ -1868,8 +1865,6 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<(), PrintError> {
define_scoped_cx!(self);

if args.is_empty() {
match self.try_print_trimmed_def_path(def_id)? {
true => return Ok(()),
Expand Down Expand Up @@ -2401,8 +2396,6 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
let _ = write!(cx, "{cont}");
};

define_scoped_cx!(self);

let possible_names = ('a'..='z').rev().map(|s| Symbol::intern(&format!("'{s}")));

let mut available_names = possible_names
Expand Down Expand Up @@ -2630,46 +2623,6 @@ where
}
}

macro_rules! forward_display_to_print {
($($ty:ty),+) => {
// Some of the $ty arguments may not actually use 'tcx
$(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
let mut cx = FmtPrinter::new(tcx, Namespace::TypeNS);
tcx.lift(*self)
.expect("could not lift for printing")
.print(&mut cx)?;
f.write_str(&cx.into_buffer())?;
Ok(())
})
}
})+
};
}

macro_rules! define_print_and_forward_display {
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
define_print!(($self, $cx): $($ty $print)*);
forward_display_to_print!($($ty),+);
};
}

macro_rules! define_print {
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
$(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty {
fn print(&$self, $cx: &mut P) -> Result<(), PrintError> {
#[allow(unused_mut)]
let mut $cx = $cx;
define_scoped_cx!($cx);
let _: () = $print;
#[allow(unreachable_code)]
Ok(())
}
})+
};
}

/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
/// the trait path. That is, it will print `Trait<U>` instead of
/// `<T as Trait<U>>`.
Expand Down Expand Up @@ -2744,6 +2697,43 @@ pub struct PrintClosureAsImpl<'tcx> {
pub closure: ty::ClosureArgs<'tcx>,
}

macro_rules! forward_display_to_print {
($($ty:ty),+) => {
// Some of the $ty arguments may not actually use 'tcx
$(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
let mut cx = FmtPrinter::new(tcx, Namespace::TypeNS);
tcx.lift(*self)
.expect("could not lift for printing")
.print(&mut cx)?;
f.write_str(&cx.into_buffer())?;
Ok(())
})
}
})+
};
}

macro_rules! define_print {
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
$(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty {
fn print(&$self, $cx: &mut P) -> Result<(), PrintError> {
define_scoped_cx!($cx);
let _: () = $print;
Ok(())
}
})+
};
}

macro_rules! define_print_and_forward_display {
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
define_print!(($self, $cx): $($ty $print)*);
forward_display_to_print!($($ty),+);
};
}

forward_display_to_print! {
ty::Region<'tcx>,
Ty<'tcx>,
Expand Down

0 comments on commit 505bc85

Please sign in to comment.