Skip to content

Commit

Permalink
[cs] Null cast to Bool is false, not runtime error. Closes HaxeFounda…
Browse files Browse the repository at this point in the history
  • Loading branch information
waneck committed Mar 8, 2014
1 parent 8b633f0 commit 788292a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gencs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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") }, _ )
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions std/cs/internal/Runtime.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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('
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/issues/Issue2725.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 788292a

Please sign in to comment.