Skip to content

Commit

Permalink
[cpp] Fix return typing for embedded closures. Closes #6121
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsando committed Mar 22, 2017
1 parent 76c6620 commit e5c9d39
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/generators/gencpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/src/unit/issues/Issue6121.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit e5c9d39

Please sign in to comment.