@@ -26,6 +26,7 @@ type OpaqueTyIds = BTreeMap<Ident, chalk_ir::OpaqueTyId<ChalkIr>>;
26
26
type AdtKinds = BTreeMap < chalk_ir:: AdtId < ChalkIr > , TypeKind > ;
27
27
type FnDefKinds = BTreeMap < chalk_ir:: FnDefId < ChalkIr > , TypeKind > ;
28
28
type FnDefAbis = BTreeMap < FnDefId < ChalkIr > , <ChalkIr as Interner >:: FnAbi > ;
29
+ type ClosureKinds = BTreeMap < chalk_ir:: ClosureId < ChalkIr > , TypeKind > ;
29
30
type TraitKinds = BTreeMap < chalk_ir:: TraitId < ChalkIr > , TypeKind > ;
30
31
type OpaqueTyKinds = BTreeMap < chalk_ir:: OpaqueTyId < ChalkIr > , TypeKind > ;
31
32
type AssociatedTyLookups = BTreeMap < ( chalk_ir:: TraitId < ChalkIr > , Ident ) , AssociatedTyLookup > ;
@@ -44,6 +45,7 @@ struct Env<'k> {
44
45
fn_def_kinds : & ' k FnDefKinds ,
45
46
fn_def_abis : & ' k FnDefAbis ,
46
47
closure_ids : & ' k ClosureIds ,
48
+ closure_kinds : & ' k ClosureKinds ,
47
49
trait_ids : & ' k TraitIds ,
48
50
trait_kinds : & ' k TraitKinds ,
49
51
opaque_ty_ids : & ' k OpaqueTyIds ,
@@ -82,6 +84,7 @@ struct AssociatedTyLookup {
82
84
enum ApplyTypeLookup {
83
85
Adt ( AdtId < ChalkIr > ) ,
84
86
FnDef ( FnDefId < ChalkIr > ) ,
87
+ Closure ( ClosureId < ChalkIr > ) ,
85
88
Opaque ( OpaqueTyId < ChalkIr > ) ,
86
89
}
87
90
@@ -146,18 +149,8 @@ impl<'k> Env<'k> {
146
149
if let Some ( id) = self . closure_ids . get ( & name. str ) {
147
150
return Ok ( chalk_ir:: TyData :: Apply ( chalk_ir:: ApplicationTy {
148
151
name : chalk_ir:: TypeName :: Closure ( id. clone ( ) ) ,
149
- // XXX closure upvars
150
- substitution : chalk_ir:: Substitution :: from (
151
- interner,
152
- Some (
153
- chalk_ir:: TyData :: Apply ( chalk_ir:: ApplicationTy {
154
- name : chalk_ir:: TypeName :: Tuple ( 0 ) ,
155
- substitution : chalk_ir:: Substitution :: empty ( interner) ,
156
- } )
157
- . intern ( interner)
158
- . cast ( interner) ,
159
- ) ,
160
- ) ,
152
+ // See note in `program`. Unlike rustc, we store upvars separately.
153
+ substitution : chalk_ir:: Substitution :: empty ( interner) ,
161
154
} )
162
155
. intern ( interner)
163
156
. cast ( interner) ) ;
@@ -193,6 +186,10 @@ impl<'k> Env<'k> {
193
186
return Ok ( ApplyTypeLookup :: FnDef ( * id) ) ;
194
187
}
195
188
189
+ if let Some ( id) = self . closure_ids . get ( & name. str ) {
190
+ return Ok ( ApplyTypeLookup :: Closure ( * id) ) ;
191
+ }
192
+
196
193
if let Some ( id) = self . opaque_ty_ids . get ( & name. str ) {
197
194
return Ok ( ApplyTypeLookup :: Opaque ( * id) ) ;
198
195
}
@@ -228,6 +225,10 @@ impl<'k> Env<'k> {
228
225
& self . fn_def_kinds [ & id]
229
226
}
230
227
228
+ fn closure_kind ( & self , id : chalk_ir:: ClosureId < ChalkIr > ) -> & TypeKind {
229
+ & self . closure_kinds [ & id]
230
+ }
231
+
231
232
fn opaque_kind ( & self , id : chalk_ir:: OpaqueTyId < ChalkIr > ) -> & TypeKind {
232
233
& self . opaque_ty_kinds [ & id]
233
234
}
@@ -346,6 +347,7 @@ impl LowerProgram for Program {
346
347
let mut adt_kinds = BTreeMap :: new ( ) ;
347
348
let mut fn_def_kinds = BTreeMap :: new ( ) ;
348
349
let mut fn_def_abis = BTreeMap :: new ( ) ;
350
+ let mut closure_kinds = BTreeMap :: new ( ) ;
349
351
let mut trait_kinds = BTreeMap :: new ( ) ;
350
352
let mut opaque_ty_kinds = BTreeMap :: new ( ) ;
351
353
let mut object_safe_traits = HashSet :: new ( ) ;
@@ -365,8 +367,10 @@ impl LowerProgram for Program {
365
367
fn_def_abis. insert ( id, defn. abi . lower ( ) ?) ;
366
368
}
367
369
Item :: ClosureDefn ( defn) => {
370
+ let type_kind = defn. lower_type_kind ( ) ?;
368
371
let id = ClosureId ( raw_id) ;
369
372
closure_ids. insert ( defn. name . str . clone ( ) , id) ;
373
+ closure_kinds. insert ( id, type_kind) ;
370
374
}
371
375
Item :: TraitDefn ( defn) => {
372
376
let type_kind = defn. lower_type_kind ( ) ?;
@@ -392,6 +396,7 @@ impl LowerProgram for Program {
392
396
let mut adt_data = BTreeMap :: new ( ) ;
393
397
let mut fn_def_data = BTreeMap :: new ( ) ;
394
398
let mut closure_data = BTreeMap :: new ( ) ;
399
+ let mut closure_upvars = BTreeMap :: new ( ) ;
395
400
let mut trait_data = BTreeMap :: new ( ) ;
396
401
let mut well_known_traits = BTreeMap :: new ( ) ;
397
402
let mut impl_data = BTreeMap :: new ( ) ;
@@ -408,6 +413,7 @@ impl LowerProgram for Program {
408
413
fn_def_kinds : & fn_def_kinds,
409
414
fn_def_abis : & fn_def_abis,
410
415
closure_ids : & closure_ids,
416
+ closure_kinds : & closure_kinds,
411
417
trait_ids : & trait_ids,
412
418
trait_kinds : & trait_kinds,
413
419
opaque_ty_ids : & opaque_ty_ids,
@@ -434,6 +440,20 @@ impl LowerProgram for Program {
434
440
closure_def_id,
435
441
Arc :: new ( defn. lower_closure ( closure_def_id, & empty_env) ?) ,
436
442
) ;
443
+ let upvars = empty_env. in_binders ( defn. all_parameters ( ) , |env| {
444
+ let upvar_tys: LowerResult < Vec < chalk_ir:: Ty < ChalkIr > > > =
445
+ defn. upvars . iter ( ) . map ( |ty| ty. lower ( & env) ) . collect ( ) ;
446
+ let substitution = chalk_ir:: Substitution :: from (
447
+ & ChalkIr ,
448
+ upvar_tys?. into_iter ( ) . map ( |ty| ty. cast ( & ChalkIr ) ) ,
449
+ ) ;
450
+ Ok ( chalk_ir:: TyData :: Apply ( chalk_ir:: ApplicationTy {
451
+ name : chalk_ir:: TypeName :: Tuple ( defn. upvars . len ( ) ) ,
452
+ substitution,
453
+ } )
454
+ . intern ( & ChalkIr ) )
455
+ } ) ?;
456
+ closure_upvars. insert ( closure_def_id, upvars) ;
437
457
}
438
458
Item :: TraitDefn ( ref trait_defn) => {
439
459
let trait_id = TraitId ( raw_id) ;
@@ -593,6 +613,8 @@ impl LowerProgram for Program {
593
613
adt_ids,
594
614
fn_def_ids,
595
615
closure_ids,
616
+ closure_upvars,
617
+ closure_kinds,
596
618
trait_ids,
597
619
adt_kinds,
598
620
fn_def_kinds,
@@ -695,6 +717,16 @@ impl LowerParameterMap for FnDefn {
695
717
}
696
718
}
697
719
720
+ impl LowerParameterMap for ClosureDefn {
721
+ fn synthetic_parameters ( & self ) -> Option < chalk_ir:: WithKind < ChalkIr , Ident > > {
722
+ None
723
+ }
724
+
725
+ fn declared_parameters ( & self ) -> & [ VariableKind ] {
726
+ & self . variable_kinds
727
+ }
728
+ }
729
+
698
730
impl LowerParameterMap for Impl {
699
731
fn synthetic_parameters ( & self ) -> Option < chalk_ir:: WithKind < ChalkIr , Ident > > {
700
732
None
@@ -832,6 +864,20 @@ impl LowerWhereClauses for FnDefn {
832
864
}
833
865
}
834
866
867
+ impl LowerTypeKind for ClosureDefn {
868
+ fn lower_type_kind ( & self ) -> LowerResult < TypeKind > {
869
+ let interner = & ChalkIr ;
870
+ Ok ( TypeKind {
871
+ sort : TypeSort :: Closure ,
872
+ name : self . name . str . clone ( ) ,
873
+ binders : chalk_ir:: Binders :: new (
874
+ chalk_ir:: VariableKinds :: from ( interner, self . all_parameters ( ) . anonymize ( ) ) ,
875
+ crate :: Unit ,
876
+ ) ,
877
+ } )
878
+ }
879
+ }
880
+
835
881
impl LowerWhereClauses for StructDefn {
836
882
fn where_clauses ( & self ) -> & [ QuantifiedWhereClause ] {
837
883
& self . where_clauses
@@ -1123,7 +1169,7 @@ impl LowerClosureDefn for ClosureDefn {
1123
1169
closure_id : chalk_ir:: ClosureId < ChalkIr > ,
1124
1170
env : & Env ,
1125
1171
) -> LowerResult < rust_ir:: ClosureDatum < ChalkIr > > {
1126
- let inputs_and_output = env. in_binders ( vec ! [ ] , |env| {
1172
+ let inputs_and_output = env. in_binders ( self . all_parameters ( ) , |env| {
1127
1173
let args: LowerResult < _ > = self . argument_types . iter ( ) . map ( |t| t. lower ( env) ) . collect ( ) ;
1128
1174
let return_type = self . return_type . lower ( env) ?;
1129
1175
Ok ( rust_ir:: FnDefInputsAndOutputDatum {
@@ -1453,6 +1499,9 @@ impl LowerTy for Ty {
1453
1499
ApplyTypeLookup :: FnDef ( id) => {
1454
1500
( chalk_ir:: TypeName :: FnDef ( id) , env. fn_def_kind ( id) )
1455
1501
}
1502
+ ApplyTypeLookup :: Closure ( id) => {
1503
+ ( chalk_ir:: TypeName :: Closure ( id) , env. closure_kind ( id) )
1504
+ }
1456
1505
ApplyTypeLookup :: Opaque ( id) => {
1457
1506
( chalk_ir:: TypeName :: OpaqueType ( id) , env. opaque_kind ( id) )
1458
1507
}
@@ -1852,6 +1901,7 @@ impl LowerGoal<LoweredProgram> for Goal {
1852
1901
adt_kinds : & program. adt_kinds ,
1853
1902
fn_def_kinds : & program. fn_def_kinds ,
1854
1903
fn_def_abis : & fn_def_abis,
1904
+ closure_kinds : & program. closure_kinds ,
1855
1905
trait_kinds : & program. trait_kinds ,
1856
1906
opaque_ty_kinds : & program. opaque_ty_kinds ,
1857
1907
associated_ty_lookups : & associated_ty_lookups,
0 commit comments