Skip to content

Commit f8d667e

Browse files
committed
Fix most unimplemented interners
1 parent a200192 commit f8d667e

File tree

8 files changed

+72
-36
lines changed

8 files changed

+72
-36
lines changed

chalk-integration/src/program.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use chalk_ir::debug::Angle;
44
use chalk_ir::interner::ChalkIr;
55
use chalk_ir::tls;
66
use chalk_ir::{
7-
AliasTy, AssocTypeId, ImplId, Parameter, ProgramClause, StructId, TraitId, TyData, TypeName,
7+
Ty, AliasTy, AssocTypeId, ImplId, Parameter, ProgramClause, StructId, TraitId, TyData, TypeName,
88
};
99
use chalk_rust_ir::{
1010
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ImplDatum, ImplType, StructDatum,
@@ -120,6 +120,15 @@ impl tls::DebugContext for Program {
120120
Angle(&other_params)
121121
)
122122
}
123+
124+
fn debug_ty(
125+
&self,
126+
ty: &Ty<ChalkIr>,
127+
fmt: &mut fmt::Formatter<'_>,
128+
) -> Result<(), fmt::Error> {
129+
let interner = self.interner();
130+
write!(fmt, "{:?}", ty.data(interner))
131+
}
123132
}
124133

125134
impl RustIrDatabase<ChalkIr> for Program {
@@ -162,6 +171,7 @@ impl RustIrDatabase<ChalkIr> for Program {
162171
trait_id: TraitId<ChalkIr>,
163172
parameters: &[Parameter<ChalkIr>],
164173
) -> Vec<ImplId<ChalkIr>> {
174+
let interner = self.interner();
165175
self.impl_data
166176
.iter()
167177
.filter(|(_, impl_datum)| {
@@ -170,6 +180,7 @@ impl RustIrDatabase<ChalkIr> for Program {
170180
assert_eq!(trait_ref.substitution.len(), parameters.len());
171181
<[_] as CouldMatch<[_]>>::could_match(
172182
&parameters,
183+
interner,
173184
&trait_ref.substitution.parameters(),
174185
)
175186
}

chalk-ir/src/could_match.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::zip::{Zip, Zipper};
33
use crate::*;
44

55
/// A fast check to see whether two things could ever possibly match.
6-
pub trait CouldMatch<T: ?Sized> {
7-
fn could_match(&self, other: &T) -> bool;
6+
pub trait CouldMatch<T: ?Sized + HasInterner> {
7+
fn could_match(&self, interner: &T::Interner, other: &T) -> bool;
88
}
99

1010
#[allow(unreachable_code, unused_variables)]
@@ -13,8 +13,7 @@ where
1313
T: Zip<I> + ?Sized + HasInterner<Interner = I>,
1414
I: Interner,
1515
{
16-
fn could_match(&self, other: &T) -> bool {
17-
let interner = unimplemented!();
16+
fn could_match(&self, interner: &I, other: &T) -> bool {
1817
return Zip::zip_with(&mut MatchZipper{ interner }, self, other).is_ok();
1918

2019
struct MatchZipper<'i, I> {
@@ -31,7 +30,7 @@ where
3130
&& a.substitution
3231
.iter()
3332
.zip(&b.substitution)
34-
.all(|(p_a, p_b)| p_a.could_match(&p_b))
33+
.all(|(p_a, p_b)| p_a.could_match(self.interner, &p_b))
3534
}
3635

3736
_ => true,
@@ -59,11 +58,11 @@ where
5958
}
6059

6160
impl<I: Interner> CouldMatch<DomainGoal<I>> for ProgramClause<I> {
62-
fn could_match(&self, other: &DomainGoal<I>) -> bool {
61+
fn could_match(&self, interner: &I, other: &DomainGoal<I>) -> bool {
6362
match self {
64-
ProgramClause::Implies(implication) => implication.consequence.could_match(other),
63+
ProgramClause::Implies(implication) => implication.consequence.could_match(interner, other),
6564

66-
ProgramClause::ForAll(clause) => clause.value.consequence.could_match(other),
65+
ProgramClause::ForAll(clause) => clause.value.consequence.could_match(interner, other),
6766
}
6867
}
6968
}

chalk-ir/src/debug.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ impl<I: Interner> Debug for AssocTypeId<I> {
2121
}
2222
}
2323

24+
impl<I: Interner> Debug for Ty<I> {
25+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
26+
I::debug_ty(self, fmt).unwrap_or_else(|| unimplemented!())
27+
}
28+
}
29+
30+
2431
impl Display for UniverseIndex {
2532
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
2633
write!(fmt, "U{}", self.counter)
@@ -43,14 +50,6 @@ impl<I: Interner> Debug for TypeName<I> {
4350
}
4451
}
4552

46-
#[allow(unreachable_code, unused_variables)]
47-
impl<I: Interner> Debug for Ty<I> {
48-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
49-
let interner = unimplemented!();
50-
write!(fmt, "{:?}", self.data(interner))
51-
}
52-
}
53-
5453
impl<I: Interner> Debug for TyData<I> {
5554
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
5655
match self {

chalk-ir/src/interner.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::ParameterData;
88
use crate::StructId;
99
use crate::TraitId;
1010
use crate::TyData;
11+
use crate::Ty;
1112
use chalk_engine::context::Context;
1213
use chalk_engine::ExClause;
1314
use std::fmt::{self, Debug};
@@ -132,6 +133,18 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
132133
/// if no info about current program is available from TLS).
133134
fn debug_alias(alias: &AliasTy<Self>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result>;
134135

136+
/// Prints the debug representation of an type. To get good
137+
/// results, this requires inspecting TLS, and is difficult to
138+
/// code without reference to a specific interner (and hence
139+
/// fully known types).
140+
///
141+
/// Returns `None` to fallback to the default debug output (e.g.,
142+
/// if no info about current program is available from TLS).
143+
fn debug_ty(
144+
ty: &Ty<Self>,
145+
fmt: &mut fmt::Formatter<'_>,
146+
) -> Option<fmt::Result>;
147+
135148
/// Create an "interned" type from `ty`. This is not normally
136149
/// invoked directly; instead, you invoke `TyData::intern` (which
137150
/// will ultimately call this method).
@@ -272,6 +285,13 @@ mod default {
272285
tls::with_current_program(|prog| Some(prog?.debug_alias(alias, fmt)))
273286
}
274287

288+
fn debug_ty(
289+
alias: &Ty<ChalkIr>,
290+
fmt: &mut fmt::Formatter<'_>,
291+
) -> Option<fmt::Result> {
292+
tls::with_current_program(|prog| Some(prog?.debug_ty(alias, fmt)))
293+
}
294+
275295
fn intern_ty(&self, ty: TyData<ChalkIr>) -> Arc<TyData<ChalkIr>> {
276296
Arc::new(ty)
277297
}

chalk-ir/src/tls.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::interner::ChalkIr;
2-
use crate::{AliasTy, AssocTypeId, StructId, TraitId};
2+
use crate::{AliasTy, AssocTypeId, Ty, StructId, TraitId};
33
use std::cell::RefCell;
44
use std::fmt;
55
use std::sync::Arc;
@@ -32,6 +32,12 @@ pub trait DebugContext {
3232
alias: &AliasTy<ChalkIr>,
3333
fmt: &mut fmt::Formatter<'_>,
3434
) -> Result<(), fmt::Error>;
35+
36+
fn debug_ty(
37+
&self,
38+
ty: &Ty<ChalkIr>,
39+
fmt: &mut fmt::Formatter<'_>,
40+
) -> Result<(), fmt::Error>;
3541
}
3642

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

chalk-solve/src/clauses.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ pub(crate) fn program_clauses_for_goal<'db, I: Interner>(
116116
goal,
117117
environment
118118
);
119+
let interner = db.interner();
119120

120121
let mut vec = vec![];
121122
vec.extend(db.custom_clauses());
122123
program_clauses_that_could_match(db, environment, goal, &mut vec);
123124
program_clauses_for_env(db, environment, &mut vec);
124-
vec.retain(|c| c.could_match(goal));
125+
vec.retain(|c| c.could_match(interner, goal));
125126

126127
debug!("vec = {:#?}", vec);
127128

chalk-solve/src/solve/slg.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'me, I: Interner> context::ContextOps<SlgContext<I>> for SlgContextOps<'me,
234234
environment
235235
.clauses
236236
.iter()
237-
.filter(|&env_clause| env_clause.could_match(goal))
237+
.filter(|&env_clause| env_clause.could_match(interner, goal))
238238
.cloned(),
239239
);
240240

@@ -495,22 +495,24 @@ fn into_ex_clause<I: Interner>(
495495
}
496496

497497
trait SubstitutionExt<I: Interner> {
498-
fn may_invalidate(&self, subst: &Canonical<Substitution<I>>) -> bool;
498+
fn may_invalidate(&self, interner: &I, subst: &Canonical<Substitution<I>>) -> bool;
499499
}
500500

501501
impl<I: Interner> SubstitutionExt<I> for Substitution<I> {
502-
fn may_invalidate(&self, subst: &Canonical<Substitution<I>>) -> bool {
502+
fn may_invalidate(&self, interner: &I, subst: &Canonical<Substitution<I>>) -> bool {
503503
self.iter()
504504
.zip(subst.value.iter())
505-
.any(|(new, current)| MayInvalidate.aggregate_parameters(new, current))
505+
.any(|(new, current)| MayInvalidate{ interner }.aggregate_parameters( new, current))
506506
}
507507
}
508508

509509
// This is a struct in case we need to add state at any point like in AntiUnifier
510-
struct MayInvalidate;
510+
struct MayInvalidate<'i, I> {
511+
interner: &'i I,
512+
}
511513

512-
impl MayInvalidate {
513-
fn aggregate_parameters<I: Interner>(
514+
impl<I: Interner> MayInvalidate<'_, I> {
515+
fn aggregate_parameters(
514516
&mut self,
515517
new: &Parameter<I>,
516518
current: &Parameter<I>,
@@ -528,9 +530,8 @@ impl MayInvalidate {
528530
}
529531

530532
// Returns true if the two types could be unequal.
531-
#[allow(unreachable_code, unused_variables)]
532-
fn aggregate_tys<I: Interner>(&mut self, new: &Ty<I>, current: &Ty<I>) -> bool {
533-
let interner = unimplemented!();
533+
fn aggregate_tys(&mut self, new: &Ty<I>, current: &Ty<I>) -> bool {
534+
let interner = self.interner;
534535
match (new.data(interner), current.data(interner)) {
535536
(_, TyData::BoundVar(_)) => {
536537
// If the aggregate solution already has an inference
@@ -586,11 +587,11 @@ impl MayInvalidate {
586587
}
587588
}
588589

589-
fn aggregate_lifetimes<I: Interner>(&mut self, _: &Lifetime<I>, _: &Lifetime<I>) -> bool {
590+
fn aggregate_lifetimes(&mut self, _: &Lifetime<I>, _: &Lifetime<I>) -> bool {
590591
true
591592
}
592593

593-
fn aggregate_application_tys<I: Interner>(
594+
fn aggregate_application_tys(
594595
&mut self,
595596
new: &ApplicationTy<I>,
596597
current: &ApplicationTy<I>,
@@ -620,7 +621,7 @@ impl MayInvalidate {
620621
new != current
621622
}
622623

623-
fn aggregate_alias_tys<I: Interner>(&mut self, new: &AliasTy<I>, current: &AliasTy<I>) -> bool {
624+
fn aggregate_alias_tys(&mut self, new: &AliasTy<I>, current: &AliasTy<I>) -> bool {
624625
let AliasTy {
625626
associated_ty_id: new_name,
626627
substitution: new_substitution,
@@ -638,7 +639,7 @@ impl MayInvalidate {
638639
)
639640
}
640641

641-
fn aggregate_name_and_substs<N, I>(
642+
fn aggregate_name_and_substs<N>(
642643
&mut self,
643644
new_name: N,
644645
new_substitution: &Substitution<I>,
@@ -647,7 +648,6 @@ impl MayInvalidate {
647648
) -> bool
648649
where
649650
N: Copy + Eq + Debug,
650-
I: Interner,
651651
{
652652
if new_name != current_name {
653653
return true;
@@ -667,6 +667,6 @@ impl MayInvalidate {
667667
new_substitution
668668
.iter()
669669
.zip(current_substitution)
670-
.any(|(new, current)| self.aggregate_parameters(new, current))
670+
.any(|(new, current)| self.aggregate_parameters( new, current))
671671
}
672672
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<I: Interner> context::AggregateOps<SlgContext<I>> for SlgContextOps<'_, I>
7070
break Guidance::Unknown;
7171
}
7272

73-
if !answers.any_future_answer(|ref mut new_subst| new_subst.may_invalidate(&subst)) {
73+
if !answers.any_future_answer(|ref mut new_subst| new_subst.may_invalidate(interner, &subst)) {
7474
break Guidance::Definite(subst);
7575
}
7676

0 commit comments

Comments
 (0)