Skip to content

Commit

Permalink
make tnull wrap everything that is not explicitly Null<T> in `Nul…
Browse files Browse the repository at this point in the history
…l<T>`
  • Loading branch information
Simn committed Oct 9, 2014
1 parent ba96ca8 commit c77e8b5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion std/python/_std/EReg.hx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class EReg {
If `s` is null, the result is unspecified.
**/
public function matchSub( s : String, pos : Int, ?len : Int):Bool {
public function matchSub( s : String, pos : Int, len : Int = 0):Bool {
if (len != null) {
matchObj = pattern.search(s, pos, pos+len);
} else {
Expand Down
11 changes: 1 addition & 10 deletions tests/unit/TestType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,8 @@ class TestType extends Test {
var ti1:Array<I1>;
var tbase:Array<Base>;
var tpbase:Array<PClassBase<Float>>;
#if (flash9 || cpp || java || cs)
var tnullbool:Array<Null<Bool>>;
var tnullbase:Array<Null<Base>>;
#else
var tnullbool:Array<Bool>;
var tnullbase:Array<Base>;
#end
var tchild1:Array<Child1>;
var ts:Array<{s:String}>;

Expand All @@ -157,7 +152,7 @@ class TestType extends Test {
typedAs([null, false], tnullbool);
typedAs([false, null], tnullbool);
typedAs([null, new Base()], tnullbase);
//typedAs([new Base(), null], tnullbase); // TODO: this fails on flash9 and cpp
typedAs([new Base(), null], tnullbase);
typedAs([new Base()], tbase);
typedAs([new Base(), new Child1()], tbase);
typedAs([new Child1(), new Base()], tbase);
Expand All @@ -169,11 +164,7 @@ class TestType extends Test {

var tbase:Base;
var ti1:I1;
#if (flash9 || cpp || java || cs)
var tnullbool:Null<Bool>;
#else
var tnullbool:Bool;
#end
var ts: { s:String };

typedAs(if (false) new Child1(); else new Child2(), tbase);
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/issues/Issue3431.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package unit.issues;

private typedef A = {
?f:Array<Int>,
}

class Issue3431 extends Test {
function test() {
var v:A = {};
var r = switch (v) {
case {f: [a,b]}: 1;
default: 2;
}
eq(2, r);
}
}
15 changes: 15 additions & 0 deletions tests/unit/issues/Issue3448.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package unit.issues;

class Issue3448 extends Test {
function test() {
t(f1() != null);
}

static function f1(?pos) {
return f2(pos);
}

static function f2(?p:haxe.PosInfos) {
return p;
}
}
8 changes: 4 additions & 4 deletions type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -556,13 +556,13 @@ let rec follow t =
follow (apply_params t.t_params tl t.t_type)
| _ -> t

let rec is_nullable ?(no_lazy=false) = function
let rec is_nullable = function
| TMono r ->
(match !r with None -> false | Some t -> is_nullable t)
| TType ({ t_path = ([],"Null") },[_]) ->
true
| TLazy f ->
if no_lazy then raise Exit else is_nullable (!f())
is_nullable (!f())
| TType (t,tl) ->
is_nullable (apply_params t.t_params tl t.t_type)
| TFun _ ->
Expand All @@ -583,13 +583,13 @@ let rec is_nullable ?(no_lazy=false) = function
| _ ->
true

let rec is_null = function
let rec is_null ?(no_lazy=false) = function
| TMono r ->
(match !r with None -> false | Some t -> is_null t)
| TType ({ t_path = ([],"Null") },[t]) ->
not (is_nullable (follow t))
| TLazy f ->
is_null (!f())
if no_lazy then raise Exit else is_null (!f())
| TType (t,tl) ->
is_null (apply_params t.t_params tl t.t_type)
| _ ->
Expand Down
6 changes: 3 additions & 3 deletions typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4823,18 +4823,18 @@ let rec create com =
| "Null" ->
let mk_null t =
try
if not (is_nullable ~no_lazy:true t) then TType (td,[t]) else t
if not (is_null ~no_lazy:true t) then TType (td,[t]) else t
with Exit ->
(* don't force lazy evaluation *)
let r = ref (fun() -> assert false) in
r := (fun() ->
let t = (if not (is_nullable t) then TType (td,[t]) else t) in
let t = (if not (is_null t) then TType (td,[t]) else t) in
r := (fun() -> t);
t
);
TLazy r
in
ctx.t.tnull <- if not com.config.pf_static then (fun t -> t) else mk_null;
ctx.t.tnull <- mk_null;
| _ -> ());
) ctx.g.std.m_types;
let m = Typeload.load_module ctx ([],"String") null_pos in
Expand Down

0 comments on commit c77e8b5

Please sign in to comment.