Skip to content

Commit 84b2491

Browse files
committed
Auto merge of #655 - flodiebold:variances-debug, r=jackh726
Make Variances Debug Necessary for e.g. putting them into a Salsa DB.
2 parents 91bf10d + a99c320 commit 84b2491

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

chalk-integration/src/interner.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::tls;
22
use chalk_ir::interner::{HasInterner, Interner};
33
use chalk_ir::{
44
AdtId, AliasTy, AssocTypeId, CanonicalVarKind, CanonicalVarKinds, ConstData, Constraint,
5-
FnDefId, Goals, InEnvironment, Lifetime, OpaqueTy, OpaqueTyId, ProgramClauseImplication,
6-
ProgramClauses, ProjectionTy, QuantifiedWhereClauses, SeparatorTraitRef, Substitution, TraitId,
7-
Ty, TyData, VariableKind, VariableKinds,
5+
Constraints, FnDefId, Goals, InEnvironment, Lifetime, OpaqueTy, OpaqueTyId,
6+
ProgramClauseImplication, ProgramClauses, ProjectionTy, QuantifiedWhereClauses,
7+
SeparatorTraitRef, Substitution, TraitId, Ty, TyData, VariableKind, VariableKinds, Variances,
88
};
99
use chalk_ir::{
1010
GenericArg, GenericArgData, Goal, GoalData, LifetimeData, ProgramClause, ProgramClauseData,
@@ -206,6 +206,20 @@ impl Interner for ChalkIr {
206206
tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt)))
207207
}
208208

209+
fn debug_constraints(
210+
constraints: &Constraints<Self>,
211+
fmt: &mut fmt::Formatter<'_>,
212+
) -> Option<fmt::Result> {
213+
tls::with_current_program(|prog| Some(prog?.debug_constraints(constraints, fmt)))
214+
}
215+
216+
fn debug_variances(
217+
variances: &Variances<Self>,
218+
fmt: &mut fmt::Formatter<'_>,
219+
) -> Option<fmt::Result> {
220+
tls::with_current_program(|prog| Some(prog?.debug_variances(variances, fmt)))
221+
}
222+
209223
fn intern_ty(&self, ty: TyData<ChalkIr>) -> Arc<TyData<ChalkIr>> {
210224
Arc::new(ty)
211225
}

chalk-integration/src/program.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,24 @@ impl tls::DebugContext for Program {
346346
let interner = self.interner();
347347
write!(fmt, "{:?}", clauses.as_slice(interner))
348348
}
349+
350+
fn debug_constraints(
351+
&self,
352+
constraints: &chalk_ir::Constraints<ChalkIr>,
353+
fmt: &mut fmt::Formatter<'_>,
354+
) -> Result<(), fmt::Error> {
355+
let interner = self.interner();
356+
write!(fmt, "{:?}", constraints.as_slice(interner))
357+
}
358+
359+
fn debug_variances(
360+
&self,
361+
variances: &chalk_ir::Variances<ChalkIr>,
362+
fmt: &mut fmt::Formatter<'_>,
363+
) -> Result<(), fmt::Error> {
364+
let interner = self.interner();
365+
write!(fmt, "{:?}", variances.as_slice(interner))
366+
}
349367
}
350368

351369
impl UnificationDatabase<ChalkIr> for Program {

chalk-integration/src/tls.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::interner::ChalkIr;
22
use chalk_ir::{
3-
debug::SeparatorTraitRef, AdtId, AliasTy, AssocTypeId, CanonicalVarKinds, FnDefId, GenericArg,
4-
Goal, Goals, Lifetime, OpaqueTy, OpaqueTyId, ProgramClause, ProgramClauseImplication,
5-
ProgramClauses, ProjectionTy, QuantifiedWhereClauses, Substitution, TraitId, Ty, VariableKinds,
3+
debug::SeparatorTraitRef, AdtId, AliasTy, AssocTypeId, CanonicalVarKinds, Constraints, FnDefId,
4+
GenericArg, Goal, Goals, Lifetime, OpaqueTy, OpaqueTyId, ProgramClause,
5+
ProgramClauseImplication, ProgramClauses, ProjectionTy, QuantifiedWhereClauses, Substitution,
6+
TraitId, Ty, VariableKinds, Variances,
67
};
78
use std::cell::RefCell;
89
use std::fmt;
@@ -140,6 +141,18 @@ pub trait DebugContext {
140141
clauses: &QuantifiedWhereClauses<ChalkIr>,
141142
fmt: &mut fmt::Formatter<'_>,
142143
) -> Result<(), fmt::Error>;
144+
145+
fn debug_constraints(
146+
&self,
147+
constraints: &Constraints<ChalkIr>,
148+
fmt: &mut fmt::Formatter<'_>,
149+
) -> Result<(), fmt::Error>;
150+
151+
fn debug_variances(
152+
&self,
153+
variances: &Variances<ChalkIr>,
154+
fmt: &mut fmt::Formatter<'_>,
155+
) -> Result<(), fmt::Error>;
143156
}
144157

145158
pub fn with_current_program<R>(op: impl FnOnce(Option<&Arc<dyn DebugContext>>) -> R) -> R {

chalk-ir/src/debug.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,3 +977,9 @@ impl<I: Interner> Debug for Substitution<I> {
977977
Display::fmt(self, fmt)
978978
}
979979
}
980+
981+
impl<I: Interner> Debug for Variances<I> {
982+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
983+
I::debug_variances(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
984+
}
985+
}

chalk-ir/src/interner.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::TyData;
3535
use crate::VariableKind;
3636
use crate::VariableKinds;
3737
use crate::Variance;
38+
use crate::Variances;
3839
use crate::{Const, ConstData};
3940
use std::fmt::{self, Debug};
4041
use std::hash::Hash;
@@ -456,6 +457,16 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
456457
None
457458
}
458459

460+
/// Prints the debug representation of a Variances.
461+
/// Returns `None` to fallback to the default debug output.
462+
#[allow(unused_variables)]
463+
fn debug_variances(
464+
variances: &Variances<Self>,
465+
fmt: &mut fmt::Formatter<'_>,
466+
) -> Option<fmt::Result> {
467+
None
468+
}
469+
459470
/// Create an "interned" type from `ty`. This is not normally
460471
/// invoked directly; instead, you invoke `TyKind::intern` (which
461472
/// will ultimately call this method).

0 commit comments

Comments
 (0)