1 //! Types required for Chalk-related queries
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.
8 use rustc_middle::ty::{self, AdtDef, TyCtxt};
10 use rustc_hir::def_id::DefId;
11 use rustc_target::spec::abi::Abi;
13 use std::cmp::Ordering;
15 use std::hash::{Hash, Hasher};
17 #[derive(Copy, Clone)]
18 pub struct RustInterner<'tcx> {
19 pub tcx: TyCtxt<'tcx>,
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) {}
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 {
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> {
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 {
48 /// We don't ever actually need this. It's only required for derives.
49 impl<'tcx> Eq for RustInterner<'tcx> {}
51 impl fmt::Debug for RustInterner<'_> {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 write!(f, "RustInterner")
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>>;
63 type InternedConst = Box<chalk_ir::ConstData<Self>>;
64 type InternedConcreteConst = ty::ValTree<'tcx>;
65 type InternedGenericArg = Box<chalk_ir::GenericArgData<Self>>;
66 type InternedGoal = Box<chalk_ir::GoalData<Self>>;
67 type InternedGoals = Vec<chalk_ir::Goal<Self>>;
68 type InternedSubstitution = Vec<chalk_ir::GenericArg<Self>>;
69 type InternedProgramClause = Box<chalk_ir::ProgramClauseData<Self>>;
70 type InternedProgramClauses = Vec<chalk_ir::ProgramClause<Self>>;
71 type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>;
72 type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>;
73 type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>;
74 type InternedVariances = Vec<chalk_ir::Variance>;
75 type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>;
77 type InternedAdtId = AdtDef<'tcx>;
81 fn debug_program_clause_implication(
82 pci: &chalk_ir::ProgramClauseImplication<Self>,
83 fmt: &mut fmt::Formatter<'_>,
84 ) -> Option<fmt::Result> {
86 write!(fmt, "{:?}", pci.consequence)?;
88 let conditions = pci.conditions.interned();
89 let constraints = pci.constraints.interned();
91 let conds = conditions.len();
92 let consts = constraints.len();
93 if conds == 0 && consts == 0 {
100 for cond in &conditions[..conds - 1] {
101 write!(fmt, "{:?}, ", cond)?;
103 write!(fmt, "{:?}", conditions[conds - 1])?;
106 if conds != 0 && consts != 0 {
111 for constraint in &constraints[..consts - 1] {
112 write!(fmt, "{:?}, ", constraint)?;
114 write!(fmt, "{:?}", constraints[consts - 1])?;
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()))
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();
139 separator_trait_ref.separator,
140 separator_trait_ref.trait_ref.trait_id,
141 chalk_ir::debug::Angle(¶meters[1..])
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()))
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))
157 chalk_ir::TyKind::Ref(chalk_ir::Mutability::Mut, lifetime, ty) => {
158 Some(write!(fmt, "(&{:?} mut {:?})", lifetime, ty))
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(
165 for (idx, substitution) in substs.interned().iter().enumerate() {
166 if idx == *len && *len != 1 {
167 // Don't add a trailing comma if the tuple has more than one element
168 write!(fmt, "{:?}", substitution)?;
170 write!(fmt, "{:?},", substitution)?;
181 alias_ty: &chalk_ir::AliasTy<Self>,
182 fmt: &mut fmt::Formatter<'_>,
183 ) -> Option<fmt::Result> {
185 chalk_ir::AliasTy::Projection(projection_ty) => {
186 Self::debug_projection_ty(projection_ty, fmt)
188 chalk_ir::AliasTy::Opaque(opaque_ty) => Self::debug_opaque_ty(opaque_ty, fmt),
192 fn debug_projection_ty(
193 projection_ty: &chalk_ir::ProjectionTy<Self>,
194 fmt: &mut fmt::Formatter<'_>,
195 ) -> Option<fmt::Result> {
198 "projection: {:?} {:?}",
199 projection_ty.associated_ty_id, projection_ty.substitution,
204 opaque_ty: &chalk_ir::OpaqueTy<Self>,
205 fmt: &mut fmt::Formatter<'_>,
206 ) -> Option<fmt::Result> {
207 Some(write!(fmt, "{:?}", opaque_ty.opaque_ty_id))
210 fn intern_ty(self, ty: chalk_ir::TyKind<Self>) -> Self::InternedType {
211 let flags = ty.compute_flags(self);
212 Box::new(chalk_ir::TyData { kind: ty, flags: flags })
215 fn ty_data(self, ty: &Self::InternedType) -> &chalk_ir::TyData<Self> {
219 fn intern_lifetime(self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime {
223 fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &chalk_ir::LifetimeData<Self> {
227 fn intern_const(self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst {
231 fn const_data(self, constant: &Self::InternedConst) -> &chalk_ir::ConstData<Self> {
237 _ty: &Self::InternedType,
238 c1: &Self::InternedConcreteConst,
239 c2: &Self::InternedConcreteConst,
244 fn intern_generic_arg(self, data: chalk_ir::GenericArgData<Self>) -> Self::InternedGenericArg {
248 fn generic_arg_data(self, data: &Self::InternedGenericArg) -> &chalk_ir::GenericArgData<Self> {
252 fn intern_goal(self, goal: chalk_ir::GoalData<Self>) -> Self::InternedGoal {
256 fn goal_data(self, goal: &Self::InternedGoal) -> &chalk_ir::GoalData<Self> {
262 data: impl IntoIterator<Item = Result<chalk_ir::Goal<Self>, E>>,
263 ) -> Result<Self::InternedGoals, E> {
264 data.into_iter().collect::<Result<Vec<_>, _>>()
267 fn goals_data(self, goals: &Self::InternedGoals) -> &[chalk_ir::Goal<Self>] {
271 fn intern_substitution<E>(
273 data: impl IntoIterator<Item = Result<chalk_ir::GenericArg<Self>, E>>,
274 ) -> Result<Self::InternedSubstitution, E> {
275 data.into_iter().collect::<Result<Vec<_>, _>>()
278 fn substitution_data(
280 substitution: &Self::InternedSubstitution,
281 ) -> &[chalk_ir::GenericArg<Self>] {
285 fn intern_program_clause(
287 data: chalk_ir::ProgramClauseData<Self>,
288 ) -> Self::InternedProgramClause {
292 fn program_clause_data(
294 clause: &Self::InternedProgramClause,
295 ) -> &chalk_ir::ProgramClauseData<Self> {
299 fn intern_program_clauses<E>(
301 data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>,
302 ) -> Result<Self::InternedProgramClauses, E> {
303 data.into_iter().collect::<Result<Vec<_>, _>>()
306 fn program_clauses_data(
308 clauses: &Self::InternedProgramClauses,
309 ) -> &[chalk_ir::ProgramClause<Self>] {
313 fn intern_quantified_where_clauses<E>(
315 data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>,
316 ) -> Result<Self::InternedQuantifiedWhereClauses, E> {
317 data.into_iter().collect::<Result<Vec<_>, _>>()
320 fn quantified_where_clauses_data(
322 clauses: &Self::InternedQuantifiedWhereClauses,
323 ) -> &[chalk_ir::QuantifiedWhereClause<Self>] {
327 fn intern_generic_arg_kinds<E>(
329 data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>,
330 ) -> Result<Self::InternedVariableKinds, E> {
331 data.into_iter().collect::<Result<Vec<_>, _>>()
334 fn variable_kinds_data(
336 parameter_kinds: &Self::InternedVariableKinds,
337 ) -> &[chalk_ir::VariableKind<Self>] {
341 fn intern_canonical_var_kinds<E>(
343 data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>,
344 ) -> Result<Self::InternedCanonicalVarKinds, E> {
345 data.into_iter().collect::<Result<Vec<_>, _>>()
348 fn canonical_var_kinds_data(
350 canonical_var_kinds: &Self::InternedCanonicalVarKinds,
351 ) -> &[chalk_ir::CanonicalVarKind<Self>] {
355 fn intern_constraints<E>(
357 data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>,
358 ) -> Result<Self::InternedConstraints, E> {
359 data.into_iter().collect::<Result<Vec<_>, _>>()
364 constraints: &Self::InternedConstraints,
365 ) -> &[chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] {
369 fn intern_variances<E>(
371 data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>,
372 ) -> Result<Self::InternedVariances, E> {
373 data.into_iter().collect::<Result<Vec<_>, _>>()
376 fn variances_data(self, variances: &Self::InternedVariances) -> &[chalk_ir::Variance] {
381 impl<'tcx> chalk_ir::interner::HasInterner for RustInterner<'tcx> {
382 type Interner = Self;
385 /// A chalk environment and goal.
386 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable, TypeFoldable, TypeVisitable)]
387 pub struct ChalkEnvironmentAndGoal<'tcx> {
388 pub environment: &'tcx ty::List<ty::Predicate<'tcx>>,
389 pub goal: ty::Predicate<'tcx>,
392 impl<'tcx> fmt::Display for ChalkEnvironmentAndGoal<'tcx> {
393 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
394 write!(f, "environment: {:?}, goal: {}", self.environment, self.goal)