Skip to content

Commit 4cf6975

Browse files
ChayimFriedman2compiler-errors
authored andcommitted
Fix retrieval of AsyncFnOnce::Output
We shouldn't just assume it's always the second associated type of `AsyncFnOnce`.
1 parent 260bf15 commit 4cf6975

File tree

9 files changed

+66
-10
lines changed

9 files changed

+66
-10
lines changed

chalk-integration/src/db.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use chalk_ir::{
1515
use chalk_solve::rust_ir::{
1616
AdtDatum, AdtRepr, AdtSizeAlign, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId,
1717
ClosureKind, CoroutineDatum, CoroutineWitnessDatum, FnDefDatum, FnDefInputsAndOutputDatum,
18-
ImplDatum, OpaqueTyDatum, TraitDatum, WellKnownTrait,
18+
ImplDatum, OpaqueTyDatum, TraitDatum, WellKnownAssocType, WellKnownTrait,
1919
};
2020
use chalk_solve::{RustIrDatabase, Solution, SubstitutionResult};
2121
use salsa::Database;
@@ -183,6 +183,15 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
183183
.well_known_trait_id(well_known_trait)
184184
}
185185

186+
fn well_known_assoc_type_id(
187+
&self,
188+
assoc_type: WellKnownAssocType,
189+
) -> Option<AssocTypeId<ChalkIr>> {
190+
self.program_ir()
191+
.unwrap()
192+
.well_known_assoc_type_id(assoc_type)
193+
}
194+
186195
fn program_clauses_for_env(
187196
&self,
188197
environment: &Environment<ChalkIr>,

chalk-integration/src/program.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,14 @@ impl RustIrDatabase<ChalkIr> for Program {
537537
self.well_known_traits.get(&well_known_trait).copied()
538538
}
539539

540+
fn well_known_assoc_type_id(
541+
&self,
542+
_assoc_type: chalk_solve::rust_ir::WellKnownAssocType,
543+
) -> Option<AssocTypeId<ChalkIr>> {
544+
// FIXME
545+
None
546+
}
547+
540548
fn program_clauses_for_env(
541549
&self,
542550
environment: &chalk_ir::Environment<ChalkIr>,

chalk-solve/src/clauses/builtin_traits/fn_family.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::clauses::ClauseBuilder;
2-
use crate::rust_ir::{ClosureKind, FnDefInputsAndOutputDatum, WellKnownTrait};
2+
use crate::rust_ir::{ClosureKind, FnDefInputsAndOutputDatum, WellKnownAssocType, WellKnownTrait};
33
use crate::{Interner, RustIrDatabase, TraitRef};
44
use chalk_ir::cast::Cast;
55
use chalk_ir::{
@@ -83,14 +83,9 @@ fn push_clauses<I: Interner>(
8383

8484
if let WellKnownTrait::AsyncFnOnce = well_known {
8585
builder.push_bound_ty(|builder, ty| {
86-
let trait_datum = db.trait_datum(trait_id);
87-
assert_eq!(
88-
trait_datum.associated_ty_ids.len(),
89-
2,
90-
"AsyncFnOnce trait should have exactly two associated types, found {:?}",
91-
trait_datum.associated_ty_ids
92-
);
93-
let output_id = trait_datum.associated_ty_ids[1];
86+
let output_id = db
87+
.well_known_assoc_type_id(WellKnownAssocType::AsyncFnOnceOutput)
88+
.unwrap();
9489
let async_alias = AliasTy::Projection(ProjectionTy {
9590
associated_ty_id: output_id,
9691
substitution,

chalk-solve/src/display/stub.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ impl<I: Interner, DB: RustIrDatabase<I>> RustIrDatabase<I> for StubWrapper<'_, D
188188
self.db.well_known_trait_id(well_known_trait)
189189
}
190190

191+
fn well_known_assoc_type_id(
192+
&self,
193+
assoc_type: crate::rust_ir::WellKnownAssocType,
194+
) -> Option<chalk_ir::AssocTypeId<I>> {
195+
self.db.well_known_assoc_type_id(assoc_type)
196+
}
197+
191198
fn program_clauses_for_env(
192199
&self,
193200
environment: &chalk_ir::Environment<I>,

chalk-solve/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ pub trait RustIrDatabase<I: Interner>: Debug {
128128
/// Returns id of a trait lang item, if found
129129
fn well_known_trait_id(&self, well_known_trait: WellKnownTrait) -> Option<TraitId<I>>;
130130

131+
/// Returns id of a associated type, if found.
132+
fn well_known_assoc_type_id(&self, assoc_type: WellKnownAssocType) -> Option<AssocTypeId<I>>;
133+
131134
/// Calculates program clauses from an env. This is intended to call the
132135
/// `program_clauses_for_env` function and then possibly cache the clauses.
133136
fn program_clauses_for_env(&self, environment: &Environment<I>) -> ProgramClauses<I>;

chalk-solve/src/logging_db.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ where
229229
trait_id
230230
}
231231

232+
fn well_known_assoc_type_id(&self, assoc_type: WellKnownAssocType) -> Option<AssocTypeId<I>> {
233+
let assoc_type_id = self.ws.db().well_known_assoc_type_id(assoc_type);
234+
if let Some(id) = assoc_type_id {
235+
self.record(self.ws.db().associated_ty_data(id).trait_id);
236+
}
237+
assoc_type_id
238+
}
239+
232240
fn program_clauses_for_env(
233241
&self,
234242
environment: &chalk_ir::Environment<I>,
@@ -489,6 +497,10 @@ where
489497
self.db.well_known_trait_id(well_known_trait)
490498
}
491499

500+
fn well_known_assoc_type_id(&self, assoc_type: WellKnownAssocType) -> Option<AssocTypeId<I>> {
501+
self.db.well_known_assoc_type_id(assoc_type)
502+
}
503+
492504
fn program_clauses_for_env(
493505
&self,
494506
environment: &chalk_ir::Environment<I>,

chalk-solve/src/rust_ir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,15 @@ pub enum WellKnownTrait {
287287

288288
chalk_ir::const_visit!(WellKnownTrait);
289289

290+
/// A list of the associated types that are "well known" to chalk, which means that
291+
/// the chalk-solve crate has special, hard-coded impls for them.
292+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
293+
pub enum WellKnownAssocType {
294+
AsyncFnOnceOutput,
295+
}
296+
297+
chalk_ir::const_visit!(WellKnownAssocType);
298+
290299
impl<I: Interner> TraitDatum<I> {
291300
pub fn is_auto_trait(&self) -> bool {
292301
self.flags.auto

tests/display/unique_names.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ where
155155
) -> Option<chalk_ir::TraitId<I>> {
156156
self.db.well_known_trait_id(well_known_trait)
157157
}
158+
fn well_known_assoc_type_id(
159+
&self,
160+
assoc_type: chalk_solve::rust_ir::WellKnownAssocType,
161+
) -> Option<chalk_ir::AssocTypeId<I>> {
162+
self.db.well_known_assoc_type_id(assoc_type)
163+
}
158164
fn program_clauses_for_env(
159165
&self,
160166
environment: &chalk_ir::Environment<I>,

tests/integration/panic.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ impl RustIrDatabase<ChalkIr> for MockDatabase {
211211
unimplemented!()
212212
}
213213

214+
fn well_known_assoc_type_id(
215+
&self,
216+
assoc_type: WellKnownAssocType,
217+
) -> Option<AssocTypeId<ChalkIr>> {
218+
unimplemented!()
219+
}
220+
214221
fn program_clauses_for_env(
215222
&self,
216223
environment: &Environment<ChalkIr>,

0 commit comments

Comments
 (0)