فهرست منبع

[cs] maintain binop types nullability when -D fast-cast
(fixes #8242)

Aleksandr Kuzmenko 6 سال پیش
والد
کامیت
b71cda17da
3فایلهای تغییر یافته به همراه37 افزوده شده و 1 حذف شده
  1. 11 1
      src/codegen/gencommon/castDetect.ml
  2. 12 0
      src/core/type.ml
  3. 14 0
      tests/unit/src/unit/issues/Issue8242.hx

+ 11 - 1
src/codegen/gencommon/castDetect.ml

@@ -953,7 +953,7 @@ let configure gen ?(overloads_cast_to_base = false) maybe_empty_t calls_paramete
 			* Otherwise, both operands are converted to type int.
 			*  *)
 		let t1, t2 = follow (run_follow gen e1.etype), follow (run_follow gen e2.etype) in
-		match t1, t2 with
+		let result = match t1, t2 with
 			| TAbstract(a1,[]), TAbstract(a2,[]) when a1 == a2 ->
 				{ main_expr with eexpr = TBinop(op, e1, e2); etype = e1.etype }
 			| TInst(i1,[]), TInst(i2,[]) when i1 == i2 ->
@@ -1000,6 +1000,16 @@ let configure gen ?(overloads_cast_to_base = false) maybe_empty_t calls_paramete
 				{ main_expr with eexpr = TBinop(op, e1, e2); etype = basic.tint }
 			| _ ->
 				{ main_expr with eexpr = TBinop(op, e1, e2) }
+	in
+		(* maintain nullability *)
+		match follow_without_null main_expr.etype, follow_without_null result.etype with
+		| TAbstract ({ a_path = ([],"Null") },_), TAbstract ({ a_path = ([],"Null") },_) ->
+			result
+		| TAbstract ({ a_path = ([],"Null") } as null,_), _ ->
+			{ result with etype = TAbstract(null, [result.etype]) }
+		| _, TAbstract ({ a_path = ([],"Null") },[t]) ->
+			{ result with etype = t }
+		| _ -> result
 	in
 	let binop_type = if Common.defined gen.gcon Define.FastCast then
 		binop_type

+ 12 - 0
src/core/type.ml

@@ -807,6 +807,18 @@ let rec follow t =
 		follow t
 	| _ -> t
 
+let rec follow_without_null t =
+	match t with
+	| TMono r ->
+		(match !r with
+		| Some t -> follow t
+		| _ -> t)
+	| TLazy f ->
+		follow (lazy_type f)
+	| TType (t,tl) ->
+		follow (apply_params t.t_params tl t.t_type)
+	| _ -> t
+
 (** Assumes `follow` has already been applied *)
 let rec ambiguate_funs t =
 	match t with

+ 14 - 0
tests/unit/src/unit/issues/Issue8242.hx

@@ -0,0 +1,14 @@
+package unit.issues;
+
+class Issue8242 extends unit.Test {
+	@:analyzer(ignore)
+	public function test() {
+		var num:Null<Int> = 22;
+		var i = (num - 1) * 2;
+		eq(42, i);
+
+		var num:Null<Float> = 22;
+		var i = (num - 1) * 2;
+		eq(42.0, i);
+	}
+}