]>
Commit | Line | Data |
---|---|---|
f9f354fc XL |
1 | //! Types required for Chalk-related queries |
2 | //! | |
3 | //! The primary purpose of this file is defining an implementation for the | |
4 | //! `chalk_ir::interner::Interner` trait. The primary purpose of this trait, as | |
5 | //! its name suggest, is to provide an abstraction boundary for creating | |
6 | //! interned Chalk types. | |
7 | ||
1b1a35ee | 8 | use rustc_middle::ty::{self, AdtDef, TyCtxt}; |
f9f354fc XL |
9 | |
10 | use rustc_hir::def_id::DefId; | |
f035d41b | 11 | use rustc_target::spec::abi::Abi; |
f9f354fc | 12 | |
f9f354fc XL |
13 | use std::cmp::Ordering; |
14 | use std::fmt; | |
15 | use std::hash::{Hash, Hasher}; | |
16 | ||
f9f354fc XL |
17 | #[derive(Copy, Clone)] |
18 | pub struct RustInterner<'tcx> { | |
19 | pub tcx: TyCtxt<'tcx>, | |
20 | } | |
21 | ||
22 | /// We don't ever actually need this. It's only required for derives. | |
23 | impl<'tcx> Hash for RustInterner<'tcx> { | |
24 | fn hash<H: Hasher>(&self, _state: &mut H) {} | |
25 | } | |
26 | ||
27 | /// We don't ever actually need this. It's only required for derives. | |
28 | impl<'tcx> Ord for RustInterner<'tcx> { | |
29 | fn cmp(&self, _other: &Self) -> Ordering { | |
30 | Ordering::Equal | |
31 | } | |
32 | } | |
33 | ||
34 | /// We don't ever actually need this. It's only required for derives. | |
35 | impl<'tcx> PartialOrd for RustInterner<'tcx> { | |
36 | fn partial_cmp(&self, _other: &Self) -> Option<Ordering> { | |
37 | None | |
38 | } | |
39 | } | |
40 | ||
41 | /// We don't ever actually need this. It's only required for derives. | |
42 | impl<'tcx> PartialEq for RustInterner<'tcx> { | |
43 | fn eq(&self, _other: &Self) -> bool { | |
44 | false | |
45 | } | |
46 | } | |
47 | ||
48 | /// We don't ever actually need this. It's only required for derives. | |
49 | impl<'tcx> Eq for RustInterner<'tcx> {} | |
50 | ||
51 | impl fmt::Debug for RustInterner<'_> { | |
52 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
53 | write!(f, "RustInterner") | |
54 | } | |
55 | } | |
56 | ||
57 | // Right now, there is no interning at all. I was running into problems with | |
58 | // adding interning in `ty/context.rs` for Chalk types with | |
59 | // `parallel-compiler = true`. -jackh726 | |
60 | impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { | |
61 | type InternedType = Box<chalk_ir::TyData<Self>>; | |
62 | type InternedLifetime = Box<chalk_ir::LifetimeData<Self>>; | |
f035d41b | 63 | type InternedConst = Box<chalk_ir::ConstData<Self>>; |
923072b8 | 64 | type InternedConcreteConst = ty::ValTree<'tcx>; |
f035d41b | 65 | type InternedGenericArg = Box<chalk_ir::GenericArgData<Self>>; |
f9f354fc XL |
66 | type InternedGoal = Box<chalk_ir::GoalData<Self>>; |
67 | type InternedGoals = Vec<chalk_ir::Goal<Self>>; | |
f035d41b | 68 | type InternedSubstitution = Vec<chalk_ir::GenericArg<Self>>; |
f9f354fc XL |
69 | type InternedProgramClause = Box<chalk_ir::ProgramClauseData<Self>>; |
70 | type InternedProgramClauses = Vec<chalk_ir::ProgramClause<Self>>; | |
71 | type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>; | |
f035d41b XL |
72 | type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>; |
73 | type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; | |
5869c6ff | 74 | type InternedVariances = Vec<chalk_ir::Variance>; |
1b1a35ee | 75 | type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>; |
f035d41b | 76 | type DefId = DefId; |
5e7ed085 | 77 | type InternedAdtId = AdtDef<'tcx>; |
f9f354fc | 78 | type Identifier = (); |
f035d41b | 79 | type FnAbi = Abi; |
f9f354fc XL |
80 | |
81 | fn debug_program_clause_implication( | |
82 | pci: &chalk_ir::ProgramClauseImplication<Self>, | |
83 | fmt: &mut fmt::Formatter<'_>, | |
84 | ) -> Option<fmt::Result> { | |
85 | let mut write = || { | |
86 | write!(fmt, "{:?}", pci.consequence)?; | |
87 | ||
88 | let conditions = pci.conditions.interned(); | |
5869c6ff | 89 | let constraints = pci.constraints.interned(); |
f9f354fc XL |
90 | |
91 | let conds = conditions.len(); | |
5869c6ff XL |
92 | let consts = constraints.len(); |
93 | if conds == 0 && consts == 0 { | |
f9f354fc XL |
94 | return Ok(()); |
95 | } | |
96 | ||
97 | write!(fmt, " :- ")?; | |
5869c6ff XL |
98 | |
99 | if conds != 0 { | |
100 | for cond in &conditions[..conds - 1] { | |
101 | write!(fmt, "{:?}, ", cond)?; | |
102 | } | |
103 | write!(fmt, "{:?}", conditions[conds - 1])?; | |
104 | } | |
105 | ||
106 | if conds != 0 && consts != 0 { | |
107 | write!(fmt, " ; ")?; | |
f9f354fc | 108 | } |
5869c6ff XL |
109 | |
110 | if consts != 0 { | |
111 | for constraint in &constraints[..consts - 1] { | |
112 | write!(fmt, "{:?}, ", constraint)?; | |
113 | } | |
114 | write!(fmt, "{:?}", constraints[consts - 1])?; | |
115 | } | |
116 | ||
f9f354fc XL |
117 | Ok(()) |
118 | }; | |
119 | Some(write()) | |
120 | } | |
121 | ||
f9f354fc XL |
122 | fn debug_substitution( |
123 | substitution: &chalk_ir::Substitution<Self>, | |
124 | fmt: &mut fmt::Formatter<'_>, | |
125 | ) -> Option<fmt::Result> { | |
126 | Some(write!(fmt, "{:?}", substitution.interned())) | |
127 | } | |
128 | ||
129 | fn debug_separator_trait_ref( | |
130 | separator_trait_ref: &chalk_ir::SeparatorTraitRef<'_, Self>, | |
131 | fmt: &mut fmt::Formatter<'_>, | |
132 | ) -> Option<fmt::Result> { | |
133 | let substitution = &separator_trait_ref.trait_ref.substitution; | |
134 | let parameters = substitution.interned(); | |
135 | Some(write!( | |
136 | fmt, | |
137 | "{:?}{}{:?}{:?}", | |
138 | parameters[0], | |
139 | separator_trait_ref.separator, | |
140 | separator_trait_ref.trait_ref.trait_id, | |
141 | chalk_ir::debug::Angle(¶meters[1..]) | |
142 | )) | |
143 | } | |
144 | ||
145 | fn debug_quantified_where_clauses( | |
146 | clauses: &chalk_ir::QuantifiedWhereClauses<Self>, | |
147 | fmt: &mut fmt::Formatter<'_>, | |
148 | ) -> Option<fmt::Result> { | |
149 | Some(write!(fmt, "{:?}", clauses.interned())) | |
150 | } | |
151 | ||
29967ef6 XL |
152 | fn debug_ty(ty: &chalk_ir::Ty<Self>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { |
153 | match &ty.interned().kind { | |
154 | chalk_ir::TyKind::Ref(chalk_ir::Mutability::Not, lifetime, ty) => { | |
155 | Some(write!(fmt, "(&{:?} {:?})", lifetime, ty)) | |
156 | } | |
157 | chalk_ir::TyKind::Ref(chalk_ir::Mutability::Mut, lifetime, ty) => { | |
158 | Some(write!(fmt, "(&{:?} mut {:?})", lifetime, ty)) | |
159 | } | |
160 | chalk_ir::TyKind::Array(ty, len) => Some(write!(fmt, "[{:?}; {:?}]", ty, len)), | |
161 | chalk_ir::TyKind::Slice(ty) => Some(write!(fmt, "[{:?}]", ty)), | |
162 | chalk_ir::TyKind::Tuple(len, substs) => Some((|| { | |
163 | write!(fmt, "(")?; | |
164 | for (idx, substitution) in substs.interned().iter().enumerate() { | |
165 | if idx == *len && *len != 1 { | |
166 | // Don't add a trailing comma if the tuple has more than one element | |
167 | write!(fmt, "{:?}", substitution)?; | |
168 | } else { | |
169 | write!(fmt, "{:?},", substitution)?; | |
170 | } | |
171 | } | |
172 | write!(fmt, ")") | |
173 | })()), | |
174 | _ => None, | |
175 | } | |
176 | } | |
177 | ||
f9f354fc XL |
178 | fn debug_alias( |
179 | alias_ty: &chalk_ir::AliasTy<Self>, | |
180 | fmt: &mut fmt::Formatter<'_>, | |
181 | ) -> Option<fmt::Result> { | |
182 | match alias_ty { | |
183 | chalk_ir::AliasTy::Projection(projection_ty) => { | |
184 | Self::debug_projection_ty(projection_ty, fmt) | |
185 | } | |
186 | chalk_ir::AliasTy::Opaque(opaque_ty) => Self::debug_opaque_ty(opaque_ty, fmt), | |
187 | } | |
188 | } | |
189 | ||
190 | fn debug_projection_ty( | |
191 | projection_ty: &chalk_ir::ProjectionTy<Self>, | |
192 | fmt: &mut fmt::Formatter<'_>, | |
193 | ) -> Option<fmt::Result> { | |
194 | Some(write!( | |
195 | fmt, | |
196 | "projection: {:?} {:?}", | |
197 | projection_ty.associated_ty_id, projection_ty.substitution, | |
198 | )) | |
199 | } | |
200 | ||
201 | fn debug_opaque_ty( | |
202 | opaque_ty: &chalk_ir::OpaqueTy<Self>, | |
203 | fmt: &mut fmt::Formatter<'_>, | |
204 | ) -> Option<fmt::Result> { | |
205 | Some(write!(fmt, "{:?}", opaque_ty.opaque_ty_id)) | |
206 | } | |
207 | ||
a2a8927a XL |
208 | fn intern_ty(self, ty: chalk_ir::TyKind<Self>) -> Self::InternedType { |
209 | let flags = ty.compute_flags(self); | |
210 | Box::new(chalk_ir::TyData { kind: ty, flags: flags }) | |
f9f354fc XL |
211 | } |
212 | ||
a2a8927a | 213 | fn ty_data<'a>(self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData<Self> { |
f9f354fc XL |
214 | ty |
215 | } | |
216 | ||
a2a8927a | 217 | fn intern_lifetime(self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime { |
f9f354fc XL |
218 | Box::new(lifetime) |
219 | } | |
220 | ||
221 | fn lifetime_data<'a>( | |
a2a8927a | 222 | self, |
f9f354fc XL |
223 | lifetime: &'a Self::InternedLifetime, |
224 | ) -> &'a chalk_ir::LifetimeData<Self> { | |
225 | &lifetime | |
226 | } | |
227 | ||
a2a8927a | 228 | fn intern_const(self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst { |
f035d41b XL |
229 | Box::new(constant) |
230 | } | |
231 | ||
a2a8927a | 232 | fn const_data<'a>(self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData<Self> { |
f035d41b XL |
233 | &constant |
234 | } | |
235 | ||
236 | fn const_eq( | |
a2a8927a | 237 | self, |
f035d41b XL |
238 | _ty: &Self::InternedType, |
239 | c1: &Self::InternedConcreteConst, | |
240 | c2: &Self::InternedConcreteConst, | |
241 | ) -> bool { | |
242 | c1 == c2 | |
243 | } | |
244 | ||
a2a8927a | 245 | fn intern_generic_arg(self, data: chalk_ir::GenericArgData<Self>) -> Self::InternedGenericArg { |
f035d41b | 246 | Box::new(data) |
f9f354fc XL |
247 | } |
248 | ||
f035d41b | 249 | fn generic_arg_data<'a>( |
a2a8927a | 250 | self, |
f035d41b XL |
251 | data: &'a Self::InternedGenericArg, |
252 | ) -> &'a chalk_ir::GenericArgData<Self> { | |
253 | &data | |
f9f354fc XL |
254 | } |
255 | ||
a2a8927a | 256 | fn intern_goal(self, goal: chalk_ir::GoalData<Self>) -> Self::InternedGoal { |
f9f354fc XL |
257 | Box::new(goal) |
258 | } | |
259 | ||
a2a8927a | 260 | fn goal_data<'a>(self, goal: &'a Self::InternedGoal) -> &'a chalk_ir::GoalData<Self> { |
f9f354fc XL |
261 | &goal |
262 | } | |
263 | ||
264 | fn intern_goals<E>( | |
a2a8927a | 265 | self, |
f9f354fc XL |
266 | data: impl IntoIterator<Item = Result<chalk_ir::Goal<Self>, E>>, |
267 | ) -> Result<Self::InternedGoals, E> { | |
268 | data.into_iter().collect::<Result<Vec<_>, _>>() | |
269 | } | |
270 | ||
a2a8927a | 271 | fn goals_data<'a>(self, goals: &'a Self::InternedGoals) -> &'a [chalk_ir::Goal<Self>] { |
f9f354fc XL |
272 | goals |
273 | } | |
274 | ||
275 | fn intern_substitution<E>( | |
a2a8927a | 276 | self, |
f035d41b | 277 | data: impl IntoIterator<Item = Result<chalk_ir::GenericArg<Self>, E>>, |
f9f354fc XL |
278 | ) -> Result<Self::InternedSubstitution, E> { |
279 | data.into_iter().collect::<Result<Vec<_>, _>>() | |
280 | } | |
281 | ||
282 | fn substitution_data<'a>( | |
a2a8927a | 283 | self, |
f9f354fc | 284 | substitution: &'a Self::InternedSubstitution, |
f035d41b | 285 | ) -> &'a [chalk_ir::GenericArg<Self>] { |
f9f354fc XL |
286 | substitution |
287 | } | |
288 | ||
289 | fn intern_program_clause( | |
a2a8927a | 290 | self, |
f9f354fc XL |
291 | data: chalk_ir::ProgramClauseData<Self>, |
292 | ) -> Self::InternedProgramClause { | |
293 | Box::new(data) | |
294 | } | |
295 | ||
296 | fn program_clause_data<'a>( | |
a2a8927a | 297 | self, |
f9f354fc XL |
298 | clause: &'a Self::InternedProgramClause, |
299 | ) -> &'a chalk_ir::ProgramClauseData<Self> { | |
300 | &clause | |
301 | } | |
302 | ||
303 | fn intern_program_clauses<E>( | |
a2a8927a | 304 | self, |
f9f354fc XL |
305 | data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, |
306 | ) -> Result<Self::InternedProgramClauses, E> { | |
307 | data.into_iter().collect::<Result<Vec<_>, _>>() | |
308 | } | |
309 | ||
310 | fn program_clauses_data<'a>( | |
a2a8927a | 311 | self, |
f9f354fc XL |
312 | clauses: &'a Self::InternedProgramClauses, |
313 | ) -> &'a [chalk_ir::ProgramClause<Self>] { | |
314 | clauses | |
315 | } | |
316 | ||
317 | fn intern_quantified_where_clauses<E>( | |
a2a8927a | 318 | self, |
f9f354fc XL |
319 | data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>, |
320 | ) -> Result<Self::InternedQuantifiedWhereClauses, E> { | |
321 | data.into_iter().collect::<Result<Vec<_>, _>>() | |
322 | } | |
323 | ||
324 | fn quantified_where_clauses_data<'a>( | |
a2a8927a | 325 | self, |
f9f354fc XL |
326 | clauses: &'a Self::InternedQuantifiedWhereClauses, |
327 | ) -> &'a [chalk_ir::QuantifiedWhereClause<Self>] { | |
328 | clauses | |
329 | } | |
330 | ||
f035d41b | 331 | fn intern_generic_arg_kinds<E>( |
a2a8927a | 332 | self, |
f035d41b XL |
333 | data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>, |
334 | ) -> Result<Self::InternedVariableKinds, E> { | |
f9f354fc XL |
335 | data.into_iter().collect::<Result<Vec<_>, _>>() |
336 | } | |
337 | ||
f035d41b | 338 | fn variable_kinds_data<'a>( |
a2a8927a | 339 | self, |
f035d41b XL |
340 | parameter_kinds: &'a Self::InternedVariableKinds, |
341 | ) -> &'a [chalk_ir::VariableKind<Self>] { | |
f9f354fc XL |
342 | parameter_kinds |
343 | } | |
344 | ||
345 | fn intern_canonical_var_kinds<E>( | |
a2a8927a | 346 | self, |
f035d41b | 347 | data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>, |
f9f354fc XL |
348 | ) -> Result<Self::InternedCanonicalVarKinds, E> { |
349 | data.into_iter().collect::<Result<Vec<_>, _>>() | |
350 | } | |
351 | ||
352 | fn canonical_var_kinds_data<'a>( | |
a2a8927a | 353 | self, |
f9f354fc | 354 | canonical_var_kinds: &'a Self::InternedCanonicalVarKinds, |
f035d41b | 355 | ) -> &'a [chalk_ir::CanonicalVarKind<Self>] { |
f9f354fc XL |
356 | canonical_var_kinds |
357 | } | |
1b1a35ee XL |
358 | |
359 | fn intern_constraints<E>( | |
a2a8927a | 360 | self, |
1b1a35ee XL |
361 | data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>, |
362 | ) -> Result<Self::InternedConstraints, E> { | |
363 | data.into_iter().collect::<Result<Vec<_>, _>>() | |
364 | } | |
365 | ||
366 | fn constraints_data<'a>( | |
a2a8927a | 367 | self, |
1b1a35ee XL |
368 | constraints: &'a Self::InternedConstraints, |
369 | ) -> &'a [chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] { | |
370 | constraints | |
371 | } | |
5869c6ff XL |
372 | |
373 | fn intern_variances<E>( | |
a2a8927a | 374 | self, |
5869c6ff XL |
375 | data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>, |
376 | ) -> Result<Self::InternedVariances, E> { | |
377 | data.into_iter().collect::<Result<Vec<_>, _>>() | |
378 | } | |
379 | ||
380 | fn variances_data<'a>( | |
a2a8927a | 381 | self, |
5869c6ff XL |
382 | variances: &'a Self::InternedVariances, |
383 | ) -> &'a [chalk_ir::Variance] { | |
384 | variances | |
385 | } | |
f9f354fc XL |
386 | } |
387 | ||
388 | impl<'tcx> chalk_ir::interner::HasInterner for RustInterner<'tcx> { | |
389 | type Interner = Self; | |
390 | } | |
391 | ||
1b1a35ee | 392 | /// A chalk environment and goal. |
064997fb | 393 | #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable, TypeFoldable, TypeVisitable)] |
f9f354fc | 394 | pub struct ChalkEnvironmentAndGoal<'tcx> { |
1b1a35ee | 395 | pub environment: &'tcx ty::List<ty::Predicate<'tcx>>, |
f9f354fc XL |
396 | pub goal: ty::Predicate<'tcx>, |
397 | } | |
398 | ||
399 | impl<'tcx> fmt::Display for ChalkEnvironmentAndGoal<'tcx> { | |
400 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
401 | write!(f, "environment: {:?}, goal: {}", self.environment, self.goal) | |
402 | } | |
403 | } |