@@ -784,15 +784,27 @@ impl<'interner> Monomorphizer<'interner> {
784
784
785
785
let is_closure = self . is_function_closure ( call. func ) ;
786
786
if is_closure {
787
- let extracted_func: ast:: Expression ;
788
- let hir_call_func = self . interner . expression ( & call. func ) ;
789
- if let HirExpression :: Lambda ( l) = hir_call_func {
790
- let ( setup, closure_variable) = self . lambda_with_setup ( l, call. func ) ;
791
- block_expressions. push ( setup) ;
792
- extracted_func = closure_variable;
793
- } else {
794
- extracted_func = * original_func;
795
- }
787
+ let local_id = self . next_local_id ( ) ;
788
+
789
+ // store the function in a temporary variable before calling it
790
+ // this is needed for example if call.func is of the form `foo()()`
791
+ // without this, we would translate it to `foo().1(foo().0)`
792
+ let let_stmt = ast:: Expression :: Let ( ast:: Let {
793
+ id : local_id,
794
+ mutable : false ,
795
+ name : "tmp" . to_string ( ) ,
796
+ expression : Box :: new ( * original_func) ,
797
+ } ) ;
798
+ block_expressions. push ( let_stmt) ;
799
+
800
+ let extracted_func = ast:: Expression :: Ident ( ast:: Ident {
801
+ location : None ,
802
+ definition : Definition :: Local ( local_id) ,
803
+ mutable : false ,
804
+ name : "tmp" . to_string ( ) ,
805
+ typ : Self :: convert_type ( & self . interner . id_type ( call. func ) ) ,
806
+ } ) ;
807
+
796
808
func = Box :: new ( ast:: Expression :: ExtractTupleField (
797
809
Box :: new ( extracted_func. clone ( ) ) ,
798
810
1usize ,
@@ -1435,7 +1447,7 @@ mod tests {
1435
1447
#[ test]
1436
1448
fn simple_closure_with_no_captured_variables ( ) {
1437
1449
let src = r#"
1438
- fn main() -> Field {
1450
+ fn main() -> pub Field {
1439
1451
let x = 1;
1440
1452
let closure = || x;
1441
1453
closure()
@@ -1451,7 +1463,10 @@ mod tests {
1451
1463
};
1452
1464
closure_variable$l2
1453
1465
};
1454
- closure$l3.1(closure$l3.0)
1466
+ {
1467
+ let tmp$4 = closure$l3;
1468
+ tmp$l4.1(tmp$l4.0)
1469
+ }
1455
1470
}
1456
1471
fn lambda$f1(mut env$l1: (Field)) -> Field {
1457
1472
env$l1.0
0 commit comments