Skip to main content

hir_ty/next_solver/
ir_print.rs

1//! Things related to IR printing in the next-trait-solver.
2
3use hir_def::signatures::{TraitSignature, TypeAliasSignature};
4use rustc_type_ir::{self as ty, ir_print::IrPrint};
5
6use crate::next_solver::TermId;
7
8use super::SolverDefId;
9use super::interner::DbInterner;
10
11impl<'db> IrPrint<ty::AliasTy<Self>> for DbInterner<'db> {
12    fn print(t: &ty::AliasTy<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        Self::print_debug(t, fmt)
14    }
15
16    fn print_debug(t: &ty::AliasTy<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        crate::with_attached_db(|db| match t.kind.def_id() {
18            SolverDefId::TypeAliasId(id) => fmt.write_str(&format!(
19                "AliasTy({:?}[{:?}])",
20                TypeAliasSignature::of(db, id).name.as_str(),
21                t.args
22            )),
23            SolverDefId::InternedOpaqueTyId(id) => {
24                fmt.write_str(&format!("AliasTy({:?}[{:?}])", id, t.args))
25            }
26            _ => panic!("Expected TypeAlias or OpaqueTy."),
27        })
28    }
29}
30
31impl<'db> IrPrint<ty::AliasTerm<Self>> for DbInterner<'db> {
32    fn print(t: &ty::AliasTerm<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        Self::print_debug(t, fmt)
34    }
35
36    fn print_debug(t: &ty::AliasTerm<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        crate::with_attached_db(|db| match t.def_id() {
38            SolverDefId::TypeAliasId(id) => fmt.write_str(&format!(
39                "AliasTerm({:?}[{:?}])",
40                TypeAliasSignature::of(db, id).name.as_str(),
41                t.args
42            )),
43            SolverDefId::InternedOpaqueTyId(id) => {
44                fmt.write_str(&format!("AliasTerm({:?}[{:?}])", id, t.args))
45            }
46            _ => panic!("Expected TypeAlias or OpaqueTy."),
47        })
48    }
49}
50impl<'db> IrPrint<ty::TraitRef<Self>> for DbInterner<'db> {
51    fn print(t: &ty::TraitRef<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        Self::print_debug(t, fmt)
53    }
54
55    fn print_debug(t: &ty::TraitRef<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        crate::with_attached_db(|db| {
57            let trait_ = t.def_id.0;
58            let self_ty = &t.args.as_slice()[0];
59            let trait_args = &t.args.as_slice()[1..];
60            if trait_args.is_empty() {
61                fmt.write_str(&format!(
62                    "{:?}: {}",
63                    self_ty,
64                    TraitSignature::of(db, trait_).name.as_str()
65                ))
66            } else {
67                fmt.write_str(&format!(
68                    "{:?}: {}<{:?}>",
69                    self_ty,
70                    TraitSignature::of(db, trait_).name.as_str(),
71                    trait_args
72                ))
73            }
74        })
75    }
76}
77impl<'db> IrPrint<ty::TraitPredicate<Self>> for DbInterner<'db> {
78    fn print(t: &ty::TraitPredicate<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        Self::print_debug(t, fmt)
80    }
81
82    fn print_debug(
83        t: &ty::TraitPredicate<Self>,
84        fmt: &mut std::fmt::Formatter<'_>,
85    ) -> std::fmt::Result {
86        match t.polarity {
87            ty::PredicatePolarity::Positive => write!(fmt, "{:?}", t.trait_ref),
88            ty::PredicatePolarity::Negative => write!(fmt, "!{:?}", t.trait_ref),
89        }
90    }
91}
92impl<'db> IrPrint<rustc_type_ir::HostEffectPredicate<Self>> for DbInterner<'db> {
93    fn print(
94        t: &rustc_type_ir::HostEffectPredicate<Self>,
95        fmt: &mut std::fmt::Formatter<'_>,
96    ) -> std::fmt::Result {
97        Self::print_debug(t, fmt)
98    }
99
100    fn print_debug(
101        t: &rustc_type_ir::HostEffectPredicate<Self>,
102        fmt: &mut std::fmt::Formatter<'_>,
103    ) -> std::fmt::Result {
104        let prefix = match t.constness {
105            ty::BoundConstness::Const => "const",
106            ty::BoundConstness::Maybe => "[const]",
107        };
108        write!(fmt, "{prefix} {:?}", t.trait_ref)
109    }
110}
111impl<'db> IrPrint<ty::ExistentialTraitRef<Self>> for DbInterner<'db> {
112    fn print(
113        t: &ty::ExistentialTraitRef<Self>,
114        fmt: &mut std::fmt::Formatter<'_>,
115    ) -> std::fmt::Result {
116        Self::print_debug(t, fmt)
117    }
118
119    fn print_debug(
120        t: &ty::ExistentialTraitRef<Self>,
121        fmt: &mut std::fmt::Formatter<'_>,
122    ) -> std::fmt::Result {
123        crate::with_attached_db(|db| {
124            let trait_ = t.def_id.0;
125            fmt.write_str(&format!(
126                "ExistentialTraitRef({:?}[{:?}])",
127                TraitSignature::of(db, trait_).name.as_str(),
128                t.args
129            ))
130        })
131    }
132}
133impl<'db> IrPrint<ty::ExistentialProjection<Self>> for DbInterner<'db> {
134    fn print(
135        t: &ty::ExistentialProjection<Self>,
136        fmt: &mut std::fmt::Formatter<'_>,
137    ) -> std::fmt::Result {
138        Self::print_debug(t, fmt)
139    }
140
141    fn print_debug(
142        t: &ty::ExistentialProjection<Self>,
143        fmt: &mut std::fmt::Formatter<'_>,
144    ) -> std::fmt::Result {
145        crate::with_attached_db(|db| {
146            let id = match t.def_id.0 {
147                TermId::TypeAliasId(id) => id,
148                _ => panic!("Expected trait."),
149            };
150            fmt.write_str(&format!(
151                "ExistentialProjection(({:?}[{:?}]) -> {:?})",
152                TypeAliasSignature::of(db, id).name.as_str(),
153                t.args,
154                t.term
155            ))
156        })
157    }
158}
159impl<'db> IrPrint<ty::ProjectionPredicate<Self>> for DbInterner<'db> {
160    fn print(
161        t: &ty::ProjectionPredicate<Self>,
162        fmt: &mut std::fmt::Formatter<'_>,
163    ) -> std::fmt::Result {
164        Self::print_debug(t, fmt)
165    }
166
167    fn print_debug(
168        t: &ty::ProjectionPredicate<Self>,
169        fmt: &mut std::fmt::Formatter<'_>,
170    ) -> std::fmt::Result {
171        crate::with_attached_db(|db| {
172            let id = match t.projection_term.def_id() {
173                SolverDefId::TypeAliasId(id) => id,
174                _ => panic!("Expected trait."),
175            };
176            fmt.write_str(&format!(
177                "ProjectionPredicate(({:?}[{:?}]) -> {:?})",
178                TypeAliasSignature::of(db, id).name.as_str(),
179                t.projection_term.args,
180                t.term
181            ))
182        })
183    }
184}
185impl<'db> IrPrint<ty::NormalizesTo<Self>> for DbInterner<'db> {
186    fn print(t: &ty::NormalizesTo<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
187        Self::print_debug(t, fmt)
188    }
189
190    fn print_debug(
191        t: &ty::NormalizesTo<Self>,
192        fmt: &mut std::fmt::Formatter<'_>,
193    ) -> std::fmt::Result {
194        write!(fmt, "NormalizesTo({} -> {:?})", t.alias, t.term)
195    }
196}
197impl<'db> IrPrint<ty::SubtypePredicate<Self>> for DbInterner<'db> {
198    fn print(
199        t: &ty::SubtypePredicate<Self>,
200        fmt: &mut std::fmt::Formatter<'_>,
201    ) -> std::fmt::Result {
202        Self::print_debug(t, fmt)
203    }
204
205    fn print_debug(
206        t: &ty::SubtypePredicate<Self>,
207        fmt: &mut std::fmt::Formatter<'_>,
208    ) -> std::fmt::Result {
209        write!(fmt, "{:?} <: {:?}", t.a, t.b)
210    }
211}
212impl<'db> IrPrint<ty::CoercePredicate<Self>> for DbInterner<'db> {
213    fn print(t: &ty::CoercePredicate<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
214        Self::print_debug(t, fmt)
215    }
216
217    fn print_debug(
218        t: &ty::CoercePredicate<Self>,
219        fmt: &mut std::fmt::Formatter<'_>,
220    ) -> std::fmt::Result {
221        write!(fmt, "CoercePredicate({:?} -> {:?})", t.a, t.b)
222    }
223}
224impl<'db> IrPrint<ty::FnSig<Self>> for DbInterner<'db> {
225    fn print(t: &ty::FnSig<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
226        Self::print_debug(t, fmt)
227    }
228
229    fn print_debug(t: &ty::FnSig<Self>, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
230        let tys = t.inputs_and_output.as_slice();
231        let (output, inputs) = tys.split_last().unwrap();
232        write!(fmt, "fn({:?}) -> {:?}", inputs, output)
233    }
234}
235
236impl<'db> IrPrint<rustc_type_ir::PatternKind<DbInterner<'db>>> for DbInterner<'db> {
237    fn print(
238        t: &rustc_type_ir::PatternKind<DbInterner<'db>>,
239        fmt: &mut std::fmt::Formatter<'_>,
240    ) -> std::fmt::Result {
241        Self::print_debug(t, fmt)
242    }
243
244    fn print_debug(
245        t: &rustc_type_ir::PatternKind<DbInterner<'db>>,
246        fmt: &mut std::fmt::Formatter<'_>,
247    ) -> std::fmt::Result {
248        match t {
249            ty::PatternKind::Range { start, end } => write!(fmt, "{:?}..={:?}", start, end),
250            ty::PatternKind::Or(list) => write!(fmt, "or({:?})", list),
251            ty::PatternKind::NotNull => fmt.write_str("!null"),
252        }
253    }
254}