Skip to content

Commit 5c55332

Browse files
Add associated_ty_matches to Chalk db to avoid computing RPITIT associated types eagerly
1 parent 09941bb commit 5c55332

File tree

8 files changed

+73
-3
lines changed

8 files changed

+73
-3
lines changed

chalk-integration/src/db.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
9999
self.program_ir().unwrap().impl_datum(id)
100100
}
101101

102+
fn associated_ty_matches(
103+
&self,
104+
trait_item: AssocTypeId<ChalkIr>,
105+
impl_item: AssociatedTyValueId<ChalkIr>,
106+
) -> bool {
107+
self.program_ir().unwrap().associated_ty_values[&impl_item].associated_ty_id == trait_item
108+
}
109+
102110
fn associated_ty_value(
103111
&self,
104112
id: AssociatedTyValueId<ChalkIr>,

chalk-integration/src/program.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,14 @@ impl RustIrDatabase<ChalkIr> for Program {
398398
self.impl_data[&id].clone()
399399
}
400400

401+
fn associated_ty_matches(
402+
&self,
403+
trait_item: AssocTypeId<ChalkIr>,
404+
impl_item: AssociatedTyValueId<ChalkIr>,
405+
) -> bool {
406+
self.associated_ty_values[&impl_item].associated_ty_id == trait_item
407+
}
408+
401409
fn associated_ty_value(
402410
&self,
403411
id: AssociatedTyValueId<ChalkIr>,

chalk-solve/src/clauses.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ pub fn program_clauses_that_could_match<I: Interner>(
639639
builder,
640640
environment,
641641
trait_id,
642+
proj.associated_ty_id,
642643
trait_parameters,
643644
binders,
644645
);
@@ -749,6 +750,7 @@ fn push_program_clauses_for_associated_type_values_in_impls_of<I: Interner>(
749750
builder: &mut ClauseBuilder<'_, I>,
750751
environment: &Environment<I>,
751752
trait_id: TraitId<I>,
753+
assoc_id: AssocTypeId<I>,
752754
trait_parameters: &[GenericArg<I>],
753755
binders: &CanonicalVarKinds<I>,
754756
) {
@@ -764,9 +766,14 @@ fn push_program_clauses_for_associated_type_values_in_impls_of<I: Interner>(
764766
debug!(?impl_id);
765767

766768
for &atv_id in &impl_datum.associated_ty_value_ids {
767-
let atv = builder.db.associated_ty_value(atv_id);
768-
debug!(?atv_id, ?atv);
769-
atv.to_program_clauses(builder, environment);
769+
// Avoid unnecessarily computing the type of associated types that do not
770+
// match this goal. This is to ensure that we have no cycles when normalizing
771+
// in RPITIT inference.
772+
if builder.db.associated_ty_matches(assoc_id, atv_id) {
773+
let atv = builder.db.associated_ty_value(atv_id);
774+
debug!(?atv_id, ?atv);
775+
atv.to_program_clauses(builder, environment);
776+
}
770777
}
771778
}
772779
}

chalk-solve/src/display/stub.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ impl<I: Interner, DB: RustIrDatabase<I>> RustIrDatabase<I> for StubWrapper<'_, D
116116
unreachable!("impl items should never be stubbed")
117117
}
118118

119+
fn associated_ty_matches(
120+
&self,
121+
_trait_item: chalk_ir::AssocTypeId<I>,
122+
_impl_item: crate::rust_ir::AssociatedTyValueId<I>,
123+
) -> bool {
124+
unreachable!("associated type values should never be stubbed")
125+
}
126+
119127
fn associated_ty_value(
120128
&self,
121129
_id: crate::rust_ir::AssociatedTyValueId<I>,

chalk-solve/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ pub trait RustIrDatabase<I: Interner>: Debug {
7676
/// Returns the datum for the impl with the given id.
7777
fn impl_datum(&self, impl_id: ImplId<I>) -> Arc<ImplDatum<I>>;
7878

79+
/// Returns if the trait item for `AssocTypeId` matches the impl item for
80+
/// `AssociatedTyValueId`.
81+
fn associated_ty_matches(
82+
&self,
83+
trait_item: AssocTypeId<I>,
84+
impl_item: AssociatedTyValueId<I>,
85+
) -> bool;
86+
7987
/// Returns the `AssociatedTyValue` with the given id.
8088
fn associated_ty_value(&self, id: AssociatedTyValueId<I>) -> Arc<AssociatedTyValue<I>>;
8189

chalk-solve/src/logging_db.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ where
171171
self.ws.db().hidden_opaque_type(id)
172172
}
173173

174+
fn associated_ty_matches(
175+
&self,
176+
trait_item: AssocTypeId<I>,
177+
impl_item: AssociatedTyValueId<I>,
178+
) -> bool {
179+
self.ws.db().associated_ty_matches(trait_item, impl_item)
180+
}
181+
174182
fn associated_ty_value(
175183
&self,
176184
id: crate::rust_ir::AssociatedTyValueId<I>,
@@ -434,6 +442,14 @@ where
434442
self.db.impl_datum(impl_id)
435443
}
436444

445+
fn associated_ty_matches(
446+
&self,
447+
trait_item: AssocTypeId<I>,
448+
impl_item: AssociatedTyValueId<I>,
449+
) -> bool {
450+
self.db.associated_ty_matches(trait_item, impl_item)
451+
}
452+
437453
fn associated_ty_value(
438454
&self,
439455
id: crate::rust_ir::AssociatedTyValueId<I>,

tests/display/unique_names.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ where
9494
) -> std::sync::Arc<chalk_solve::rust_ir::ImplDatum<I>> {
9595
self.db.impl_datum(impl_id)
9696
}
97+
fn associated_ty_matches(
98+
&self,
99+
trait_item: chalk_ir::AssocTypeId<I>,
100+
impl_item: chalk_solve::rust_ir::AssociatedTyValueId<I>,
101+
) -> bool {
102+
self.db.associated_ty_matches(trait_item, impl_item)
103+
}
97104
fn associated_ty_value(
98105
&self,
99106
id: chalk_solve::rust_ir::AssociatedTyValueId<I>,

tests/integration/panic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ impl RustIrDatabase<ChalkIr> for MockDatabase {
118118
})
119119
}
120120

121+
fn associated_ty_matches(
122+
&self,
123+
_trait_item: AssocTypeId<ChalkIr>,
124+
_impl_item: AssociatedTyValueId<ChalkIr>,
125+
) -> bool {
126+
unimplemented!()
127+
}
128+
121129
fn associated_ty_value(
122130
&self,
123131
id: AssociatedTyValueId<ChalkIr>,

0 commit comments

Comments
 (0)