Skip to content

Commit 35e5123

Browse files
committed
rustc: support overriding type printing in ty::print::Printer.
1 parent 88d96b2 commit 35e5123

File tree

5 files changed

+295
-240
lines changed

5 files changed

+295
-240
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
458458

459459
type Path = Vec<String>;
460460
type Region = !;
461+
type Type = !;
461462

462463
fn print_region(
463464
self: PrintCx<'_, '_, '_, Self>,
@@ -466,6 +467,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
466467
Err(NonTrivialPath)
467468
}
468469

470+
fn print_type<'tcx>(
471+
self: PrintCx<'_, '_, 'tcx, Self>,
472+
_ty: Ty<'tcx>,
473+
) -> Result<Self::Type, Self::Error> {
474+
Err(NonTrivialPath)
475+
}
476+
469477
fn path_crate(
470478
self: PrintCx<'_, '_, '_, Self>,
471479
cnum: CrateNum,

src/librustc/ty/print.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ pub trait Printer: Sized {
252252

253253
type Path;
254254
type Region;
255+
type Type;
255256

256257
fn print_def_path(
257258
self: PrintCx<'_, '_, 'tcx, Self>,
@@ -278,6 +279,11 @@ pub trait Printer: Sized {
278279
region: ty::Region<'_>,
279280
) -> Result<Self::Region, Self::Error>;
280281

282+
fn print_type(
283+
self: PrintCx<'_, '_, 'tcx, Self>,
284+
ty: Ty<'tcx>,
285+
) -> Result<Self::Type, Self::Error>;
286+
281287
fn path_crate(
282288
self: PrintCx<'_, '_, '_, Self>,
283289
cnum: CrateNum,
@@ -317,7 +323,15 @@ pub trait Printer: Sized {
317323
}
318324

319325
/// Trait for printers that pretty-print using `fmt::Write` to the printer.
320-
pub trait PrettyPrinter: Printer<Error = fmt::Error, Path = Self, Region = Self> + fmt::Write {
326+
pub trait PrettyPrinter:
327+
Printer<
328+
Error = fmt::Error,
329+
Path = Self,
330+
Region = Self,
331+
Type = Self,
332+
> +
333+
fmt::Write
334+
{
321335
/// Enter a nested print context, for pretty-printing
322336
/// nested components in some larger context.
323337
fn nest<'a, 'gcx, 'tcx, E>(
@@ -870,8 +884,8 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
870884
nest!(self, |cx| ty.print_display(cx));
871885
}
872886
UnpackedKind::Const(ct) => {
873-
start_or_continue(self, start, ", ")?;
874-
ct.print_display(self)?;
887+
start_or_continue(&mut self, start, ", ")?;
888+
nest!(self, |cx| ct.print_display(cx));
875889
}
876890
}
877891
}
@@ -901,6 +915,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
901915

902916
type Path = Self;
903917
type Region = Self;
918+
type Type = Self;
904919

905920
fn print_def_path(
906921
mut self: PrintCx<'_, '_, 'tcx, Self>,
@@ -1031,6 +1046,13 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
10311046
Ok(self.printer)
10321047
}
10331048

1049+
fn print_type(
1050+
self: PrintCx<'_, '_, 'tcx, Self>,
1051+
ty: Ty<'tcx>,
1052+
) -> Result<Self::Type, Self::Error> {
1053+
self.pretty_print_type(ty)
1054+
}
1055+
10341056
fn path_crate(
10351057
mut self: PrintCx<'_, '_, '_, Self>,
10361058
cnum: CrateNum,

0 commit comments

Comments
 (0)