Skip to content

Commit 85c89d5

Browse files
Compute associated type in impl from trait id without iterating through all AssociatedTyValues
1 parent 09941bb commit 85c89d5

File tree

8 files changed

+73
-1
lines changed

8 files changed

+73
-1
lines changed

chalk-integration/src/db.rs

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

102+
fn associated_ty_from_impl(
103+
&self,
104+
impl_id: ImplId<ChalkIr>,
105+
assoc_type_id: AssocTypeId<ChalkIr>,
106+
) -> Option<AssociatedTyValueId<ChalkIr>> {
107+
let ir = self.program_ir().unwrap();
108+
ir.impl_data[&impl_id]
109+
.associated_ty_value_ids
110+
.iter()
111+
.copied()
112+
.find(|id| ir.associated_ty_values[id].associated_ty_id == assoc_type_id)
113+
}
114+
102115
fn associated_ty_value(
103116
&self,
104117
id: AssociatedTyValueId<ChalkIr>,

chalk-integration/src/program.rs

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

401+
fn associated_ty_from_impl(
402+
&self,
403+
impl_id: ImplId<ChalkIr>,
404+
assoc_type_id: AssocTypeId<ChalkIr>,
405+
) -> Option<AssociatedTyValueId<ChalkIr>> {
406+
self.impl_data[&impl_id]
407+
.associated_ty_value_ids
408+
.iter()
409+
.copied()
410+
.find(|id| self.associated_ty_values[id].associated_ty_id == assoc_type_id)
411+
}
412+
401413
fn associated_ty_value(
402414
&self,
403415
id: AssociatedTyValueId<ChalkIr>,

chalk-solve/src/clauses.rs

Lines changed: 3 additions & 1 deletion
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
) {
@@ -763,7 +765,7 @@ fn push_program_clauses_for_associated_type_values_in_impls_of<I: Interner>(
763765

764766
debug!(?impl_id);
765767

766-
for &atv_id in &impl_datum.associated_ty_value_ids {
768+
if let Some(atv_id) = builder.db.associated_ty_from_impl(impl_id, assoc_id) {
767769
let atv = builder.db.associated_ty_value(atv_id);
768770
debug!(?atv_id, ?atv);
769771
atv.to_program_clauses(builder, environment);

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_from_impl(
120+
&self,
121+
_impl_id: chalk_ir::ImplId<I>,
122+
_assoc_type_id: chalk_ir::AssocTypeId<I>,
123+
) -> Option<crate::rust_ir::AssociatedTyValueId<I>> {
124+
unreachable!("should never reach projection if impl datum is not stubbed")
125+
}
126+
119127
fn associated_ty_value(
120128
&self,
121129
_id: crate::rust_ir::AssociatedTyValueId<I>,

chalk-solve/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ 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+
fn associated_ty_from_impl(
80+
&self,
81+
impl_id: ImplId<I>,
82+
assoc_type_id: AssocTypeId<I>,
83+
) -> Option<AssociatedTyValueId<I>>;
84+
7985
/// Returns the `AssociatedTyValue` with the given id.
8086
fn associated_ty_value(&self, id: AssociatedTyValueId<I>) -> Arc<AssociatedTyValue<I>>;
8187

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_from_impl(
175+
&self,
176+
impl_id: ImplId<I>,
177+
assoc_type_id: AssocTypeId<I>,
178+
) -> Option<AssociatedTyValueId<I>> {
179+
self.ws.db().associated_ty_from_impl(impl_id, assoc_type_id)
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_from_impl(
446+
&self,
447+
impl_id: ImplId<I>,
448+
assoc_type_id: AssocTypeId<I>,
449+
) -> Option<AssociatedTyValueId<I>> {
450+
self.db.associated_ty_from_impl(impl_id, assoc_type_id)
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_from_impl(
98+
&self,
99+
impl_id: chalk_ir::ImplId<I>,
100+
assoc_type_id: chalk_ir::AssocTypeId<I>,
101+
) -> Option<chalk_solve::rust_ir::AssociatedTyValueId<I>> {
102+
self.db.associated_ty_from_impl(impl_id, assoc_type_id)
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_from_impl(
122+
&self,
123+
_impl_id: ImplId<ChalkIr>,
124+
_assoc_type_id: AssocTypeId<ChalkIr>,
125+
) -> Option<AssociatedTyValueId<ChalkIr>> {
126+
unimplemented!()
127+
}
128+
121129
fn associated_ty_value(
122130
&self,
123131
id: AssociatedTyValueId<ChalkIr>,

0 commit comments

Comments
 (0)