Browse Source

[cpp] Add extra casting when performing ops directly on anonymous fields. Closes #6588

hughsando 7 năm trước cách đây
mục cha
commit
69c423fbc3
2 tập tin đã thay đổi với 10 bổ sung1 xóa
  1. 7 1
      src/generators/gencpp.ml
  2. 3 0
      tests/unit/src/unit/TestOps.hx

+ 7 - 1
src/generators/gencpp.ml

@@ -2896,6 +2896,7 @@ let retype_expression ctx request_type function_args function_type expression_tr
             | OpBoolAnd | OpBoolOr -> TCppScalar("bool")
             | OpAnd | OpOr | OpXor | OpShl | OpShr | OpUShr -> TCppScalar("int")
             | OpAssign -> (retype TCppUnchanged left).cpptype
+            | OpMult | OpSub -> cpp_type_of expr.etype
             | _ -> TCppUnchanged
             in
             let e1 = retype binOpType left in
@@ -2938,7 +2939,12 @@ let retype_expression ctx request_type function_args function_type expression_tr
 
                | _ -> CppBinop(op,e1,e2)
             in
-            reference, cpp_type_of expr.etype
+            (match op,e1.cpptype,e2.cpptype  with
+            (* Variant + Variant = Variant *)
+            | OpAdd, _, TCppVariant | OpAdd, TCppVariant, _
+                -> reference, TCppVariant
+            | _,_,_ -> reference, cpp_type_of expr.etype
+            )
 
          | TUnop (op,pre,e1) ->
             let targetType = match op with

+ 3 - 0
tests/unit/src/unit/TestOps.hx

@@ -39,6 +39,7 @@ class TestOps extends Test {
 		eq( 2 << 3 >> 1, 8 );
 		eq( (2 << 3) >> 1, 8 );
 		eq( 2 << (3 >> 1), 4 );
+		eq( (getA().a + 1) >> 1, 1 );
 
 		f( (1 & 0x8000) != 0 );
 		f( 1 & 0x8000 != 0 );
@@ -98,4 +99,6 @@ class TestOps extends Test {
 		eq(5 * @foo @bar 3 + @baz 4, 19);
 	}
 
+	static function getA() return { a:1 };
+
 }