فهرست منبع

make `tnull` wrap everything that is not explicitly `Null<T>` in `Null<T>`

Simon Krajewski 11 سال پیش
والد
کامیت
c77e8b52d7
6فایلهای تغییر یافته به همراه40 افزوده شده و 18 حذف شده
  1. 1 1
      std/python/_std/EReg.hx
  2. 1 10
      tests/unit/TestType.hx
  3. 16 0
      tests/unit/issues/Issue3431.hx
  4. 15 0
      tests/unit/issues/Issue3448.hx
  5. 4 4
      type.ml
  6. 3 3
      typer.ml

+ 1 - 1
std/python/_std/EReg.hx

@@ -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 {

+ 1 - 10
tests/unit/TestType.hx

@@ -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}>;
 
@@ -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);
@@ -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);

+ 16 - 0
tests/unit/issues/Issue3431.hx

@@ -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 - 0
tests/unit/issues/Issue3448.hx

@@ -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;
+	}
+}

+ 4 - 4
type.ml

@@ -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 _ ->
@@ -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)
 	| _ ->

+ 3 - 3
typer.ml

@@ -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