Skip to content

Commit 5a3b871

Browse files
authored
Merge pull request #478 from detrumi/lazy-opaque-types
Request hidden opaque types lazily
2 parents f4977ab + a094046 commit 5a3b871

File tree

8 files changed

+27
-8
lines changed

8 files changed

+27
-8
lines changed

chalk-integration/src/db.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
use chalk_engine::forest::SubstitutionResult;
1010
use chalk_ir::{
1111
AdtId, AssocTypeId, Canonical, ConstrainedSubst, Environment, FnDefId, GenericArg, Goal,
12-
ImplId, InEnvironment, OpaqueTyId, ProgramClause, ProgramClauses, TraitId, UCanonical,
12+
ImplId, InEnvironment, OpaqueTyId, ProgramClause, ProgramClauses, TraitId, Ty, UCanonical,
1313
};
1414
use chalk_solve::rust_ir::{
1515
AdtDatum, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, FnDefDatum, ImplDatum,
@@ -97,6 +97,10 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
9797
self.program_ir().unwrap().opaque_ty_data(id)
9898
}
9999

100+
fn hidden_opaque_type(&self, id: OpaqueTyId<ChalkIr>) -> Ty<ChalkIr> {
101+
self.program_ir().unwrap().hidden_opaque_type(id)
102+
}
103+
100104
fn adt_datum(&self, id: AdtId<ChalkIr>) -> Arc<AdtDatum<ChalkIr>> {
101105
self.program_ir().unwrap().adt_datum(id)
102106
}

chalk-integration/src/lowering.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ impl LowerProgram for Program {
365365
let mut associated_ty_data = BTreeMap::new();
366366
let mut associated_ty_values = BTreeMap::new();
367367
let mut opaque_ty_data = BTreeMap::new();
368+
let mut hidden_opaque_types = BTreeMap::new();
368369
let mut custom_clauses = Vec::new();
369370
for (item, &raw_id) in self.items.iter().zip(&raw_ids) {
370371
let empty_env = Env {
@@ -500,6 +501,7 @@ impl LowerProgram for Program {
500501
// So if we have `type Foo<P1..Pn> = impl Trait<T1..Tn>`, this would introduce `P1..Pn`
501502
let binders = empty_env.in_binders(variable_kinds, |env| {
502503
let hidden_ty = opaque_ty.ty.lower(&env)?;
504+
hidden_opaque_types.insert(opaque_ty_id, Arc::new(hidden_ty));
503505

504506
// Introduce a variable to represent the hidden "self type". This will be used in the bounds.
505507
// So the `impl Trait<T1..Tn>` will be lowered to `exists<Self> { Self: Trait<T1..Tn> }`.
@@ -530,7 +532,7 @@ impl LowerProgram for Program {
530532
},
531533
)?;
532534

533-
Ok(OpaqueTyDatumBound { hidden_ty, bounds })
535+
Ok(OpaqueTyDatumBound { bounds })
534536
})?;
535537

536538
opaque_ty_data.insert(
@@ -562,6 +564,7 @@ impl LowerProgram for Program {
562564
opaque_ty_ids,
563565
opaque_ty_kinds,
564566
opaque_ty_data,
567+
hidden_opaque_types,
565568
custom_clauses,
566569
object_safe_traits,
567570
};

chalk-integration/src/program.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ pub struct Program {
5656
/// For each opaque type:
5757
pub opaque_ty_data: BTreeMap<OpaqueTyId<ChalkIr>, Arc<OpaqueTyDatum<ChalkIr>>>,
5858

59+
/// Stores the hidden types for opaque types
60+
pub hidden_opaque_types: BTreeMap<OpaqueTyId<ChalkIr>, Arc<Ty<ChalkIr>>>,
61+
5962
/// For each trait:
6063
pub trait_data: BTreeMap<TraitId<ChalkIr>, Arc<TraitDatum<ChalkIr>>>,
6164

@@ -336,6 +339,10 @@ impl RustIrDatabase<ChalkIr> for Program {
336339
self.opaque_ty_data[&id].clone()
337340
}
338341

342+
fn hidden_opaque_type(&self, id: OpaqueTyId<ChalkIr>) -> Ty<ChalkIr> {
343+
(*self.hidden_opaque_types[&id]).clone()
344+
}
345+
339346
fn adt_datum(&self, id: AdtId<ChalkIr>) -> Arc<AdtDatum<ChalkIr>> {
340347
self.adt_data[&id].clone()
341348
}

chalk-solve/src/clauses.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ pub fn push_auto_trait_impls_opaque<I: Interner>(
142142
1
143143
);
144144

145-
let binders = opaque_ty_datum.bound.map_ref(|b| &b.hidden_ty);
146-
builder.push_binders(&binders, |builder, hidden_ty| {
145+
let hidden_ty = builder.db.hidden_opaque_type(opaque_id);
146+
let binders = opaque_ty_datum.bound.clone();
147+
builder.push_binders(&binders, |builder, _| {
147148
let self_ty: Ty<_> = ApplicationTy {
148149
name: opaque_id.cast(interner),
149150
substitution: builder.substitution_in_scope(),

chalk-solve/src/clauses/program_clauses.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<I: Interner> ToProgramClauses<I> for OpaqueTyDatum<I> {
151151
DomainGoal::Holds(
152152
AliasEq {
153153
alias: alias.clone(),
154-
ty: opaque_ty_bound.hidden_ty.clone(),
154+
ty: builder.db.hidden_opaque_type(self.opaque_ty_id),
155155
}
156156
.cast(interner),
157157
),

chalk-solve/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ pub trait RustIrDatabase<I: Interner>: Debug {
5151
/// Returns the `OpaqueTyDatum` with the given id.
5252
fn opaque_ty_data(&self, id: OpaqueTyId<I>) -> Arc<OpaqueTyDatum<I>>;
5353

54+
/// Returns the "hidden type" corresponding with the opaque type.
55+
fn hidden_opaque_type(&self, id: OpaqueTyId<I>) -> Ty<I>;
56+
5457
/// Returns a list of potentially relevant impls for a given
5558
/// trait-id; we also supply the type parameters that we are
5659
/// trying to match (if known: these parameters may contain

chalk-solve/src/rust_ir.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,6 @@ pub struct OpaqueTyDatum<I: Interner> {
544544

545545
#[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, HasInterner)]
546546
pub struct OpaqueTyDatumBound<I: Interner> {
547-
/// The value for the "hidden type" for `opaque type Foo = ...`
548-
pub hidden_ty: Ty<I>,
549-
550547
/// Trait bounds for the opaque type.
551548
pub bounds: Binders<Vec<QuantifiedWhereClause<I>>>,
552549
}

tests/integration/panic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ impl RustIrDatabase<ChalkIr> for MockDatabase {
121121
unimplemented!()
122122
}
123123

124+
fn hidden_opaque_type(&self, id: OpaqueTyId<ChalkIr>) -> Ty<ChalkIr> {
125+
unimplemented!()
126+
}
127+
124128
fn adt_datum(&self, id: AdtId<ChalkIr>) -> Arc<AdtDatum<ChalkIr>> {
125129
unimplemented!()
126130
}

0 commit comments

Comments
 (0)