Browse Source

[cs] Null cast to Bool is false, not runtime error. Closes #2725

Cauê Waneck 11 years ago
parent
commit
788292a404
3 changed files with 37 additions and 0 deletions
  1. 17 0
      gencs.ml
  2. 8 0
      std/cs/internal/Runtime.hx
  3. 12 0
      tests/unit/issues/Issue2725.hx

+ 17 - 0
gencs.ml

@@ -115,6 +115,14 @@ let rec is_int_float t =
     | TInst( { cl_path = (["haxe"; "lang"], "Null") }, [t] ) -> is_int_float t
     | _ -> false
 
+let is_bool t =
+  match follow t with
+    | TEnum( { e_path = ([], "Bool") }, [] )
+    | TAbstract ({ a_path = ([], "Bool") },[]) ->
+      true
+    | _ -> false
+
+
 let rec is_null t =
   match t with
     | TInst( { cl_path = (["haxe"; "lang"], "Null") }, _ )
@@ -416,6 +424,15 @@ struct
           run ef
         | TNew( { cl_path = ([], "String") }, [], [p] ) -> run p (* new String(myString) -> myString *)
 
+        | TCast(expr, _) when is_bool e.etype ->
+          {
+            eexpr = TCall(
+              mk_static_field_access_infer runtime_cl "toBool" expr.epos [],
+              [ run expr ]
+            );
+            etype = basic.tbool;
+            epos = e.epos
+          }
         | TCast(expr, _) when is_int_float e.etype && not (is_int_float expr.etype) && not (is_null e.etype) ->
           let needs_cast = match gen.gfollow#run_f e.etype with
             | TInst _ -> false

+ 8 - 0
std/cs/internal/Runtime.hx

@@ -772,6 +772,14 @@ import cs.system.Type;
 		return null;
 	}
 
+	@:functionCode('
+		return dyn == null ? false : ((bool) dyn);
+	')
+	public static function toBool(dyn:Dynamic):Bool
+	{
+		return false;
+	}
+
 
 	//TODO: change from genericCast to getConverter, so we don't need to handle extra boxing associated with it
 	/*@:functionCode('

+ 12 - 0
tests/unit/issues/Issue2725.hx

@@ -0,0 +1,12 @@
+package unit.issues;
+
+class Issue2725 extends unit.Test
+{
+	public function test()
+	{
+		var x:Dynamic = null;
+		var b:Bool = x;
+		f(b);
+		t(!b);
+	}
+}