Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Variances Debug #655

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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