2
0
Эх сурвалжийг харах

[cpp] Fix return typing for embedded closures. Closes #6121

Hugh 8 жил өмнө
parent
commit
f2686728fb

+ 7 - 2
src/generators/gencpp.ml

@@ -2327,6 +2327,7 @@ let retype_expression ctx request_type function_args function_type expression_tr
    let injection = ref forInjection in
    let this_real = ref (if ctx.ctx_real_this_ptr then ThisReal else ThisDynamic) in
    let file_id = ctx.ctx_file_id in
+   let function_return_type = ref (cpp_type_of ctx function_type) in
    let loop_stack = ref [] in
    let alloc_file_id () =
       incr file_id;
@@ -2740,6 +2741,9 @@ let retype_expression ctx request_type function_args function_type expression_tr
             let old_declarations = Hashtbl.copy !declarations in
             let old_uses_this = !uses_this in
             let old_gc_stack = !gc_stack in
+            let old_return_type = !function_return_type in
+            let ret =cpp_type_of func.tf_type in
+            function_return_type := ret;
             uses_this := None;
             undeclared := Hashtbl.create 0;
             declarations := Hashtbl.create 0;
@@ -2749,7 +2753,7 @@ let retype_expression ctx request_type function_args function_type expression_tr
             let result = { close_expr=cppExpr;
                            close_id= !closureId;
                            close_undeclared= !undeclared;
-                           close_type= cpp_type_of func.tf_type;
+                           close_type= ret;
                            close_args= func.tf_args;
                            close_this= !uses_this;
                          } in
@@ -2760,6 +2764,7 @@ let retype_expression ctx request_type function_args function_type expression_tr
                if not (Hashtbl.mem !declarations name) then
                   Hashtbl.replace !undeclared name tvar;
             ) result.close_undeclared;
+            function_return_type := old_return_type;
             this_real := old_this_real;
             uses_this := if !uses_this != None then Some old_this_real else old_uses_this;
             gc_stack := old_gc_stack;
@@ -2975,7 +2980,7 @@ let retype_expression ctx request_type function_args function_type expression_tr
             CppTry(cppBlock, cppCatches), TCppVoid
 
          | TReturn eo ->
-            CppReturn(match eo with None -> None | Some e -> Some (retype (cpp_type_of function_type) e)), TCppVoid
+            CppReturn(match eo with None -> None | Some e -> Some (retype (!function_return_type) e)), TCppVoid
 
          | TCast (base,None) -> (* Use auto-cast rules *)
             let return_type = cpp_type_of expr.etype in

+ 29 - 0
tests/unit/src/unit/issues/Issue6121.hx

@@ -0,0 +1,29 @@
+package unit.issues;
+
+class Issue6121 extends unit.Test {
+
+   public function add(t:Issue6121) : Issue6121 return this;
+
+   public function start(run:Void->Issue6121) : Issue6121 return null;
+
+   static public function setCallback( cb:Void->Issue6121 ) : Issue6121 return null;
+
+   function callFunction() : Int
+   {
+      setCallback(function () return add(setCallback(function() return start(function() return null))));
+      return 1;
+   }
+
+   function callBind() : Int
+   {
+      setCallback(function () return add(setCallback(start.bind(function() return null))));
+      return 2;
+   }
+
+
+	function test() {
+		eq(callFunction(),1);
+		eq(callBind(),2);
+	}
+}
+