Skip to content

Commit

Permalink
Make Variances Debug
Browse files Browse the repository at this point in the history
Necessary for e.g. putting them into a Salsa DB. Also implement TLS debug
printing for Constraints (I assume that just got overlooked).
  • Loading branch information
flodiebold committed Nov 20, 2020
1 parent e97bcf4 commit a99c320
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
20 changes: 17 additions & 3 deletions chalk-integration/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::tls;
use chalk_ir::interner::{HasInterner, Interner};
use chalk_ir::{
AdtId, AliasTy, AssocTypeId, CanonicalVarKind, CanonicalVarKinds, ConstData, Constraint,
FnDefId, Goals, InEnvironment, Lifetime, OpaqueTy, OpaqueTyId, ProgramClauseImplication,
ProgramClauses, ProjectionTy, QuantifiedWhereClauses, SeparatorTraitRef, Substitution, TraitId,
Ty, TyData, VariableKind, VariableKinds,
Constraints, FnDefId, Goals, InEnvironment, Lifetime, OpaqueTy, OpaqueTyId,
ProgramClauseImplication, ProgramClauses, ProjectionTy, QuantifiedWhereClauses,
SeparatorTraitRef, Substitution, TraitId, Ty, TyData, VariableKind, VariableKinds, Variances,
};
use chalk_ir::{
GenericArg, GenericArgData, Goal, GoalData, LifetimeData, ProgramClause, ProgramClauseData,
Expand Down Expand Up @@ -206,6 +206,20 @@ impl Interner for ChalkIr {
tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt)))
}

fn debug_constraints(
constraints: &Constraints<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_constraints(constraints, fmt)))
}

fn debug_variances(
variances: &Variances<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_variances(variances, fmt)))
}

fn intern_ty(&self, ty: TyData<ChalkIr>) -> Arc<TyData<ChalkIr>> {
Arc::new(ty)
}
Expand Down
18 changes: 18 additions & 0 deletions chalk-integration/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,24 @@ impl tls::DebugContext for Program {
let interner = self.interner();
write!(fmt, "{:?}", clauses.as_slice(interner))
}

fn debug_constraints(
&self,
constraints: &chalk_ir::Constraints<ChalkIr>,
fmt: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
let interner = self.interner();
write!(fmt, "{:?}", constraints.as_slice(interner))
}

fn debug_variances(
&self,
variances: &chalk_ir::Variances<ChalkIr>,
fmt: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
let interner = self.interner();
write!(fmt, "{:?}", variances.as_slice(interner))
}
}

impl UnificationDatabase<ChalkIr> for Program {
Expand Down
19 changes: 16 additions & 3 deletions chalk-integration/src/tls.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::interner::ChalkIr;
use chalk_ir::{
debug::SeparatorTraitRef, AdtId, AliasTy, AssocTypeId, CanonicalVarKinds, FnDefId, GenericArg,
Goal, Goals, Lifetime, OpaqueTy, OpaqueTyId, ProgramClause, ProgramClauseImplication,
ProgramClauses, ProjectionTy, QuantifiedWhereClauses, Substitution, TraitId, Ty, VariableKinds,
debug::SeparatorTraitRef, AdtId, AliasTy, AssocTypeId, CanonicalVarKinds, Constraints, FnDefId,
GenericArg, Goal, Goals, Lifetime, OpaqueTy, OpaqueTyId, ProgramClause,
ProgramClauseImplication, ProgramClauses, ProjectionTy, QuantifiedWhereClauses, Substitution,
TraitId, Ty, VariableKinds, Variances,
};
use std::cell::RefCell;
use std::fmt;
Expand Down Expand Up @@ -140,6 +141,18 @@ pub trait DebugContext {
clauses: &QuantifiedWhereClauses<ChalkIr>,
fmt: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error>;

fn debug_constraints(
&self,
constraints: &Constraints<ChalkIr>,
fmt: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error>;

fn debug_variances(
&self,
variances: &Variances<ChalkIr>,
fmt: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error>;
}

pub fn with_current_program<R>(op: impl FnOnce(Option<&Arc<dyn DebugContext>>) -> R) -> R {
Expand Down
6 changes: 6 additions & 0 deletions chalk-ir/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,3 +977,9 @@ impl<I: Interner> Debug for Substitution<I> {
Display::fmt(self, fmt)
}
}

impl<I: Interner> Debug for Variances<I> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
I::debug_variances(self, fmt).unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
}
}
11 changes: 11 additions & 0 deletions chalk-ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use crate::TyData;
use crate::VariableKind;
use crate::VariableKinds;
use crate::Variance;
use crate::Variances;
use crate::{Const, ConstData};
use std::fmt::{self, Debug};
use std::hash::Hash;
Expand Down Expand Up @@ -456,6 +457,16 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
None
}

/// Prints the debug representation of a Variances.
/// Returns `None` to fallback to the default debug output.
#[allow(unused_variables)]
fn debug_variances(
variances: &Variances<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
None
}

/// Create an "interned" type from `ty`. This is not normally
/// invoked directly; instead, you invoke `TyKind::intern` (which
/// will ultimately call this method).
Expand Down

0 comments on commit a99c320

Please sign in to comment.