1
1
use chalk_ir:: cast:: { Cast , Caster } ;
2
2
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 } ;
4
4
use chalk_parse:: ast:: * ;
5
5
use chalk_rust_ir as rust_ir;
6
6
use chalk_rust_ir:: { Anonymize , AssociatedTyValueId , IntoWhereClauses , ToParameter } ;
@@ -13,8 +13,12 @@ use crate::program::Program as LoweredProgram;
13
13
use crate :: { Identifier as Ident , RawId , TypeKind , TypeSort } ;
14
14
15
15
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 > > ;
16
18
type TraitIds = BTreeMap < Ident , chalk_ir:: TraitId < ChalkIr > > ;
17
19
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 > ;
18
22
type TraitKinds = BTreeMap < chalk_ir:: TraitId < ChalkIr > , TypeKind > ;
19
23
type AssociatedTyLookups = BTreeMap < ( chalk_ir:: TraitId < ChalkIr > , Ident ) , AssociatedTyLookup > ;
20
24
type AssociatedTyValueIds =
@@ -27,6 +31,10 @@ pub type LowerResult<T> = Result<T, RustIrError>;
27
31
struct Env < ' k > {
28
32
struct_ids : & ' k StructIds ,
29
33
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 ,
30
38
trait_ids : & ' k TraitIds ,
31
39
trait_kinds : & ' k TraitKinds ,
32
40
associated_ty_lookups : & ' k AssociatedTyLookups ,
@@ -222,8 +230,12 @@ impl LowerProgram for Program {
222
230
}
223
231
224
232
let mut struct_ids = BTreeMap :: new ( ) ;
233
+ let mut fn_def_ids = BTreeMap :: new ( ) ;
234
+ let mut closure_ids = BTreeMap :: new ( ) ;
225
235
let mut trait_ids = BTreeMap :: new ( ) ;
226
236
let mut struct_kinds = BTreeMap :: new ( ) ;
237
+ let mut fn_def_kinds = BTreeMap :: new ( ) ;
238
+ let mut closure_kinds = BTreeMap :: new ( ) ;
227
239
let mut trait_kinds = BTreeMap :: new ( ) ;
228
240
for ( item, & raw_id) in self . items . iter ( ) . zip ( & raw_ids) {
229
241
match item {
@@ -233,6 +245,12 @@ impl LowerProgram for Program {
233
245
struct_ids. insert ( type_kind. name , id) ;
234
246
struct_kinds. insert ( id, type_kind) ;
235
247
}
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
+ }
236
254
Item :: TraitDefn ( defn) => {
237
255
let type_kind = defn. lower_type_kind ( ) ?;
238
256
let id = TraitId ( raw_id) ;
@@ -245,6 +263,8 @@ impl LowerProgram for Program {
245
263
}
246
264
247
265
let mut struct_data = BTreeMap :: new ( ) ;
266
+ let mut fn_def_data = BTreeMap :: new ( ) ;
267
+ let mut closure_data = BTreeMap :: new ( ) ;
248
268
let mut trait_data = BTreeMap :: new ( ) ;
249
269
let mut impl_data = BTreeMap :: new ( ) ;
250
270
let mut associated_ty_data = BTreeMap :: new ( ) ;
@@ -254,6 +274,10 @@ impl LowerProgram for Program {
254
274
let empty_env = Env {
255
275
struct_ids : & struct_ids,
256
276
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,
257
281
trait_ids : & trait_ids,
258
282
trait_kinds : & trait_kinds,
259
283
associated_ty_lookups : & associated_ty_lookups,
@@ -265,6 +289,10 @@ impl LowerProgram for Program {
265
289
let struct_id = StructId ( raw_id) ;
266
290
struct_data. insert ( struct_id, Arc :: new ( d. lower_struct ( struct_id, & empty_env) ?) ) ;
267
291
}
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
+ }
268
296
Item :: TraitDefn ( ref trait_defn) => {
269
297
let trait_id = TraitId ( raw_id) ;
270
298
trait_data. insert (
@@ -362,10 +390,16 @@ impl LowerProgram for Program {
362
390
363
391
let program = LoweredProgram {
364
392
struct_ids,
393
+ fn_def_ids,
394
+ closure_ids,
365
395
trait_ids,
366
396
struct_kinds,
397
+ fn_def_kinds,
398
+ closure_kinds,
367
399
trait_kinds,
368
400
struct_data,
401
+ fn_def_data,
402
+ closure_data,
369
403
trait_data,
370
404
impl_data,
371
405
associated_ty_values,
@@ -441,6 +475,16 @@ impl LowerParameterMap for StructDefn {
441
475
}
442
476
}
443
477
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
+
444
488
impl LowerParameterMap for Impl {
445
489
fn synthetic_parameters ( & self ) -> Option < chalk_ir:: ParameterKind < Ident > > {
446
490
None
@@ -528,12 +572,31 @@ impl LowerTypeKind for StructDefn {
528
572
}
529
573
}
530
574
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
+
531
588
impl LowerWhereClauses for StructDefn {
532
589
fn where_clauses ( & self ) -> & [ QuantifiedWhereClause ] {
533
590
& self . where_clauses
534
591
}
535
592
}
536
593
594
+ impl LowerWhereClauses for FnDefn {
595
+ fn where_clauses ( & self ) -> & [ QuantifiedWhereClause ] {
596
+ & self . where_clauses
597
+ }
598
+ }
599
+
537
600
impl LowerTypeKind for TraitDefn {
538
601
fn lower_type_kind ( & self ) -> LowerResult < TypeKind > {
539
602
let binders: Vec < _ > = self . parameter_kinds . iter ( ) . map ( |p| p. lower ( ) ) . collect ( ) ;
@@ -725,6 +788,49 @@ impl LowerStructDefn for StructDefn {
725
788
}
726
789
}
727
790
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
+
728
834
trait LowerTraitRef {
729
835
fn lower ( & self , env : & Env ) -> LowerResult < chalk_ir:: TraitRef < ChalkIr > > ;
730
836
}
@@ -1281,8 +1387,12 @@ impl LowerGoal<LoweredProgram> for Goal {
1281
1387
1282
1388
let env = Env {
1283
1389
struct_ids : & program. struct_ids ,
1390
+ fn_def_ids : & program. fn_def_ids ,
1391
+ closure_ids : & program. closure_ids ,
1284
1392
trait_ids : & program. trait_ids ,
1285
1393
struct_kinds : & program. struct_kinds ,
1394
+ fn_def_kinds : & program. fn_def_kinds ,
1395
+ closure_kinds : & program. closure_kinds ,
1286
1396
trait_kinds : & program. trait_kinds ,
1287
1397
associated_ty_lookups : & associated_ty_lookups,
1288
1398
parameter_map : BTreeMap :: new ( ) ,
0 commit comments