diff options
author | Florian Diebold <[email protected]> | 2021-04-09 13:11:37 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2021-04-09 13:11:37 +0100 |
commit | 743faa21e74cc5b627935e2c4c3365807a5c722f (patch) | |
tree | 28504dbaaa7238c5d7d64d2371dc3b672cb21e16 /crates/hir_ty/src/traits/chalk/tls.rs | |
parent | 99ed68a109c9f7e0dc6a82ccb5bf854d60943957 (diff) |
Reorganize hir_ty modules
Chalk isn't really a 'traits' thing anymore, so it doesn't make sense to
have all the Chalk-related stuff in submodules of `traits`.
Diffstat (limited to 'crates/hir_ty/src/traits/chalk/tls.rs')
-rw-r--r-- | crates/hir_ty/src/traits/chalk/tls.rs | 275 |
1 files changed, 0 insertions, 275 deletions
diff --git a/crates/hir_ty/src/traits/chalk/tls.rs b/crates/hir_ty/src/traits/chalk/tls.rs deleted file mode 100644 index 8892a63a9..000000000 --- a/crates/hir_ty/src/traits/chalk/tls.rs +++ /dev/null | |||
@@ -1,275 +0,0 @@ | |||
1 | //! Implementation of Chalk debug helper functions using TLS. | ||
2 | use std::fmt; | ||
3 | |||
4 | use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; | ||
5 | use itertools::Itertools; | ||
6 | |||
7 | use super::{from_chalk, Interner}; | ||
8 | use crate::{db::HirDatabase, from_assoc_type_id, CallableDefId}; | ||
9 | use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; | ||
10 | |||
11 | pub(crate) use unsafe_tls::{set_current_program, with_current_program}; | ||
12 | |||
13 | pub(crate) struct DebugContext<'a>(&'a dyn HirDatabase); | ||
14 | |||
15 | impl DebugContext<'_> { | ||
16 | pub(crate) fn debug_struct_id( | ||
17 | &self, | ||
18 | id: super::AdtId, | ||
19 | f: &mut fmt::Formatter<'_>, | ||
20 | ) -> Result<(), fmt::Error> { | ||
21 | let name = match id.0 { | ||
22 | AdtId::StructId(it) => self.0.struct_data(it).name.clone(), | ||
23 | AdtId::UnionId(it) => self.0.union_data(it).name.clone(), | ||
24 | AdtId::EnumId(it) => self.0.enum_data(it).name.clone(), | ||
25 | }; | ||
26 | write!(f, "{}", name) | ||
27 | } | ||
28 | |||
29 | pub(crate) fn debug_trait_id( | ||
30 | &self, | ||
31 | id: super::TraitId, | ||
32 | fmt: &mut fmt::Formatter<'_>, | ||
33 | ) -> Result<(), fmt::Error> { | ||
34 | let trait_: hir_def::TraitId = from_chalk(self.0, id); | ||
35 | let trait_data = self.0.trait_data(trait_); | ||
36 | write!(fmt, "{}", trait_data.name) | ||
37 | } | ||
38 | |||
39 | pub(crate) fn debug_assoc_type_id( | ||
40 | &self, | ||
41 | id: super::AssocTypeId, | ||
42 | fmt: &mut fmt::Formatter<'_>, | ||
43 | ) -> Result<(), fmt::Error> { | ||
44 | let type_alias: TypeAliasId = from_assoc_type_id(id); | ||
45 | let type_alias_data = self.0.type_alias_data(type_alias); | ||
46 | let trait_ = match type_alias.lookup(self.0.upcast()).container { | ||
47 | AssocContainerId::TraitId(t) => t, | ||
48 | _ => panic!("associated type not in trait"), | ||
49 | }; | ||
50 | let trait_data = self.0.trait_data(trait_); | ||
51 | write!(fmt, "{}::{}", trait_data.name, type_alias_data.name) | ||
52 | } | ||
53 | |||
54 | pub(crate) fn debug_opaque_ty_id( | ||
55 | &self, | ||
56 | opaque_ty_id: chalk_ir::OpaqueTyId<Interner>, | ||
57 | fmt: &mut fmt::Formatter<'_>, | ||
58 | ) -> Result<(), fmt::Error> { | ||
59 | fmt.debug_struct("OpaqueTyId").field("index", &opaque_ty_id.0).finish() | ||
60 | } | ||
61 | |||
62 | pub(crate) fn debug_alias( | ||
63 | &self, | ||
64 | alias_ty: &AliasTy<Interner>, | ||
65 | fmt: &mut fmt::Formatter<'_>, | ||
66 | ) -> Result<(), fmt::Error> { | ||
67 | match alias_ty { | ||
68 | AliasTy::Projection(projection_ty) => self.debug_projection_ty(projection_ty, fmt), | ||
69 | AliasTy::Opaque(opaque_ty) => self.debug_opaque_ty(opaque_ty, fmt), | ||
70 | } | ||
71 | } | ||
72 | |||
73 | pub(crate) fn debug_projection_ty( | ||
74 | &self, | ||
75 | projection_ty: &chalk_ir::ProjectionTy<Interner>, | ||
76 | fmt: &mut fmt::Formatter<'_>, | ||
77 | ) -> Result<(), fmt::Error> { | ||
78 | let type_alias = from_assoc_type_id(projection_ty.associated_ty_id); | ||
79 | let type_alias_data = self.0.type_alias_data(type_alias); | ||
80 | let trait_ = match type_alias.lookup(self.0.upcast()).container { | ||
81 | AssocContainerId::TraitId(t) => t, | ||
82 | _ => panic!("associated type not in trait"), | ||
83 | }; | ||
84 | let trait_data = self.0.trait_data(trait_); | ||
85 | let params = projection_ty.substitution.as_slice(&Interner); | ||
86 | write!(fmt, "<{:?} as {}", ¶ms[0], trait_data.name,)?; | ||
87 | if params.len() > 1 { | ||
88 | write!( | ||
89 | fmt, | ||
90 | "<{}>", | ||
91 | ¶ms[1..].iter().format_with(", ", |x, f| f(&format_args!("{:?}", x))), | ||
92 | )?; | ||
93 | } | ||
94 | write!(fmt, ">::{}", type_alias_data.name) | ||
95 | } | ||
96 | |||
97 | pub(crate) fn debug_opaque_ty( | ||
98 | &self, | ||
99 | opaque_ty: &chalk_ir::OpaqueTy<Interner>, | ||
100 | fmt: &mut fmt::Formatter<'_>, | ||
101 | ) -> Result<(), fmt::Error> { | ||
102 | write!(fmt, "{:?}", opaque_ty.opaque_ty_id) | ||
103 | } | ||
104 | |||
105 | pub(crate) fn debug_ty( | ||
106 | &self, | ||
107 | ty: &chalk_ir::Ty<Interner>, | ||
108 | fmt: &mut fmt::Formatter<'_>, | ||
109 | ) -> Result<(), fmt::Error> { | ||
110 | write!(fmt, "{:?}", ty.data(&Interner)) | ||
111 | } | ||
112 | |||
113 | pub(crate) fn debug_lifetime( | ||
114 | &self, | ||
115 | lifetime: &Lifetime<Interner>, | ||
116 | fmt: &mut fmt::Formatter<'_>, | ||
117 | ) -> Result<(), fmt::Error> { | ||
118 | write!(fmt, "{:?}", lifetime.data(&Interner)) | ||
119 | } | ||
120 | |||
121 | pub(crate) fn debug_generic_arg( | ||
122 | &self, | ||
123 | parameter: &GenericArg<Interner>, | ||
124 | fmt: &mut fmt::Formatter<'_>, | ||
125 | ) -> Result<(), fmt::Error> { | ||
126 | write!(fmt, "{:?}", parameter.data(&Interner).inner_debug()) | ||
127 | } | ||
128 | |||
129 | pub(crate) fn debug_goal( | ||
130 | &self, | ||
131 | goal: &Goal<Interner>, | ||
132 | fmt: &mut fmt::Formatter<'_>, | ||
133 | ) -> Result<(), fmt::Error> { | ||
134 | let goal_data = goal.data(&Interner); | ||
135 | write!(fmt, "{:?}", goal_data) | ||
136 | } | ||
137 | |||
138 | pub(crate) fn debug_goals( | ||
139 | &self, | ||
140 | goals: &Goals<Interner>, | ||
141 | fmt: &mut fmt::Formatter<'_>, | ||
142 | ) -> Result<(), fmt::Error> { | ||
143 | write!(fmt, "{:?}", goals.debug(&Interner)) | ||
144 | } | ||
145 | |||
146 | pub(crate) fn debug_program_clause_implication( | ||
147 | &self, | ||
148 | pci: &ProgramClauseImplication<Interner>, | ||
149 | fmt: &mut fmt::Formatter<'_>, | ||
150 | ) -> Result<(), fmt::Error> { | ||
151 | write!(fmt, "{:?}", pci.debug(&Interner)) | ||
152 | } | ||
153 | |||
154 | pub(crate) fn debug_substitution( | ||
155 | &self, | ||
156 | substitution: &chalk_ir::Substitution<Interner>, | ||
157 | fmt: &mut fmt::Formatter<'_>, | ||
158 | ) -> Result<(), fmt::Error> { | ||
159 | write!(fmt, "{:?}", substitution.debug(&Interner)) | ||
160 | } | ||
161 | |||
162 | pub(crate) fn debug_separator_trait_ref( | ||
163 | &self, | ||
164 | separator_trait_ref: &chalk_ir::SeparatorTraitRef<Interner>, | ||
165 | fmt: &mut fmt::Formatter<'_>, | ||
166 | ) -> Result<(), fmt::Error> { | ||
167 | write!(fmt, "{:?}", separator_trait_ref.debug(&Interner)) | ||
168 | } | ||
169 | |||
170 | pub(crate) fn debug_fn_def_id( | ||
171 | &self, | ||
172 | fn_def_id: chalk_ir::FnDefId<Interner>, | ||
173 | fmt: &mut fmt::Formatter<'_>, | ||
174 | ) -> Result<(), fmt::Error> { | ||
175 | let def: CallableDefId = from_chalk(self.0, fn_def_id); | ||
176 | let name = match def { | ||
177 | CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(), | ||
178 | CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(), | ||
179 | CallableDefId::EnumVariantId(e) => { | ||
180 | let enum_data = self.0.enum_data(e.parent); | ||
181 | enum_data.variants[e.local_id].name.clone() | ||
182 | } | ||
183 | }; | ||
184 | match def { | ||
185 | CallableDefId::FunctionId(_) => write!(fmt, "{{fn {}}}", name), | ||
186 | CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => { | ||
187 | write!(fmt, "{{ctor {}}}", name) | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | pub(crate) fn debug_const( | ||
193 | &self, | ||
194 | _constant: &chalk_ir::Const<Interner>, | ||
195 | fmt: &mut fmt::Formatter<'_>, | ||
196 | ) -> fmt::Result { | ||
197 | write!(fmt, "const") | ||
198 | } | ||
199 | |||
200 | pub(crate) fn debug_variable_kinds( | ||
201 | &self, | ||
202 | variable_kinds: &chalk_ir::VariableKinds<Interner>, | ||
203 | fmt: &mut fmt::Formatter<'_>, | ||
204 | ) -> fmt::Result { | ||
205 | write!(fmt, "{:?}", variable_kinds.as_slice(&Interner)) | ||
206 | } | ||
207 | pub(crate) fn debug_variable_kinds_with_angles( | ||
208 | &self, | ||
209 | variable_kinds: &chalk_ir::VariableKinds<Interner>, | ||
210 | fmt: &mut fmt::Formatter<'_>, | ||
211 | ) -> fmt::Result { | ||
212 | write!(fmt, "{:?}", variable_kinds.inner_debug(&Interner)) | ||
213 | } | ||
214 | pub(crate) fn debug_canonical_var_kinds( | ||
215 | &self, | ||
216 | canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Interner>, | ||
217 | fmt: &mut fmt::Formatter<'_>, | ||
218 | ) -> fmt::Result { | ||
219 | write!(fmt, "{:?}", canonical_var_kinds.as_slice(&Interner)) | ||
220 | } | ||
221 | pub(crate) fn debug_program_clause( | ||
222 | &self, | ||
223 | clause: &chalk_ir::ProgramClause<Interner>, | ||
224 | fmt: &mut fmt::Formatter<'_>, | ||
225 | ) -> fmt::Result { | ||
226 | write!(fmt, "{:?}", clause.data(&Interner)) | ||
227 | } | ||
228 | pub(crate) fn debug_program_clauses( | ||
229 | &self, | ||
230 | clauses: &chalk_ir::ProgramClauses<Interner>, | ||
231 | fmt: &mut fmt::Formatter<'_>, | ||
232 | ) -> fmt::Result { | ||
233 | write!(fmt, "{:?}", clauses.as_slice(&Interner)) | ||
234 | } | ||
235 | pub(crate) fn debug_quantified_where_clauses( | ||
236 | &self, | ||
237 | clauses: &chalk_ir::QuantifiedWhereClauses<Interner>, | ||
238 | fmt: &mut fmt::Formatter<'_>, | ||
239 | ) -> fmt::Result { | ||
240 | write!(fmt, "{:?}", clauses.as_slice(&Interner)) | ||
241 | } | ||
242 | } | ||
243 | |||
244 | mod unsafe_tls { | ||
245 | use super::DebugContext; | ||
246 | use crate::db::HirDatabase; | ||
247 | use scoped_tls::scoped_thread_local; | ||
248 | |||
249 | scoped_thread_local!(static PROGRAM: DebugContext); | ||
250 | |||
251 | pub(crate) fn with_current_program<R>( | ||
252 | op: impl for<'a> FnOnce(Option<&'a DebugContext<'a>>) -> R, | ||
253 | ) -> R { | ||
254 | if PROGRAM.is_set() { | ||
255 | PROGRAM.with(|prog| op(Some(prog))) | ||
256 | } else { | ||
257 | op(None) | ||
258 | } | ||
259 | } | ||
260 | |||
261 | pub(crate) fn set_current_program<OP, R>(p: &dyn HirDatabase, op: OP) -> R | ||
262 | where | ||
263 | OP: FnOnce() -> R, | ||
264 | { | ||
265 | let ctx = DebugContext(p); | ||
266 | // we're transmuting the lifetime in the DebugContext to static. This is | ||
267 | // fine because we only keep the reference for the lifetime of this | ||
268 | // function, *and* the only way to access the context is through | ||
269 | // `with_current_program`, which hides the lifetime through the `for` | ||
270 | // type. | ||
271 | let static_p: &DebugContext<'static> = | ||
272 | unsafe { std::mem::transmute::<&DebugContext, &DebugContext<'static>>(&ctx) }; | ||
273 | PROGRAM.set(static_p, || op()) | ||
274 | } | ||
275 | } | ||