Ver Fonte

[cs] fix comparison of Null<T> with null constant (closes #2474)

add a test for it to ensure it won't be broken
Dan Korostelev há 11 anos atrás
pai
commit
274bd4a602
2 ficheiros alterados com 21 adições e 1 exclusões
  1. 11 1
      gencs.ml
  2. 10 0
      tests/unit/TestCSharp.hx

+ 11 - 1
gencs.ml

@@ -2293,7 +2293,17 @@ let configure gen =
   DynamicOperators.configure gen
     (DynamicOperators.abstract_implementation gen (fun e -> match e.eexpr with
       | TBinop (Ast.OpEq, e1, e2)
-      | TBinop (Ast.OpNotEq, e1, e2) -> should_handle_opeq e1.etype or should_handle_opeq e2.etype
+      | TBinop (Ast.OpNotEq, e1, e2) ->
+        (
+          (* dont touch (v == null) and (null == v) comparisons because they are handled by HardNullableSynf later *)
+          match e1.eexpr, e2.eexpr with
+          | TConst(TNull), _ when is_null_expr e2 ->
+            false
+          | _, TConst(TNull) when is_null_expr e1 ->
+            false
+          | _ ->
+            should_handle_opeq e1.etype or should_handle_opeq e2.etype
+        )
       | TBinop (Ast.OpAssignOp Ast.OpAdd, e1, e2) ->
         is_dynamic_expr e1 || is_null_expr e1 || is_string e.etype
       | TBinop (Ast.OpAdd, e1, e2) -> is_dynamic e1.etype or is_dynamic e2.etype or is_type_param e1.etype or is_type_param e2.etype or is_string e1.etype or is_string e2.etype or is_string e.etype

+ 10 - 0
tests/unit/TestCSharp.hx

@@ -89,6 +89,16 @@ class TestCSharp extends Test
 
 	#end
 
+	// test these because C# generator got a special filter for these expressions
+	public function testNullConstEq()
+	{
+		var a:Null<Int> = 10;
+		f(a == null);
+		f(null == a);
+		t(a != null);
+		f(null != a);
+	}
+
 	#end
 }