Skip to content

Commit d793117

Browse files
committed
finish intern-goals-data
1 parent a090028 commit d793117

File tree

9 files changed

+126
-54
lines changed

9 files changed

+126
-54
lines changed

chalk-integration/src/program.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use chalk_ir::debug::Angle;
44
use chalk_ir::interner::ChalkIr;
55
use chalk_ir::tls;
66
use chalk_ir::{
7-
AliasTy, AssocTypeId, Goal, GoalData, ImplId, Lifetime, Parameter, ParameterKind,
8-
ProgramClause, StructId, TraitId, Ty, TyData, TypeName,
7+
AliasTy, AssocTypeId, Goal, GoalData, Goals, ImplId, Lifetime, Parameter, ParameterKind,
8+
ProgramClause, ProgramClauseImplication, StructId, TraitId, Ty, TyData, TypeName,
99
};
1010
use chalk_rust_ir::{
1111
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ImplDatum, ImplType, StructDatum,
@@ -176,6 +176,45 @@ impl tls::DebugContext for Program {
176176
GoalData::CannotProve(()) => write!(fmt, r"¯\_(ツ)_/¯"),
177177
}
178178
}
179+
180+
fn debug_goals(
181+
&self,
182+
goals: &Goals<ChalkIr>,
183+
fmt: &mut fmt::Formatter<'_>,
184+
) -> Result<(), fmt::Error> {
185+
let interner = self.interner();
186+
write!(fmt, "(")?;
187+
for (goal, index) in goals.iter(interner).zip(0..) {
188+
if index > 0 {
189+
write!(fmt, ", ")?;
190+
}
191+
write!(fmt, "{:?}", goal)?;
192+
}
193+
write!(fmt, ")")?;
194+
Ok(())
195+
}
196+
197+
fn debug_program_clause_implication(
198+
&self,
199+
pci: &ProgramClauseImplication<ChalkIr>,
200+
fmt: &mut fmt::Formatter<'_>,
201+
) -> Result<(), fmt::Error> {
202+
let interner = self.interner();
203+
write!(fmt, "{:?}", pci.consequence)?;
204+
205+
let conditions = pci.conditions.as_slice(interner);
206+
207+
let conds = conditions.len();
208+
if conds == 0 {
209+
return Ok(());
210+
}
211+
212+
write!(fmt, " :- ")?;
213+
for cond in &conditions[..conds - 1] {
214+
write!(fmt, "{:?}, ", cond)?;
215+
}
216+
write!(fmt, "{:?}", conditions[conds - 1])
217+
}
179218
}
180219

181220
impl RustIrDatabase<ChalkIr> for Program {

chalk-ir/src/debug.rs

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ impl<I: Interner> Debug for Goal<I> {
4545
}
4646
}
4747

48+
impl<I: Interner> Debug for Goals<I> {
49+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
50+
I::debug_goals(self, fmt).unwrap_or_else(|| unimplemented!())
51+
}
52+
}
53+
54+
impl<I: Interner> Debug for ProgramClauseImplication<I> {
55+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
56+
I::debug_program_clause_implication(self, fmt).unwrap_or_else(|| unimplemented!())
57+
}
58+
}
59+
4860
impl Display for UniverseIndex {
4961
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
5062
write!(fmt, "U{}", self.counter)
@@ -269,20 +281,6 @@ impl<I: Interner> Debug for EqGoal<I> {
269281
}
270282
}
271283

