Skip to content

Commit 0fe8a29

Browse files
committed
Add functions and closures to TypeName
1 parent 039fc90 commit 0fe8a29

File tree

15 files changed

+532
-11
lines changed

15 files changed

+532
-11
lines changed

chalk-integration/src/db.rs

+12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use chalk_ir::interner::ChalkIr;
77
use chalk_ir::tls;
88
use chalk_ir::AssocTypeId;
99
use chalk_ir::Canonical;
10+
use chalk_ir::ClosureId;
1011
use chalk_ir::ConstrainedSubst;
12+
use chalk_ir::FnDefId;
1113
use chalk_ir::Goal;
1214
use chalk_ir::ImplId;
1315
use chalk_ir::InEnvironment;
@@ -20,6 +22,8 @@ use chalk_ir::UCanonical;
2022
use chalk_rust_ir::AssociatedTyDatum;
2123
use chalk_rust_ir::AssociatedTyValue;
2224
use chalk_rust_ir::AssociatedTyValueId;
25+
use chalk_rust_ir::ClosureDatum;
26+
use chalk_rust_ir::FnDefDatum;
2327
use chalk_rust_ir::ImplDatum;
2428
use chalk_rust_ir::StructDatum;
2529
use chalk_rust_ir::TraitDatum;
@@ -111,6 +115,14 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
111115
self.program_ir().unwrap().as_struct_id(type_name)
112116
}
113117

