rustc_type_ir/
ir_print.rs1use std::fmt;
2
3use crate::{
4 AliasTerm, AliasTy, Binder, ClosureKind, CoercePredicate, ExistentialProjection,
5 ExistentialTraitRef, FnSig, HostEffectPredicate, Interner, NormalizesTo, OutlivesPredicate,
6 PatternKind, ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef, UnevaluatedConst,
7};
8
9pub trait IrPrint<T> {
10 fn print(t: &T, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
11 fn print_debug(t: &T, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
12}
13
14macro_rules! define_display_via_print {
15 ($($ty:ident),+ $(,)?) => {
16 $(
17 impl<I: Interner> fmt::Display for $ty<I> {
18 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
19 <I as IrPrint<$ty<I>>>::print(self, fmt)
20 }
21 }
22 )*
23 }
24}
25
26impl<I: Interner, T> fmt::Display for Binder<I, T>
27where
28 I: IrPrint<Binder<I, T>>,
29{
30 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
31 <I as IrPrint<Binder<I, T>>>::print(self, fmt)
32 }
33}
34
35macro_rules! define_debug_via_print {
36 ($($ty:ident),+ $(,)?) => {
37 $(
38 impl<I: Interner> fmt::Debug for $ty<I> {
39 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
40 <I as IrPrint<$ty<I>>>::print_debug(self, fmt)
41 }
42 }
43 )*
44 }
45}
46
47define_display_via_print!(
48 TraitRef,
49 TraitPredicate,
50 ExistentialTraitRef,
51 ExistentialProjection,
52 ProjectionPredicate,
53 NormalizesTo,
54 SubtypePredicate,
55 CoercePredicate,
56 HostEffectPredicate,
57 AliasTy,
58 AliasTerm,
59 FnSig,
60 PatternKind,
61);
62
63define_debug_via_print!(TraitRef, ExistentialTraitRef, PatternKind);
64
65impl<I: Interner, T> fmt::Display for OutlivesPredicate<I, T>
66where
67 I: IrPrint<OutlivesPredicate<I, T>>,
68{
69 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
70 <I as IrPrint<OutlivesPredicate<I, T>>>::print(self, fmt)
71 }
72}
73
74#[cfg(feature = "nightly")]
75mod into_diag_arg_impls {
76 use rustc_error_messages::{DiagArgValue, IntoDiagArg};
77
78 use super::*;
79
80 impl<I: Interner> IntoDiagArg for TraitRef<I> {
81 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
82 self.to_string().into_diag_arg(path)
83 }
84 }
85
86 impl<I: Interner> IntoDiagArg for ExistentialTraitRef<I> {
87 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
88 self.to_string().into_diag_arg(path)
89 }
90 }
91
92 impl<I: Interner> IntoDiagArg for UnevaluatedConst<I> {
93 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
94 format!("{self:?}").into_diag_arg(path)
95 }
96 }
97
98 impl<I: Interner> IntoDiagArg for FnSig<I> {
99 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
100 format!("{self:?}").into_diag_arg(path)
101 }
102 }
103
104 impl<I: Interner, T: IntoDiagArg> IntoDiagArg for Binder<I, T> {
105 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
106 self.skip_binder().into_diag_arg(path)
107 }
108 }
109
110 impl IntoDiagArg for ClosureKind {
111 fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
112 DiagArgValue::Str(self.as_str().into())
113 }
114 }
115}