diff options
Diffstat (limited to 'crates/hir_ty/src/traits/chalk/tls.rs')
-rw-r--r-- | crates/hir_ty/src/traits/chalk/tls.rs | 162 |
1 files changed, 36 insertions, 126 deletions
diff --git a/crates/hir_ty/src/traits/chalk/tls.rs b/crates/hir_ty/src/traits/chalk/tls.rs index b4568cff6..75b16172e 100644 --- a/crates/hir_ty/src/traits/chalk/tls.rs +++ b/crates/hir_ty/src/traits/chalk/tls.rs | |||
@@ -1,114 +1,32 @@ | |||
1 | //! Implementation of Chalk debug helper functions using TLS. | 1 | //! Implementation of Chalk debug helper functions using TLS. |
2 | use std::fmt; | 2 | use std::fmt; |
3 | 3 | ||
4 | use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication, TypeName}; | 4 | use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; |
5 | use itertools::Itertools; | 5 | use itertools::Itertools; |
6 | 6 | ||
7 | use super::{from_chalk, Interner, TypeAliasAsAssocType}; | 7 | use super::{from_chalk, Interner, TypeAliasAsAssocType}; |
8 | use crate::{db::HirDatabase, CallableDefId, TypeCtor}; | 8 | use crate::{db::HirDatabase, CallableDefId}; |
9 | use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId}; | 9 | use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; |
10 | 10 | ||
11 | pub use unsafe_tls::{set_current_program, with_current_program}; | 11 | pub(crate) use unsafe_tls::{set_current_program, with_current_program}; |
12 | 12 | ||
13 | pub struct DebugContext<'a>(&'a dyn HirDatabase); | 13 | pub(crate) struct DebugContext<'a>(&'a dyn HirDatabase); |
14 | 14 | ||
15 | impl DebugContext<'_> { | 15 | impl DebugContext<'_> { |
16 | pub fn debug_struct_id( | 16 | pub(crate) fn debug_struct_id( |
17 | &self, | 17 | &self, |
18 | id: super::AdtId, | 18 | id: super::AdtId, |
19 | f: &mut fmt::Formatter<'_>, | 19 | f: &mut fmt::Formatter<'_>, |
20 | ) -> Result<(), fmt::Error> { | 20 | ) -> Result<(), fmt::Error> { |
21 | let type_ctor: TypeCtor = from_chalk(self.0, TypeName::Adt(id)); | 21 | let name = match id.0 { |
22 | match type_ctor { | 22 | AdtId::StructId(it) => self.0.struct_data(it).name.clone(), |
23 | TypeCtor::Bool => write!(f, "bool")?, | 23 | AdtId::UnionId(it) => self.0.union_data(it).name.clone(), |
24 | TypeCtor::Char => write!(f, "char")?, | 24 | AdtId::EnumId(it) => self.0.enum_data(it).name.clone(), |
25 | TypeCtor::Int(t) => write!(f, "{}", t)?, | 25 | }; |
26 | TypeCtor::Float(t) => write!(f, "{}", t)?, | 26 | write!(f, "{}", name) |
27 | TypeCtor::Str => write!(f, "str")?, | ||
28 | TypeCtor::Slice => write!(f, "slice")?, | ||
29 | TypeCtor::Array => write!(f, "array")?, | ||
30 | TypeCtor::RawPtr(m) => write!(f, "*{}", m.as_keyword_for_ptr())?, | ||
31 | TypeCtor::Ref(m) => write!(f, "&{}", m.as_keyword_for_ref())?, | ||
32 | TypeCtor::Never => write!(f, "!")?, | ||
33 | TypeCtor::Tuple { .. } => { | ||
34 | write!(f, "()")?; | ||
35 | } | ||
36 | TypeCtor::FnPtr { .. } => { | ||
37 | write!(f, "fn")?; | ||
38 | } | ||
39 | TypeCtor::FnDef(def) => { | ||
40 | let name = match def { | ||
41 | CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(), | ||
42 | CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(), | ||
43 | CallableDefId::EnumVariantId(e) => { | ||
44 | let enum_data = self.0.enum_data(e.parent); | ||
45 | enum_data.variants[e.local_id].name.clone() | ||
46 | } | ||
47 | }; | ||
48 | match def { | ||
49 | CallableDefId::FunctionId(_) => write!(f, "{{fn {}}}", name)?, | ||
50 | CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => { | ||
51 | write!(f, "{{ctor {}}}", name)? | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | TypeCtor::Adt(def_id) => { | ||
56 | let name = match def_id { | ||
57 | AdtId::StructId(it) => self.0.struct_data(it).name.clone(), | ||
58 | AdtId::UnionId(it) => self.0.union_data(it).name.clone(), | ||
59 | AdtId::EnumId(it) => self.0.enum_data(it).name.clone(), | ||
60 | }; | ||
61 | write!(f, "{}", name)?; | ||
62 | } | ||
63 | TypeCtor::AssociatedType(type_alias) => { | ||
64 | let trait_ = match type_alias.lookup(self.0.upcast()).container { | ||
65 | AssocContainerId::TraitId(it) => it, | ||
66 | _ => panic!("not an associated type"), | ||
67 | }; | ||
68 | let trait_name = self.0.trait_data(trait_).name.clone(); | ||
69 | let name = self.0.type_alias_data(type_alias).name.clone(); | ||
70 | write!(f, "{}::{}", trait_name, name)?; | ||
71 | } | ||
72 | TypeCtor::OpaqueType(opaque_ty_id) => match opaque_ty_id { | ||
73 | crate::OpaqueTyId::ReturnTypeImplTrait(func, idx) => { | ||
74 | write!(f, "{{impl trait {} of {:?}}}", idx, func)?; | ||
75 | } | ||
76 | crate::OpaqueTyId::AsyncBlockTypeImplTrait(def, idx) => { | ||
77 | write!(f, "{{impl trait of async block {} of {:?}}}", idx.into_raw(), def)?; | ||
78 | } | ||
79 | }, | ||
80 | TypeCtor::ForeignType(type_alias) => { | ||
81 | let name = self.0.type_alias_data(type_alias).name.clone(); | ||
82 | write!(f, "{}", name)?; | ||
83 | } | ||
84 | TypeCtor::Closure { def, expr } => { | ||
85 | write!(f, "{{closure {:?} in ", expr.into_raw())?; | ||
86 | match def { | ||
87 | DefWithBodyId::FunctionId(func) => { | ||
88 | write!(f, "fn {}", self.0.function_data(func).name)? | ||
89 | } | ||
90 | DefWithBodyId::StaticId(s) => { | ||
91 | if let Some(name) = self.0.static_data(s).name.as_ref() { | ||
92 | write!(f, "body of static {}", name)?; | ||
93 | } else { | ||
94 | write!(f, "body of unnamed static {:?}", s)?; | ||
95 | } | ||
96 | } | ||
97 | DefWithBodyId::ConstId(c) => { | ||
98 | if let Some(name) = self.0.const_data(c).name.as_ref() { | ||
99 | write!(f, "body of const {}", name)?; | ||
100 | } else { | ||
101 | write!(f, "body of unnamed const {:?}", c)?; | ||
102 | } | ||
103 | } | ||
104 | }; | ||
105 | write!(f, "}}")?; | ||
106 | } | ||
107 | } | ||
108 | Ok(()) | ||
109 | } | 27 | } |
110 | 28 | ||
111 | pub fn debug_trait_id( | 29 | pub(crate) fn debug_trait_id( |
112 | &self, | 30 | &self, |
113 | id: super::TraitId, | 31 | id: super::TraitId, |
114 | fmt: &mut fmt::Formatter<'_>, | 32 | fmt: &mut fmt::Formatter<'_>, |
@@ -118,7 +36,7 @@ impl DebugContext<'_> { | |||
118 | write!(fmt, "{}", trait_data.name) | 36 | write!(fmt, "{}", trait_data.name) |
119 | } | 37 | } |
120 | 38 | ||
121 | pub fn debug_assoc_type_id( | 39 | pub(crate) fn debug_assoc_type_id( |
122 | &self, | 40 | &self, |
123 | id: super::AssocTypeId, | 41 | id: super::AssocTypeId, |
124 | fmt: &mut fmt::Formatter<'_>, | 42 | fmt: &mut fmt::Formatter<'_>, |
@@ -133,7 +51,7 @@ impl DebugContext<'_> { | |||
133 | write!(fmt, "{}::{}", trait_data.name, type_alias_data.name) | 51 | write!(fmt, "{}::{}", trait_data.name, type_alias_data.name) |
134 | } | 52 | } |
135 | 53 | ||
136 | pub fn debug_opaque_ty_id( | 54 | pub(crate) fn debug_opaque_ty_id( |
137 | &self, | 55 | &self, |
138 | opaque_ty_id: chalk_ir::OpaqueTyId<Interner>, | 56 | opaque_ty_id: chalk_ir::OpaqueTyId<Interner>, |
139 | fmt: &mut fmt::Formatter<'_>, | 57 | fmt: &mut fmt::Formatter<'_>, |
@@ -141,7 +59,7 @@ impl DebugContext<'_> { | |||
141 | fmt.debug_struct("OpaqueTyId").field("index", &opaque_ty_id.0).finish() | 59 | fmt.debug_struct("OpaqueTyId").field("index", &opaque_ty_id.0).finish() |
142 | } | 60 | } |
143 | 61 | ||
144 | pub fn debug_alias( | 62 | pub(crate) fn debug_alias( |
145 | &self, | 63 | &self, |
146 | alias_ty: &AliasTy<Interner>, | 64 | alias_ty: &AliasTy<Interner>, |
147 | fmt: &mut fmt::Formatter<'_>, | 65 | fmt: &mut fmt::Formatter<'_>, |
@@ -152,7 +70,7 @@ impl DebugContext<'_> { | |||
152 | } | 70 | } |
153 | } | 71 | } |
154 | 72 | ||
155 | pub fn debug_projection_ty( | 73 | pub(crate) fn debug_projection_ty( |
156 | &self, | 74 | &self, |
157 | projection_ty: &chalk_ir::ProjectionTy<Interner>, | 75 | projection_ty: &chalk_ir::ProjectionTy<Interner>, |
158 | fmt: &mut fmt::Formatter<'_>, | 76 | fmt: &mut fmt::Formatter<'_>, |
@@ -177,7 +95,7 @@ impl DebugContext<'_> { | |||
177 | write!(fmt, ">::{}", type_alias_data.name) | 95 | write!(fmt, ">::{}", type_alias_data.name) |
178 | } | 96 | } |
179 | 97 | ||
180 | pub fn debug_opaque_ty( | 98 | pub(crate) fn debug_opaque_ty( |
181 | &self, | 99 | &self, |
182 | opaque_ty: &chalk_ir::OpaqueTy<Interner>, | 100 | opaque_ty: &chalk_ir::OpaqueTy<Interner>, |
183 | fmt: &mut fmt::Formatter<'_>, | 101 | fmt: &mut fmt::Formatter<'_>, |
@@ -185,7 +103,7 @@ impl DebugContext<'_> { | |||
185 | write!(fmt, "{:?}", opaque_ty.opaque_ty_id) | 103 | write!(fmt, "{:?}", opaque_ty.opaque_ty_id) |
186 | } | 104 | } |
187 | 105 | ||
188 | pub fn debug_ty( | 106 | pub(crate) fn debug_ty( |
189 | &self, | 107 | &self, |
190 | ty: &chalk_ir::Ty<Interner>, | 108 | ty: &chalk_ir::Ty<Interner>, |
191 | fmt: &mut fmt::Formatter<'_>, | 109 | fmt: &mut fmt::Formatter<'_>, |
@@ -193,7 +111,7 @@ impl DebugContext<'_> { | |||
193 | write!(fmt, "{:?}", ty.data(&Interner)) | 111 | write!(fmt, "{:?}", ty.data(&Interner)) |
194 | } | 112 | } |
195 | 113 | ||
196 | pub fn debug_lifetime( | 114 | pub(crate) fn debug_lifetime( |
197 | &self, | 115 | &self, |
198 | lifetime: &Lifetime<Interner>, | 116 | lifetime: &Lifetime<Interner>, |
199 | fmt: &mut fmt::Formatter<'_>, | 117 | fmt: &mut fmt::Formatter<'_>, |
@@ -201,7 +119,7 @@ impl DebugContext<'_> { | |||
201 | write!(fmt, "{:?}", lifetime.data(&Interner)) | 119 | write!(fmt, "{:?}", lifetime.data(&Interner)) |
202 | } | 120 | } |
203 | 121 | ||
204 | pub fn debug_generic_arg( | 122 | pub(crate) fn debug_generic_arg( |
205 | &self, | 123 | &self, |
206 | parameter: &GenericArg<Interner>, | 124 | parameter: &GenericArg<Interner>, |
207 | fmt: &mut fmt::Formatter<'_>, | 125 | fmt: &mut fmt::Formatter<'_>, |
@@ -209,7 +127,7 @@ impl DebugContext<'_> { | |||
209 | write!(fmt, "{:?}", parameter.data(&Interner).inner_debug()) | 127 | write!(fmt, "{:?}", parameter.data(&Interner).inner_debug()) |
210 | } | 128 | } |
211 | 129 | ||
212 | pub fn debug_goal( | 130 | pub(crate) fn debug_goal( |
213 | &self, | 131 | &self, |
214 | goal: &Goal<Interner>, | 132 | goal: &Goal<Interner>, |
215 | fmt: &mut fmt::Formatter<'_>, | 133 | fmt: &mut fmt::Formatter<'_>, |
@@ -218,7 +136,7 @@ impl DebugContext<'_> { | |||
218 | write!(fmt, "{:?}", goal_data) | 136 | write!(fmt, "{:?}", goal_data) |
219 | } | 137 | } |
220 | 138 | ||
221 | pub fn debug_goals( | 139 | pub(crate) fn debug_goals( |
222 | &self, | 140 | &self, |
223 | goals: &Goals<Interner>, | 141 | goals: &Goals<Interner>, |
224 | fmt: &mut fmt::Formatter<'_>, | 142 | fmt: &mut fmt::Formatter<'_>, |
@@ -226,7 +144,7 @@ impl DebugContext<'_> { | |||
226 | write!(fmt, "{:?}", goals.debug(&Interner)) | 144 | write!(fmt, "{:?}", goals.debug(&Interner)) |
227 | } | 145 | } |
228 | 146 | ||
229 | pub fn debug_program_clause_implication( | 147 | pub(crate) fn debug_program_clause_implication( |
230 | &self, | 148 | &self, |
231 | pci: &ProgramClauseImplication<Interner>, | 149 | pci: &ProgramClauseImplication<Interner>, |
232 | fmt: &mut fmt::Formatter<'_>, | 150 | fmt: &mut fmt::Formatter<'_>, |
@@ -234,15 +152,7 @@ impl DebugContext<'_> { | |||
234 | write!(fmt, "{:?}", pci.debug(&Interner)) | 152 | write!(fmt, "{:?}", pci.debug(&Interner)) |
235 | } | 153 | } |
236 | 154 | ||
237 | pub fn debug_application_ty( | 155 | pub(crate) fn debug_substitution( |
238 | &self, | ||
239 | application_ty: &chalk_ir::ApplicationTy<Interner>, | ||
240 | fmt: &mut fmt::Formatter<'_>, | ||
241 | ) -> Result<(), fmt::Error> { | ||
242 | write!(fmt, "{:?}", application_ty.debug(&Interner)) | ||
243 | } | ||
244 | |||
245 | pub fn debug_substitution( | ||
246 | &self, | 156 | &self, |
247 | substitution: &chalk_ir::Substitution<Interner>, | 157 | substitution: &chalk_ir::Substitution<Interner>, |
248 | fmt: &mut fmt::Formatter<'_>, | 158 | fmt: &mut fmt::Formatter<'_>, |
@@ -250,7 +160,7 @@ impl DebugContext<'_> { | |||
250 | write!(fmt, "{:?}", substitution.debug(&Interner)) | 160 | write!(fmt, "{:?}", substitution.debug(&Interner)) |
251 | } | 161 | } |
252 | 162 | ||
253 | pub fn debug_separator_trait_ref( | 163 | pub(crate) fn debug_separator_trait_ref( |
254 | &self, | 164 | &self, |
255 | separator_trait_ref: &chalk_ir::SeparatorTraitRef<Interner>, | 165 | separator_trait_ref: &chalk_ir::SeparatorTraitRef<Interner>, |
256 | fmt: &mut fmt::Formatter<'_>, | 166 | fmt: &mut fmt::Formatter<'_>, |
@@ -258,7 +168,7 @@ impl DebugContext<'_> { | |||
258 | write!(fmt, "{:?}", separator_trait_ref.debug(&Interner)) | 168 | write!(fmt, "{:?}", separator_trait_ref.debug(&Interner)) |
259 | } | 169 | } |
260 | 170 | ||
261 | pub fn debug_fn_def_id( | 171 | pub(crate) fn debug_fn_def_id( |
262 | &self, | 172 | &self, |
263 | fn_def_id: chalk_ir::FnDefId<Interner>, | 173 | fn_def_id: chalk_ir::FnDefId<Interner>, |
264 | fmt: &mut fmt::Formatter<'_>, | 174 | fmt: &mut fmt::Formatter<'_>, |
@@ -280,7 +190,7 @@ impl DebugContext<'_> { | |||
280 | } | 190 | } |
281 | } | 191 | } |
282 | 192 | ||
283 | pub fn debug_const( | 193 | pub(crate) fn debug_const( |
284 | &self, | 194 | &self, |
285 | _constant: &chalk_ir::Const<Interner>, | 195 | _constant: &chalk_ir::Const<Interner>, |
286 | fmt: &mut fmt::Formatter<'_>, | 196 | fmt: &mut fmt::Formatter<'_>, |
@@ -288,42 +198,42 @@ impl DebugContext<'_> { | |||
288 | write!(fmt, "const") | 198 | write!(fmt, "const") |
289 | } | 199 | } |
290 | 200 | ||
291 | pub fn debug_variable_kinds( | 201 | pub(crate) fn debug_variable_kinds( |
292 | &self, | 202 | &self, |
293 | variable_kinds: &chalk_ir::VariableKinds<Interner>, | 203 | variable_kinds: &chalk_ir::VariableKinds<Interner>, |
294 | fmt: &mut fmt::Formatter<'_>, | 204 | fmt: &mut fmt::Formatter<'_>, |
295 | ) -> fmt::Result { | 205 | ) -> fmt::Result { |
296 | write!(fmt, "{:?}", variable_kinds.as_slice(&Interner)) | 206 | write!(fmt, "{:?}", variable_kinds.as_slice(&Interner)) |
297 | } | 207 | } |
298 | pub fn debug_variable_kinds_with_angles( | 208 | pub(crate) fn debug_variable_kinds_with_angles( |
299 | &self, | 209 | &self, |
300 | variable_kinds: &chalk_ir::VariableKinds<Interner>, | 210 | variable_kinds: &chalk_ir::VariableKinds<Interner>, |
301 | fmt: &mut fmt::Formatter<'_>, | 211 | fmt: &mut fmt::Formatter<'_>, |
302 | ) -> fmt::Result { | 212 | ) -> fmt::Result { |
303 | write!(fmt, "{:?}", variable_kinds.inner_debug(&Interner)) | 213 | write!(fmt, "{:?}", variable_kinds.inner_debug(&Interner)) |
304 | } | 214 | } |
305 | pub fn debug_canonical_var_kinds( | 215 | pub(crate) fn debug_canonical_var_kinds( |
306 | &self, | 216 | &self, |
307 | canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Interner>, | 217 | canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Interner>, |
308 | fmt: &mut fmt::Formatter<'_>, | 218 | fmt: &mut fmt::Formatter<'_>, |
309 | ) -> fmt::Result { | 219 | ) -> fmt::Result { |
310 | write!(fmt, "{:?}", canonical_var_kinds.as_slice(&Interner)) | 220 | write!(fmt, "{:?}", canonical_var_kinds.as_slice(&Interner)) |
311 | } | 221 | } |
312 | pub fn debug_program_clause( | 222 | pub(crate) fn debug_program_clause( |
313 | &self, | 223 | &self, |
314 | clause: &chalk_ir::ProgramClause<Interner>, | 224 | clause: &chalk_ir::ProgramClause<Interner>, |
315 | fmt: &mut fmt::Formatter<'_>, | 225 | fmt: &mut fmt::Formatter<'_>, |
316 | ) -> fmt::Result { | 226 | ) -> fmt::Result { |
317 | write!(fmt, "{:?}", clause.data(&Interner)) | 227 | write!(fmt, "{:?}", clause.data(&Interner)) |
318 | } | 228 | } |
319 | pub fn debug_program_clauses( | 229 | pub(crate) fn debug_program_clauses( |
320 | &self, | 230 | &self, |
321 | clauses: &chalk_ir::ProgramClauses<Interner>, | 231 | clauses: &chalk_ir::ProgramClauses<Interner>, |
322 | fmt: &mut fmt::Formatter<'_>, | 232 | fmt: &mut fmt::Formatter<'_>, |
323 | ) -> fmt::Result { | 233 | ) -> fmt::Result { |
324 | write!(fmt, "{:?}", clauses.as_slice(&Interner)) | 234 | write!(fmt, "{:?}", clauses.as_slice(&Interner)) |
325 | } | 235 | } |
326 | pub fn debug_quantified_where_clauses( | 236 | pub(crate) fn debug_quantified_where_clauses( |
327 | &self, | 237 | &self, |
328 | clauses: &chalk_ir::QuantifiedWhereClauses<Interner>, | 238 | clauses: &chalk_ir::QuantifiedWhereClauses<Interner>, |
329 | fmt: &mut fmt::Formatter<'_>, | 239 | fmt: &mut fmt::Formatter<'_>, |
@@ -339,7 +249,7 @@ mod unsafe_tls { | |||
339 | 249 | ||
340 | scoped_thread_local!(static PROGRAM: DebugContext); | 250 | scoped_thread_local!(static PROGRAM: DebugContext); |
341 | 251 | ||
342 | pub fn with_current_program<R>( | 252 | pub(crate) fn with_current_program<R>( |
343 | op: impl for<'a> FnOnce(Option<&'a DebugContext<'a>>) -> R, | 253 | op: impl for<'a> FnOnce(Option<&'a DebugContext<'a>>) -> R, |
344 | ) -> R { | 254 | ) -> R { |
345 | if PROGRAM.is_set() { | 255 | if PROGRAM.is_set() { |
@@ -349,7 +259,7 @@ mod unsafe_tls { | |||
349 | } | 259 | } |
350 | } | 260 | } |
351 | 261 | ||
352 | pub fn set_current_program<OP, R>(p: &dyn HirDatabase, op: OP) -> R | 262 | pub(crate) fn set_current_program<OP, R>(p: &dyn HirDatabase, op: OP) -> R |
353 | where | 263 | where |
354 | OP: FnOnce() -> R, | 264 | OP: FnOnce() -> R, |
355 | { | 265 | { |