Skip to content

Commit b68aa38

Browse files
committed
Changes from review
1 parent 623c268 commit b68aa38

File tree

1 file changed

+84
-78
lines changed

1 file changed

+84
-78
lines changed

chalk-integration/src/lowering.rs

Lines changed: 84 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -118,52 +118,49 @@ impl Env<'_> {
118118
}
119119
};
120120

121-
if let Ok(lookup) = self.lookup_apply_type(name) {
122-
match lookup {
123-
ApplyTypeLookup::Param(p) => {
124-
let b = p.skip_kind();
125-
match &p.kind {
126-
chalk_ir::VariableKind::Ty(_) => Ok(chalk_ir::TyData::BoundVar(*b)
127-
.intern(interner)
128-
.cast(interner)),
129-
chalk_ir::VariableKind::Lifetime => {
130-
Ok(chalk_ir::LifetimeData::BoundVar(*b)
131-
.intern(interner)
132-
.cast(interner))
133-
}
134-
chalk_ir::VariableKind::Const(ty) => {
135-
Ok(b.to_const(interner, ty.clone()).cast(interner))
136-
}
121+
match self.lookup_apply_type(name) {
122+
Ok(ApplyTypeLookup::Param(p)) => {
123+
let b = p.skip_kind();
124+
match &p.kind {
125+
chalk_ir::VariableKind::Ty(_) => Ok(chalk_ir::TyData::BoundVar(*b)
126+
.intern(interner)
127+
.cast(interner)),
128+
chalk_ir::VariableKind::Lifetime => Ok(chalk_ir::LifetimeData::BoundVar(*b)
129+
.intern(interner)
130+
.cast(interner)),
131+
chalk_ir::VariableKind::Const(ty) => {
132+
Ok(b.to_const(interner, ty.clone()).cast(interner))
137133
}
138134
}
139-
ApplyTypeLookup::Adt(id) => apply(self.adt_kind(id), chalk_ir::TypeName::Adt(id)),
140-
ApplyTypeLookup::FnDef(id) => {
141-
apply(self.fn_def_kind(id), chalk_ir::TypeName::FnDef(id))
142-
}
143-
ApplyTypeLookup::Closure(id) => {
144-
apply(self.closure_kind(id), chalk_ir::TypeName::Closure(id))
145-
}
146-
ApplyTypeLookup::Opaque(id) => Ok(chalk_ir::TyData::Alias(
147-
chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy {
148-
opaque_ty_id: id,
149-
substitution: chalk_ir::Substitution::empty(interner),
150-
}),
151-
)
152-
.intern(interner)
153-
.cast(interner)),
154135
}
155-
} else {
156-
if let Some(id) = self.foreign_ty_ids.get(&name.str) {
157-
Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
158-
name: chalk_ir::TypeName::Foreign(*id),
136+
Ok(ApplyTypeLookup::Adt(id)) => apply(self.adt_kind(id), chalk_ir::TypeName::Adt(id)),
137+
Ok(ApplyTypeLookup::FnDef(id)) => {
138+
apply(self.fn_def_kind(id), chalk_ir::TypeName::FnDef(id))
139+
}
140+
Ok(ApplyTypeLookup::Closure(id)) => {
141+
apply(self.closure_kind(id), chalk_ir::TypeName::Closure(id))
142+
}
143+
Ok(ApplyTypeLookup::Opaque(id)) => Ok(chalk_ir::TyData::Alias(
144+
chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy {
145+
opaque_ty_id: id,
159146
substitution: chalk_ir::Substitution::empty(interner),
160-
})
161-
.intern(interner)
162-
.cast(interner))
163-
} else if let Some(_) = self.trait_ids.get(&name.str) {
164-
Err(RustIrError::NotStruct(name.clone()))
165-
} else {
166-
Err(RustIrError::InvalidParameterName(name.clone()))
147+
}),
148+
)
149+
.intern(interner)
150+
.cast(interner)),
151+
Err(_) => {
152+
if let Some(id) = self.foreign_ty_ids.get(&name.str) {
153+
Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
154+
name: chalk_ir::TypeName::Foreign(*id),
155+
substitution: chalk_ir::Substitution::empty(interner),
156+
})
157+
.intern(interner)
158+
.cast(interner))
159+
} else if let Some(_) = self.trait_ids.get(&name.str) {
160+
Err(RustIrError::NotStruct(name.clone()))
161+
} else {
162+
Err(RustIrError::InvalidParameterName(name.clone()))
163+
}
167164
}
168165
}
169166
}
@@ -343,47 +340,29 @@ pub fn lower_program(program: &Program) -> LowerResult<LoweredProgram> {
343340
let mut opaque_ty_kinds = BTreeMap::new();
344341
let mut object_safe_traits = HashSet::new();
345342

346-
macro_rules! lower_type_kind {
347-
($self: ident, $sort: ident, $params: expr) => {
348-
Ok(TypeKind {
349-
sort: TypeSort::$sort,
350-
name: $self.name.str.clone(),
351-
binders: chalk_ir::Binders::new(
352-
VariableKinds::from_iter(&ChalkIr, $params.anonymize()),
353-
crate::Unit,
354-
),
355-
})
356-
};
357-
}
358-
359343
for (item, &raw_id) in program.items.iter().zip(&raw_ids) {
360344
match item {
361345
Item::AdtDefn(defn) => {
362-
let type_kind = lower_type_kind!(defn, Adt, defn.all_parameters())?;
346+
let type_kind = defn.lower_type_kind()?;
363347
let id = AdtId(raw_id);
364348
adt_ids.insert(type_kind.name.clone(), id);
365349
adt_kinds.insert(id, type_kind);
366350
}
367351
Item::FnDefn(defn) => {
368-
let type_kind = lower_type_kind!(defn, FnDef, defn.all_parameters())?;
352+
let type_kind = defn.lower_type_kind()?;
369353
let id = FnDefId(raw_id);
370354
fn_def_ids.insert(type_kind.name.clone(), id);
371355
fn_def_kinds.insert(id, type_kind);
372356
fn_def_abis.insert(id, lower_fn_abi(&defn.sig.abi)?);
373357
}
374358
Item::ClosureDefn(defn) => {
375-
let type_kind = lower_type_kind!(defn, Closure, defn.all_parameters())?;
359+
let type_kind = defn.lower_type_kind()?;
376360
let id = ClosureId(raw_id);
377361
closure_ids.insert(defn.name.str.clone(), id);
378362
closure_kinds.insert(id, type_kind);
379363
}
380364
Item::TraitDefn(defn) => {
381-
let variable_kinds = defn
382-
.variable_kinds
383-
.iter()
384-
.map(lower_variable_kind)
385-
.collect::<Vec<_>>();
386-
let type_kind = lower_type_kind!(defn, Trait, variable_kinds)?;
365+
let type_kind = defn.lower_type_kind()?;
387366
let id = TraitId(raw_id);
388367
trait_ids.insert(type_kind.name.clone(), id);
389368
trait_kinds.insert(id, type_kind);
@@ -394,12 +373,7 @@ pub fn lower_program(program: &Program) -> LowerResult<LoweredProgram> {
394373
}
395374
}
396375
Item::OpaqueTyDefn(defn) => {
397-
let variable_kinds = defn
398-
.variable_kinds
399-
.iter()
400-
.map(lower_variable_kind)
401-
.collect::<Vec<_>>();
402-
let type_kind = lower_type_kind!(defn, Opaque, variable_kinds)?;
376+
let type_kind = defn.lower_type_kind()?;
403377
let id = OpaqueTyId(raw_id);
404378
opaque_ty_ids.insert(defn.name.str.clone(), id);
405379
opaque_ty_kinds.insert(id, type_kind);
@@ -735,6 +709,42 @@ lower_param_map!(
735709
))
736710
);
737711

712+
trait LowerTypeKind {
713+
fn lower_type_kind(&self) -> LowerResult<TypeKind>;
714+
}
715+
716+
macro_rules! lower_type_kind {
717+
($type: ident, $sort: ident, $params: expr) => {
718+
impl LowerTypeKind for $type {
719+
fn lower_type_kind(&self) -> LowerResult<TypeKind> {
720+
Ok(TypeKind {
721+
sort: TypeSort::$sort,
722+
name: self.name.str.clone(),
723+
binders: chalk_ir::Binders::new(
724+
VariableKinds::from_iter(&ChalkIr, $params(self).anonymize()),
725+
crate::Unit,
726+
),
727+
})
728+
}
729+
}
730+
};
731+
}
732+
733+
lower_type_kind!(AdtDefn, Adt, |defn: &AdtDefn| defn.all_parameters());
734+
lower_type_kind!(FnDefn, FnDef, |defn: &FnDefn| defn.all_parameters());
735+
lower_type_kind!(ClosureDefn, Closure, |defn: &ClosureDefn| defn
736+
.all_parameters());
737+
lower_type_kind!(TraitDefn, Trait, |defn: &TraitDefn| defn
738+
.variable_kinds
739+
.iter()
740+
.map(lower_variable_kind)
741+
.collect::<Vec<_>>());
742+
lower_type_kind!(OpaqueTyDefn, Opaque, |defn: &OpaqueTyDefn| defn
743+
.variable_kinds
744+
.iter()
745+
.map(lower_variable_kind)
746+
.collect::<Vec<_>>());
747+
738748
fn get_type_of_u32() -> chalk_ir::Ty<ChalkIr> {
739749
chalk_ir::ApplicationTy {
740750
name: chalk_ir::TypeName::Scalar(chalk_ir::Scalar::Uint(chalk_ir::UintTy::U32)),
@@ -1713,7 +1723,10 @@ impl Lower for Goal {
17131723
// `if (FromEnv(T: Trait)) { ... /* this part is untouched */ ... }`.
17141724
let where_clauses = hyp
17151725
.iter()
1716-
.flat_map(|clause| apply_result(clause.lower(env)))
1726+
.flat_map(|clause| match clause.lower(env) {
1727+
Ok(v) => v.into_iter().map(Ok).collect(),
1728+
Err(e) => vec![Err(e)],
1729+
})
17171730
.map(|result| result.map(|h| h.into_from_env_clause(interner)));
17181731
let where_clauses =
17191732
chalk_ir::ProgramClauses::from_fallible(interner, where_clauses);
@@ -1752,13 +1765,6 @@ fn lower_quantified(
17521765
Ok(chalk_ir::GoalData::Quantified(quantifier_kind, subgoal).intern(interner))
17531766
}
17541767

1755-
fn apply_result<T>(result: LowerResult<Vec<T>>) -> Vec<LowerResult<T>> {
1756-
match result {
1757-
Ok(v) => v.into_iter().map(Ok).collect(),
1758-
Err(e) => vec![Err(e)],
1759-
}
1760-
}
1761-
17621768
fn lower_well_known_trait(well_known_trait: WellKnownTrait) -> rust_ir::WellKnownTrait {
17631769
match well_known_trait {
17641770
WellKnownTrait::Sized => rust_ir::WellKnownTrait::Sized,

0 commit comments

Comments
 (0)