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