Skip to main content

rustc_type_ir/
ir_print.rs

1use std::fmt;
2
3#[cfg(feature = "nightly")]
4use crate::{AliasConst, ClosureKind};
5use crate::{
6    AliasTerm, AliasTy, Binder, CoercePredicate, ExistentialProjection, ExistentialTraitRef, FnSig,
7    HostEffectPredicate, Interner, NormalizesTo, OutlivesPredicate, PatternKind, Placeholder,
8    ProjectionPredicate, Region, SubtypePredicate, TraitPredicate, TraitRef,
9};
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> fmt::Display for Region<I>
59where
60    I: IrPrint<Region<I>>,
61{
62    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
63        <I as IrPrint<Region<I>>>::print(self, fmt)
64    }
65}
66
67impl<I: Interner, T> fmt::Display for OutlivesPredicate<I, T>
68where
69    I: IrPrint<OutlivesPredicate<I, T>>,
70{
71    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
72        <I as IrPrint<OutlivesPredicate<I, T>>>::print(self, fmt)
73    }
74}
75
76impl<I: Interner, T> fmt::Display for Binder<I, T>
77where
78    I: IrPrint<Binder<I, T>>,
79{
80    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
81        <I as IrPrint<Binder<I, T>>>::print(self, fmt)
82    }
83}
84
85impl<I: Interner, T> fmt::Display for Placeholder<I, T>
86where
87    I: IrPrint<Placeholder<I, T>>,
88{
89    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
90        <I as IrPrint<Placeholder<I, T>>>::print(self, fmt)
91    }
92}
93
94#[cfg(feature = "nightly")]
95mod into_diag_arg_impls {
96    use rustc_error_messages::{DiagArgValue, IntoDiagArg};
97
98    use super::*;
99
100    impl<I: Interner> IntoDiagArg for TraitRef<I> {
101        fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
102            self.to_string().into_diag_arg(path)
103        }
104    }
105
106    impl<I: Interner> IntoDiagArg for ExistentialTraitRef<I> {
107        fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
108            self.to_string().into_diag_arg(path)
109        }
110    }
111
112    impl<I: Interner + IrPrint<Region<I>>> IntoDiagArg for Region<I> {
113        fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
114            self.to_string().into_diag_arg(path)
115        }
116    }
117
118    impl<I: Interner> IntoDiagArg for AliasConst<I> {
119        fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
120            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", self))
    })format!("{self:?}").into_diag_arg(path)
121        }
122    }
123
124    impl<I: Interner> IntoDiagArg for FnSig<I> {
125        fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
126            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", self))
    })format!("{self:?}").into_diag_arg(path)
127        }
128    }
129
130    impl<I: Interner, T: IntoDiagArg> IntoDiagArg for Binder<I, T> {
131        fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
132            self.skip_binder().into_diag_arg(path)
133        }
134    }
135
136    impl IntoDiagArg for ClosureKind {
137        fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
138            DiagArgValue::Str(self.as_str().into())
139        }
140    }
141}