118+
fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
119+
self.program_ir().unwrap().fn_def_datum(id)
120+
}
121+
122+
fn closure_datum(&self, id: ClosureId<ChalkIr>) -> Arc<ClosureDatum<ChalkIr>> {
123+
self.program_ir().unwrap().closure_datum(id)
124+
}
125+
114126
fn impls_for_trait(
115127
&self,
116128
trait_id: TraitId<ChalkIr>,

chalk-integration/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use chalk_ir::Binders;
1717
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1818
pub enum TypeSort {
1919
Struct,
20+
Function,
2021
Trait,
2122
}
2223

chalk-integration/src/lowering.rs

+111-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use chalk_ir::cast::{Cast, Caster};
22
use chalk_ir::interner::ChalkIr;
3-
use chalk_ir::{self, AssocTypeId, BoundVar, DebruijnIndex, ImplId, StructId, TraitId};
3+
use chalk_ir::{self, AssocTypeId, BoundVar, DebruijnIndex, FnDefId, ImplId, StructId, TraitId};
44
use chalk_parse::ast::*;
55
use chalk_rust_ir as rust_ir;
66
use chalk_rust_ir::{Anonymize, AssociatedTyValueId, IntoWhereClauses, ToParameter};
@@ -13,8 +13,12 @@ use crate::program::Program as LoweredProgram;
1313
use crate::{Identifier as Ident, RawId, TypeKind, TypeSort};
1414

1515
type StructIds = BTreeMap<Ident, chalk_ir::StructId<ChalkIr>>;
16+
type FnDefIds = BTreeMap<Ident, chalk_ir::FnDefId<ChalkIr>>;
17+
type ClosureIds = BTreeMap<Ident, chalk_ir::ClosureId<ChalkIr>>;
1618
type TraitIds = BTreeMap<Ident, chalk_ir::TraitId<ChalkIr>>;
1719
type StructKinds = BTreeMap<chalk_ir::StructId<ChalkIr>, TypeKind>;
20+
type FnDefKinds = BTreeMap<chalk_ir::FnDefId<ChalkIr>, TypeKind>;
21+
type ClosureKinds = BTreeMap<chalk_ir::ClosureId<ChalkIr>, TypeKind>;
1822
type TraitKinds = BTreeMap<chalk_ir::TraitId<ChalkIr>, TypeKind>;
1923
type AssociatedTyLookups = BTreeMap<(chalk_ir::TraitId<ChalkIr>, Ident), AssociatedTyLookup>;
2024
type AssociatedTyValueIds =
@@ -27,6 +31,10 @@ pub type LowerResult<T> = Result<T, RustIrError>;
2731
struct Env<'k> {
2832
struct_ids: &'k StructIds,
2933
struct_kinds: &'k StructKinds,
34+
fn_def_ids: &'k FnDefIds,
35+
fn_def_kinds: &'k FnDefKinds,
36+
closure_ids: &'k ClosureIds,
37+
closure_kinds: &'k ClosureKinds,
3038
trait_ids: &'k TraitIds,
3139
trait_kinds: &'k TraitKinds,
3240
associated_ty_lookups: &'k AssociatedTyLookups,
@@ -222,8 +230,12 @@ impl LowerProgram for Program {
222230
}
223231

224232
let mut struct_ids = BTreeMap::new();
233+
let mut fn_def_ids = BTreeMap::new();
234+
let mut closure_ids = BTreeMap::new();
225235
let mut trait_ids = BTreeMap::new();
226236
let mut struct_kinds = BTreeMap::new();
237+
let mut fn_def_kinds = BTreeMap::new();
238+
let mut closure_kinds = BTreeMap::new();
227239
let mut trait_kinds = BTreeMap::new();
228240
for (item, &raw_id) in self.items.iter().zip(&raw_ids) {
229241
match item {
@@ -233,6 +245,12 @@ impl LowerProgram for Program {
233245
struct_ids.insert(type_kind.name, id);
234246
struct_kinds.insert(id, type_kind);
235247
}
248+
Item::FnDefn(defn) => {
249+
let type_kind = defn.lower_type_kind()?;
250+
let id = FnDefId(raw_id);
251+
fn_def_ids.insert(type_kind.name, id);
252+
fn_def_kinds.insert(id, type_kind);
253+
}
236254
Item::TraitDefn(defn) => {
237255
let type_kind = defn.lower_type_kind()?;
238256
let id = TraitId(raw_id);
@@ -245,6 +263,8 @@ impl LowerProgram for Program {
245263
}
246264

247265
let mut struct_data = BTreeMap::new();
266+
let mut fn_def_data = BTreeMap::new();
267+
let mut closure_data = BTreeMap::new();
248268
let mut trait_data = BTreeMap::new();
249269
let mut impl_data = BTreeMap::new();
250270
let mut associated_ty_data = BTreeMap::new();
@@ -254,6 +274,10 @@ impl LowerProgram for Program {
254274
let empty_env = Env {
255275
struct_ids: &struct_ids,
256276
struct_kinds: &struct_kinds,
277+
fn_def_ids: &fn_def_ids,
278+
fn_def_kinds: &fn_def_kinds,
279+
closure_ids: &closure_ids,
280+
closure_kinds: &closure_kinds,
257281
trait_ids: &trait_ids,
258282
trait_kinds: &trait_kinds,
259283
associated_ty_lookups: &associated_ty_lookups,
@@ -265,6 +289,10 @@ impl LowerProgram for Program {
265289
let struct_id = StructId(raw_id);
266290
struct_data.insert(struct_id, Arc::new(d.lower_struct(struct_id, &empty_env)?));
267291
}
292+
Item::FnDefn(ref d) => {
293+
let fn_def_id = FnDefId(raw_id);
294+
fn_def_data.insert(fn_def_id, Arc::new(d.lower_fn(fn_def_id, &empty_env)?));
295+
}
268296
Item::TraitDefn(ref trait_defn) => {
269297
let trait_id = TraitId(raw_id);
270298
trait_data.insert(
@@ -362,10 +390,16 @@ impl LowerProgram for Program {
362390

363391
let program = LoweredProgram {
364392
struct_ids,
393+
fn_def_ids,
394+
closure_ids,
365395
trait_ids,
366396
struct_kinds,
397+
fn_def_kinds,
398+
closure_kinds,
367399
trait_kinds,
368400
struct_data,
401+
fn_def_data,
402+
closure_data,
369403
trait_data,
370404
impl_data,
371405
associated_ty_values,
@@ -441,6 +475,16 @@ impl LowerParameterMap for StructDefn {
441475
}
442476
}
443477

478+
impl LowerParameterMap for FnDefn {
479+
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
480+
None
481+
}
482+
483+
fn declared_parameters(&self) -> &[ParameterKind] {
484+
&self.parameter_kinds
485+
}
486+
}
487+
444488
impl LowerParameterMap for Impl {
445489
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
446490
None
@@ -528,12 +572,31 @@ impl LowerTypeKind for StructDefn {
528572
}
529573
}
530574

575+
impl LowerTypeKind for FnDefn {
576+
fn lower_type_kind(&self) -> LowerResult<TypeKind> {
577+
Ok(TypeKind {
578+
sort: TypeSort::Function,
579+
name: self.name.str,
580+
binders: chalk_ir::Binders {
581+
binders: self.all_parameters().anonymize(),
582+
value: (),
583+
},
584+
})
585+
}
586+
}
587+
531588
impl LowerWhereClauses for StructDefn {
532589
fn where_clauses(&self) -> &[QuantifiedWhereClause] {
533590
&self.where_clauses
534591
}
535592
}
536593

594+
impl LowerWhereClauses for FnDefn {
595+
fn where_clauses(&self) -> &[QuantifiedWhereClause] {
596+
&self.where_clauses
597+
}
598+
}
599+
537600
impl LowerTypeKind for TraitDefn {
538601
fn lower_type_kind(&self) -> LowerResult<TypeKind> {
539602
let binders: Vec<_> = self.parameter_kinds.iter().map(|p| p.lower()).collect();
@@ -725,6 +788,49 @@ impl LowerStructDefn for StructDefn {
725788
}
726789
}
727790

791+
trait LowerFnDefn {
792+
fn lower_fn(
793+
&self,
794+
struct_id: chalk_ir::FnDefId<ChalkIr>,
795+
env: &Env,
796+
) -> LowerResult<rust_ir::FnDefDatum<ChalkIr>>;
797+
}
798+
799+
impl LowerFnDefn for FnDefn {
800+
fn lower_fn(
801+
&self,
802+
fn_def_id: chalk_ir::FnDefId<ChalkIr>,
803+
env: &Env,
804+
) -> LowerResult<rust_ir::FnDefDatum<ChalkIr>> {
805+
if self.flags.fundamental && self.all_parameters().len() != 1 {
806+
Err(RustIrError::InvalidFundamentalTypesParameters(self.name))?;
807+
}
808+
809+
let binders = env.in_binders(self.all_parameters(), |env| {
810+
let fn_params: LowerResult<_> = self.params.iter().map(|f| f.ty.lower(env)).collect();
811+
let fn_returns: LowerResult<_> = self.returns.iter().map(|f| f.ty.lower(env)).collect();
812+
let where_clauses = self.lower_where_clauses(env)?;
813+
814+
Ok(rust_ir::FnDefDatumBound {
815+
params: fn_params?,
816+
returns: fn_returns?,
817+
where_clauses,
818+
})
819+
})?;
820+
821+
let flags = rust_ir::FnDefFlags {
822+
upstream: self.flags.upstream,
823+
fundamental: self.flags.fundamental,
824+
};
825+
826+
Ok(rust_ir::FnDefDatum {
827+
id: fn_def_id,
828+
binders,
829+
flags,
830+
})
831+
}
832+
}
833+
728834
trait LowerTraitRef {
729835
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::TraitRef<ChalkIr>>;
730836
}
@@ -1281,8 +1387,12 @@ impl LowerGoal<LoweredProgram> for Goal {
12811387

12821388
let env = Env {
12831389
struct_ids: &program.struct_ids,
1390+
fn_def_ids: &program.fn_def_ids,
1391+
closure_ids: &program.closure_ids,
12841392
trait_ids: &program.trait_ids,
12851393
struct_kinds: &program.struct_kinds,
1394+
fn_def_kinds: &program.fn_def_kinds,
1395+
closure_kinds: &program.closure_kinds,
12861396
trait_kinds: &program.trait_kinds,
12871397
associated_ty_lookups: &associated_ty_lookups,
12881398
parameter_map: BTreeMap::new(),

chalk-integration/src/program.rs

+59-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use chalk_ir::debug::Angle;
44
use chalk_ir::interner::ChalkIr;
55
use chalk_ir::tls;
66
use chalk_ir::{
7-
debug::SeparatorTraitRef, AliasTy, ApplicationTy, AssocTypeId, Goal, Goals, ImplId, Lifetime,
8-
Parameter, ProgramClause, ProgramClauseImplication, StructId, Substitution, TraitId, Ty,
9-
TyData, TypeName,
7+
debug::SeparatorTraitRef, AliasTy, ApplicationTy, AssocTypeId, ClosureId, FnDefId, Goal, Goals,
8+
ImplId, Lifetime, Parameter, ProgramClause, ProgramClauseImplication, StructId, Substitution,
9+
TraitId, Ty, TyData, TypeName,
1010
};
1111
use chalk_rust_ir::{
12-
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ImplDatum, ImplType, StructDatum,
13-
TraitDatum,
12+
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureDatum, FnDefDatum, ImplDatum,
13+
ImplType, StructDatum, TraitDatum,
1414
};
1515
use chalk_solve::split::Split;
1616
use chalk_solve::RustIrDatabase;
@@ -26,6 +26,18 @@ pub struct Program {
2626
/// For each struct:
2727
pub struct_kinds: BTreeMap<StructId<ChalkIr>, TypeKind>,
2828

29+
/// From function name to item-id. Used during lowering only.
30+
pub fn_def_ids: BTreeMap<Identifier, FnDefId<ChalkIr>>,
31+
32+
/// For each function definition:
33+
pub fn_def_kinds: BTreeMap<FnDefId<ChalkIr>, TypeKind>,
34+
35+
/// From closure to item-id. Used during lowering only.
36+
pub closure_ids: BTreeMap<Identifier, ClosureId<ChalkIr>>,
37+
38+
/// For each closure:
39+
pub closure_kinds: BTreeMap<ClosureId<ChalkIr>, TypeKind>,
40+
2941
/// From trait name to item-id. Used during lowering only.
3042
pub trait_ids: BTreeMap<Identifier, TraitId<ChalkIr>>,
3143

@@ -35,6 +47,12 @@ pub struct Program {
3547
/// For each struct:
3648
pub struct_data: BTreeMap<StructId<ChalkIr>, Arc<StructDatum<ChalkIr>>>,
3749

50+
/// For each function definition:
51+
pub fn_def_data: BTreeMap<FnDefId<ChalkIr>, Arc<FnDefDatum<ChalkIr>>>,
52+
53+
/// For each closure:
54+
pub closure_data: BTreeMap<ClosureId<ChalkIr>, Arc<ClosureDatum<ChalkIr>>>,
55+
3856
/// For each impl:
3957
pub impl_data: BTreeMap<ImplId<ChalkIr>, Arc<ImplDatum<ChalkIr>>>,
4058

@@ -78,6 +96,34 @@ impl tls::DebugContext for Program {
7896
}
7997
}
8098

99+
fn debug_fn_def_id(
100+
&self,
101+
fn_def_id: FnDefId<ChalkIr>,
102+
fmt: &mut fmt::Formatter<'_>,
103+
) -> Result<(), fmt::Error> {
104+
if let Some(k) = self.fn_def_kinds.get(&fn_def_id) {
105+
write!(fmt, "{}", k.name)
106+
} else {
107+
fmt.debug_struct("InvalidFnDefId")
108+
.field("index", &fn_def_id.0)
109+
.finish()
110+
}
111+
}
112+
113+
fn debug_closure_id(
114+
&self,
115+
closure_id: ClosureId<ChalkIr>,
116+
fmt: &mut fmt::Formatter<'_>,
117+
) -> Result<(), fmt::Error> {
118+
if let Some(k) = self.closure_kinds.get(&closure_id) {
119+
write!(fmt, "{}", k.name)
120+
} else {
121+
fmt.debug_struct("InvalidClosureId")
122+
.field("index", &closure_id.0)
123+
.finish()
124+
}
125+
}
126+
81127
fn debug_trait_id(
82128
&self,
83129
trait_id: TraitId<ChalkIr>,
@@ -236,6 +282,14 @@ impl RustIrDatabase<ChalkIr> for Program {
236282
}
237283
}
238284

285+
fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
286+
self.fn_def_data[&id].clone()
287+
}
288+
289+
fn closure_datum(&self, id: ClosureId<ChalkIr>) -> Arc<ClosureDatum<ChalkIr>> {
290+
self.closure_data[&id].clone()
291+
}
292+
239293
fn impls_for_trait(
240294
&self,
241295
trait_id: TraitId<ChalkIr>,

chalk-ir/src/cast.rs

+18
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,24 @@ where
296296
}
297297
}
298298

299+
impl<I> CastTo<TypeName<I>> for FnDefId<I>
300+
where
301+
I: Interner,
302+
{
303+
fn cast_to(self, _interner: &I) -> TypeName<I> {
304+
TypeName::FnDef(self)
305+
}
306+
}
307+
308+
impl<I> CastTo<TypeName<I>> for ClosureId<I>
309+
where
310+
I: Interner,
311+
{
312+
fn cast_to(self, _interner: &I) -> TypeName<I> {
313+
TypeName::Closure(self)
314+
}
315+
}
316+
299317
impl<T> CastTo<T> for &T
300318
where
301319
T: Clone + HasInterner,

0 commit comments

Comments
 (0)