rustc_type_ir/
ir_print.rs

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