Browse Source

fix missing getter (#12066)

Aidan Lee 5 months ago
parent
commit
cfb034c1b6

+ 1 - 0
src/generators/cpp/cppAst.ml

@@ -225,6 +225,7 @@ and tcpp_class_variable = {
   tcv_type : t;
   tcv_default : texpr option;
 
+  tcv_has_getter : bool;
   tcv_is_stackonly : bool;
   tcv_is_gc_element : bool;
   tcv_is_reflective : bool;

+ 1 - 0
src/generators/cpp/cppRetyper.ml

@@ -1533,6 +1533,7 @@ let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
     tcv_type = field.cf_type;
     tcv_default = None;
 
+    tcv_has_getter = (match field.cf_kind with | Var { v_read = AccCall } -> true | _ -> false);
     tcv_is_stackonly = has_meta Meta.StackOnly field.cf_meta;
     tcv_is_reflective = reflective class_def field;
     tcv_is_gc_element = cpp_type_of field.cf_type |> is_gc_element ctx;

+ 6 - 7
src/generators/cpp/gen/cppGenClassImplementation.ml

@@ -582,17 +582,16 @@ let generate_managed_class base_ctx tcpp_class =
       value
     in
 
-  let print_variable var_printer get_printer (var:tcpp_class_variable) acc =
+  let print_variable var_printer get_printer var acc =
     if var.tcv_is_reflective && not (is_abstract_impl class_def) then
       let variable = get_wrapper var.tcv_field var.tcv_name in
 
-      match var.tcv_field.cf_kind with
-      | Var { v_read = AccCall } ->
+      if var.tcv_has_getter then
         let prop_check = checkPropCall var.tcv_field in
         let getter     = Printf.sprintf "get_%s()" var.tcv_field.cf_name |> get_wrapper var.tcv_field in
 
         (var.tcv_field.cf_name, String.length var.tcv_field.cf_name, get_printer prop_check getter variable) :: acc
-      | _ ->
+      else
         (var.tcv_field.cf_name, String.length var.tcv_field.cf_name, var_printer variable) :: acc
     else
       acc
@@ -608,11 +607,11 @@ let generate_managed_class base_ctx tcpp_class =
       acc
   in
 
-  let print_property printer (var:tcpp_class_variable) acc =
-    if var.tcv_is_reflective && not (is_abstract_impl class_def) then
+  let print_property printer var acc =
+    if var.tcv_has_getter && var.tcv_is_reflective && not (is_abstract_impl class_def) then (
       let prop_check = checkPropCall var.tcv_field in
       let getter     = Printf.sprintf "get_%s()" var.tcv_field.cf_name |> get_wrapper var.tcv_field in
-      (var.tcv_field.cf_name, String.length var.tcv_field.cf_name, printer prop_check getter) :: acc
+      (var.tcv_field.cf_name, String.length var.tcv_field.cf_name, printer prop_check getter) :: acc)
     else
       acc
   in

+ 19 - 0
tests/unit/src/unit/issues/Issue12058.hx

@@ -0,0 +1,19 @@
+package unit.issues;
+
+import utest.Assert;
+
+private class Foo {
+    var prop(never, set):Int;
+
+	function set_prop(i:Int) {
+		return i;
+	}
+
+    public function new() {}
+}
+
+class Issue12058 extends Test {
+    function test() {
+        Assert.notNull(new Foo());
+    }
+}