Skip to content

Commit afabe97

Browse files
committed
Introduce OpaqueTyBound
1 parent 8eb7922 commit afabe97

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

chalk-integration/src/lowering.rs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use chalk_ir::{
66
};
77
use chalk_parse::ast::*;
88
use chalk_rust_ir as rust_ir;
9-
use chalk_rust_ir::{Anonymize, AssociatedTyValueId, IntoWhereClauses, OpaqueTyDatum, ToParameter};
9+
use chalk_rust_ir::{
10+
Anonymize, AssociatedTyValueId, IntoWhereClauses, OpaqueTyBound, OpaqueTyDatum, ToParameter,
11+
};
1012
use lalrpop_intern::intern;
1113
use std::collections::BTreeMap;
1214
use std::sync::Arc;
@@ -378,36 +380,50 @@ impl LowerProgram for Program {
378380
custom_clauses.extend(clause.lower_clause(&empty_env)?);
379381
}
380382
Item::OpaqueTyDefn(ref opaque_ty) => {
381-
if let Some(&value) = opaque_ty_ids.get(&opaque_ty.identifier.str) {
383+
if let Some(&opaque_ty_id) = opaque_ty_ids.get(&opaque_ty.identifier.str) {
382384
let parameter_kinds = opaque_ty
383385
.parameter_kinds
384386
.iter()
385387
.map(|k| k.lower())
386388
.collect::<Vec<_>>();
387389

388-
let binders = empty_env
389-
.in_binders(parameter_kinds, |env| opaque_ty.ty.lower(&env))?;
390+
let binders = empty_env.in_binders(parameter_kinds, |env| {
391+
let hidden_ty = opaque_ty.ty.lower(&env)?;
392+
393+
let hidden_ty_bounds: chalk_ir::Binders<Vec<chalk_ir::Binders<_>>> =
394+
env.in_binders(
395+
Some(chalk_ir::ParameterKind::Ty(intern(FIXME_SELF))),
396+
|env1| {
397+
let interner = env1.interner();
398+
Ok(opaque_ty
399+
.bounds
400+
.lower(&env1)?
401+
.iter()
402+
.flat_map(|qil| {
403+
qil.into_where_clauses(
404+
interner,
405+
chalk_ir::TyData::BoundVar(BoundVar::new(
406+
DebruijnIndex::INNERMOST,
407+
todo!(),
408+
))
409+
.intern(interner),
410+
)
411+
})
412+
.collect())
413+
},
414+
)?;
415+
416+
Ok(OpaqueTyBound {
417+
hidden_ty,
418+
bounds: hidden_ty_bounds.skip_binders().clone(),
419+
})
420+
})?;
390421

391422
opaque_ty_data.insert(
392-
value,
423+
opaque_ty_id,
393424
Arc::new(OpaqueTyDatum {
394-
opaque_ty_id: value,
395-
bounds: opaque_ty
396-
.bounds
397-
.lower(&empty_env)?
398-
.iter()
399-
.flat_map(|qil| {
400-
qil.into_where_clauses(
401-
empty_env.interner(),
402-
chalk_ir::TyData::BoundVar(BoundVar::new(
403-
DebruijnIndex::INNERMOST,
404-
todo!(),
405-
))
406-
.intern(empty_env.interner()),
407-
)
408-
})
409-
.collect(),
410-
ty: binders,
425+
opaque_ty_id,
426+
bound: binders,
411427
}),
412428
);
413429
}

chalk-integration/src/program.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl tls::DebugContext for Program {
121121
fmt: &mut fmt::Formatter<'_>,
122122
) -> Result<(), fmt::Error> {
123123
if let Some(d) = self.opaque_ty_data.get(&opaque_ty_id) {
124-
write!(fmt, "{:?}", d.bounds)
124+
write!(fmt, "{:?}", d.bound)
125125
} else {
126126
fmt.debug_struct("InvalidItemId")
127127
.field("index", &opaque_ty_id.0)

chalk-rust-ir/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,17 @@ pub struct OpaqueTyDatum<I: Interner> {
516516
/// The placeholder `!T` that corresponds to the opaque type `T`.
517517
pub opaque_ty_id: OpaqueTyId<I>,
518518

519+
/// The type bound to when revealed.
520+
pub bound: Binders<OpaqueTyBound<I>>,
521+
}
522+
523+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, HasInterner)]
524+
pub struct OpaqueTyBound<I: Interner> {
525+
/// The value for the "hidden type" for `opaque type Foo = ...`
526+
pub hidden_ty: Ty<I>,
527+
519528
/// Trait bounds for the opaque type.
520529
pub bounds: Vec<QuantifiedWhereClause<I>>,
521-
522-
/// The "hidden type" that the opaque type is equal to when revealed.
523-
pub ty: Binders<Ty<I>>,
524530
}
525531

526532
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]

chalk-solve/src/clauses/program_clauses.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ impl<I: Interner> ToProgramClauses<I> for OpaqueTyDatum<I> {
138138

139139
let alias_ty = Ty::new(interner, alias.clone());
140140

141-
builder.push_binders(&self.ty, |builder, opaque_ty_datum| {
141+
builder.push_binders(&self.bound, |builder, opaque_ty_bound| {
142142
// AliasEq(T<..> = HiddenTy) :- Reveal.
143143
builder.push_clause(
144144
DomainGoal::Holds(
145145
AliasEq {
146146
alias: alias.clone(),
147-
ty: opaque_ty_datum.clone(),
147+
ty: opaque_ty_bound.hidden_ty.clone(),
148148
}
149149
.cast(interner),
150150
),
@@ -161,7 +161,8 @@ impl<I: Interner> ToProgramClauses<I> for OpaqueTyDatum<I> {
161161
));
162162
});
163163

164-
for bound in &self.bounds {
164+
let opaque_ty_bound = &self.bound.skip_binders();
165+
for bound in &opaque_ty_bound.bounds {
165166
// Implemented(!T: Bound).
166167
builder.push_fact(bound.skip_binders().clone().into_well_formed_goal(interner));
167168
}
@@ -175,7 +176,7 @@ impl<I: Interner> ToProgramClauses<I> for OpaqueTyDatum<I> {
175176
},
176177
iter::once(TraitRef {
177178
trait_id: auto_trait_id,
178-
substitution: Substitution::from1(interner, self.ty.skip_binders().clone()),
179+
substitution: Substitution::from1(interner, opaque_ty_bound.hidden_ty.clone()),
179180
}),
180181
);
181182
}

0 commit comments

Comments
 (0)