272-
impl<I: Interner> Debug for Goals<I> {
273-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
274-
write!(fmt, "(")?;
275-
for (goal, index) in self.iter().zip(0..) {
276-
if index > 0 {
277-
write!(fmt, ", ")?;
278-
}
279-
write!(fmt, "{:?}", goal)?;
280-
}
281-
write!(fmt, ")")?;
282-
Ok(())
283-
}
284-
}
285-
286284
impl<T: Debug> Debug for Binders<T> {
287285
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
288286
let Binders {
@@ -315,25 +313,6 @@ impl<I: Interner> Debug for ProgramClause<I> {
315313
}
316314
}
317315

318-
impl<I: Interner> Debug for ProgramClauseImplication<I> {
319-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
320-
write!(fmt, "{:?}", self.consequence)?;
321-
322-
let conditions = self.conditions.as_slice();
323-
324-
let conds = conditions.len();
325-
if conds == 0 {
326-
return Ok(());
327-
}
328-
329-
write!(fmt, " :- ")?;
330-
for cond in &conditions[..conds - 1] {
331-
write!(fmt, "{:?}, ", cond)?;
332-
}
333-
write!(fmt, "{:?}", conditions[conds - 1])
334-
}
335-
}
336-
337316
impl<I: Interner> Debug for Environment<I> {
338317
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
339318
write!(fmt, "Env({:?})", self.clauses)

chalk-ir/src/fold/boring_impls.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ impl<I: Interner, TI: TargetInterner<I>> Fold<I, TI> for Goals<I> {
158158
I: 'i,
159159
TI: 'i,
160160
{
161-
let interner = folder.target_interner();
162-
let folded = self.iter().map(|p| p.fold_with(folder, binders));
163-
Ok(Goals::from_fallible(interner, folded)?)
161+
let interner = folder.interner();
162+
let target_interner = folder.target_interner();
163+
let folded = self.iter(interner).map(|p| p.fold_with(folder, binders));
164+
Ok(Goals::from_fallible(target_interner, folded)?)
164165
}
165166
}
166167

chalk-ir/src/interner.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use crate::AliasTy;
22
use crate::AssocTypeId;
33
use crate::Goal;
44
use crate::GoalData;
5+
use crate::Goals;
56
use crate::Lifetime;
67
use crate::LifetimeData;
78
use crate::Parameter;
89
use crate::ParameterData;
10+
use crate::ProgramClauseImplication;
911
use crate::StructId;
1012
use crate::TraitId;
1113
use crate::Ty;
@@ -176,6 +178,27 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
176178
/// if no info about current program is available from TLS).
177179
fn debug_goal(goal: &Goal<Self>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result>;
178180

181+
/// Prints the debug representation of a list of goals. To get good
182+
/// results, this requires inspecting TLS, and is difficult to
183+
/// code without reference to a specific interner (and hence
184+
/// fully known types).
185+
///
186+
/// Returns `None` to fallback to the default debug output (e.g.,
187+
/// if no info about current program is available from TLS).
188+
fn debug_goals(goals: &Goals<Self>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result>;
189+
190+
/// Prints the debug representation of a ProgramClauseImplication. To get good
191+
/// results, this requires inspecting TLS, and is difficult to
192+
/// code without reference to a specific interner (and hence
193+
/// fully known types).
194+
///
195+
/// Returns `None` to fallback to the default debug output (e.g.,
196+
/// if no info about current program is available from TLS).
197+
fn debug_program_clause_implication(
198+
pci: &ProgramClauseImplication<Self>,
199+
fmt: &mut fmt::Formatter<'_>,
200+
) -> Option<fmt::Result>;
201+
179202
/// Create an "interned" type from `ty`. This is not normally
180203
/// invoked directly; instead, you invoke `TyData::intern` (which
181204
/// will ultimately call this method).
@@ -218,7 +241,7 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
218241
fn intern_goals(&self, data: impl IntoIterator<Item = Goal<Self>>) -> Self::InternedGoals;
219242

220243
/// Lookup the `GoalsData` that was interned to create a `InternedGoals`.
221-
fn goals_data(goals: &Self::InternedGoals) -> &[Goal<Self>];
244+
fn goals_data<'a>(&self, goals: &'a Self::InternedGoals) -> &'a [Goal<Self>];
222245

223246
/// Create an "interned" substitution from `data`. This is not
224247
/// normally invoked directly; instead, you invoke
@@ -339,6 +362,20 @@ mod default {
339362
tls::with_current_program(|prog| Some(prog?.debug_goal(goal, fmt)))
340363
}
341364

365+
fn debug_goals(
366+
goals: &Goals<ChalkIr>,
367+
fmt: &mut fmt::Formatter<'_>,
368+
) -> Option<fmt::Result> {
369+
tls::with_current_program(|prog| Some(prog?.debug_goals(goals, fmt)))
370+
}
371+
372+
fn debug_program_clause_implication(
373+
pci: &ProgramClauseImplication<ChalkIr>,
374+
fmt: &mut fmt::Formatter<'_>,
375+
) -> Option<fmt::Result> {
376+
tls::with_current_program(|prog| Some(prog?.debug_program_clause_implication(pci, fmt)))
377+
}
378+
342379
fn intern_ty(&self, ty: TyData<ChalkIr>) -> Arc<TyData<ChalkIr>> {
343380
Arc::new(ty)
344381
}
@@ -384,7 +421,7 @@ mod default {
384421
data.into_iter().collect()
385422
}
386423

387-
fn goals_data(goals: &Vec<Goal<ChalkIr>>) -> &[Goal<ChalkIr>] {
424+
fn goals_data<'a>(&self, goals: &'a Vec<Goal<ChalkIr>>) -> &'a [Goal<ChalkIr>] {
388425
goals
389426
}
390427

chalk-ir/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ impl<I: Interner> ProgramClause<I> {
968968
pub fn into_from_env_clause(self, interner: &I) -> ProgramClause<I> {
969969
match self {
970970
ProgramClause::Implies(implication) => {
971-
if implication.conditions.is_empty() {
971+
if implication.conditions.is_empty(interner) {
972972
ProgramClause::Implies(ProgramClauseImplication {
973973
consequence: implication.consequence.into_from_env_goal(interner),
974974
conditions: Goals::new(interner),
@@ -1054,20 +1054,20 @@ impl<I: Interner> Goals<I> {
10541054
Ok(Goals::from(interner, goals))
10551055
}
10561056

1057-
pub fn iter(&self) -> std::slice::Iter<'_, Goal<I>> {
1058-
self.as_slice().iter()
1057+
pub fn iter(&self, interner: &I) -> std::slice::Iter<'_, Goal<I>> {
1058+
self.as_slice(interner).iter()
10591059
}
10601060

1061-
pub fn is_empty(&self) -> bool {
1062-
self.as_slice().is_empty()
1061+
pub fn is_empty(&self, interner: &I) -> bool {
1062+
self.as_slice(interner).is_empty()
10631063
}
10641064

1065-
pub fn len(&self) -> usize {
1066-
self.as_slice().len()
1065+
pub fn len(&self, interner: &I) -> usize {
1066+
self.as_slice(interner).len()
10671067
}
10681068

1069-
pub fn as_slice(&self) -> &[Goal<I>] {
1070-
I::goals_data(&self.goals)
1069+
pub fn as_slice(&self, interner: &I) -> &[Goal<I>] {
1070+
interner.goals_data(&self.goals)
10711071
}
10721072
}
10731073

@@ -1140,7 +1140,7 @@ impl<I: Interner> Goal<I> {
11401140
/// required to prove it.
11411141
pub fn is_trivially_true(&self, interner: &I) -> bool {
11421142
match self.data(interner) {
1143-
GoalData::All(goals) => goals.is_empty(),
1143+
GoalData::All(goals) => goals.is_empty(interner),
11441144
_ => false,
11451145
}
11461146
}

chalk-ir/src/tls.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::interner::ChalkIr;
2-
use crate::{AliasTy, AssocTypeId, Goal, Lifetime, Parameter, StructId, TraitId, Ty};
2+
use crate::{
3+
AliasTy, AssocTypeId, Goal, Goals, Lifetime, Parameter, ProgramClauseImplication, StructId,
4+
TraitId, Ty,
5+
};
36
use std::cell::RefCell;
47
use std::fmt;
58
use std::sync::Arc;
@@ -52,6 +55,18 @@ pub trait DebugContext {
5255
goal: &Goal<ChalkIr>,
5356
fmt: &mut fmt::Formatter<'_>,
5457
) -> Result<(), fmt::Error>;
58+
59+
fn debug_goals(
60+
&self,
61+
goals: &Goals<ChalkIr>,
62+
fmt: &mut fmt::Formatter<'_>,
63+
) -> Result<(), fmt::Error>;
64+
65+
fn debug_program_clause_implication(
66+
&self,
67+
pci: &ProgramClauseImplication<ChalkIr>,
68+
fmt: &mut fmt::Formatter<'_>,
69+
) -> Result<(), fmt::Error>;
5570
}
5671

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

chalk-ir/src/zip.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ impl<I: Interner> Zip<I> for Goals<I> {
261261
where
262262
I: 'i,
263263
{
264-
Zip::zip_with(zipper, a.as_slice(), b.as_slice())?;
264+
let interner = zipper.interner();
265+
Zip::zip_with(zipper, a.as_slice(interner), b.as_slice(interner))?;
265266
Ok(())
266267
}
267268
}

chalk-solve/src/solve/slg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<'me, I: Interner> context::ContextOps<SlgContext<I>> for SlgContextOps<'me,
331331
HhGoal::Exists(binders_goal)
332332
}
333333
GoalData::Implies(dg, subgoal) => HhGoal::Implies(dg, subgoal),
334-
GoalData::All(goals) => HhGoal::All(goals.iter().cloned().collect()),
334+
GoalData::All(goals) => HhGoal::All(goals.iter(interner).cloned().collect()),
335335
GoalData::Not(g1) => HhGoal::Not(g1),
336336
GoalData::EqGoal(EqGoal { a, b }) => HhGoal::Unify((), a, b),
337337
GoalData::DomainGoal(domain_goal) => HhGoal::DomainGoal(domain_goal),

chalk-solve/src/solve/slg/resolvent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<I: Interner> context::ResolventOps<SlgContext<I>> for TruncatingInferenceTa
114114
// Add the `conditions` from the program clause into the result too.
115115
ex_clause
116116
.subgoals
117-
.extend(conditions.iter().map(|c| match c.data(interner) {
117+
.extend(conditions.iter(interner).map(|c| match c.data(interner) {
118118
GoalData::Not(c1) => {
119119
Literal::Negative(InEnvironment::new(environment, Goal::clone(c1)))
120120
}

0 commit comments

Comments
 (0)