瀏覽代碼

[php] fix -2147483648 as init value of static vars (#5289)

Aleksandr Kuzmenko 6 年之前
父節點
當前提交
bbbf55752b
共有 2 個文件被更改,包括 31 次插入17 次删除
  1. 22 17
      src/generators/genphp7.ml
  2. 9 0
      tests/unit/src/unit/issues/Issue5289.hx

+ 22 - 17
src/generators/genphp7.ml

@@ -911,20 +911,18 @@ class class_wrapper (cls) =
 				match cls.cl_init with
 				match cls.cl_init with
 					| Some _ -> true
 					| Some _ -> true
 					| None ->
 					| None ->
-						let needs = ref false in
-						PMap.iter
-							(fun _ field ->
+						List.exists
+							(fun field ->
 								(* Skip `inline var` fields *)
 								(* Skip `inline var` fields *)
-								if not (is_inline_var field) then begin
-									if not !needs then needs := is_var_with_nonconstant_expr field;
-									(* Check static vars with non-constant expressions *)
-									if not !needs then needs := is_var_with_nonconstant_expr field;
-									(* Check static dynamic functions *)
-									if not !needs then needs := is_dynamic_method field
-								end
+								not (is_inline_var field)
+								&& match field.cf_kind, field.cf_expr with
+									| Var _, Some { eexpr = TConst (TInt value) } -> value = Int32.min_int
+									| Var _, Some { eexpr = TConst _ } -> false
+									| Var _, Some _ -> true
+									| Method MethDynamic, _ -> true
+									| _ -> false
 							)
 							)
-							cls.cl_statics;
-						!needs
+							cls.cl_ordered_statics
 		(**
 		(**
 			Returns expression of a user-defined static __init__ method
 			Returns expression of a user-defined static __init__ method
 			@see http://old.haxe.org/doc/advanced/magic#initialization-magic
 			@see http://old.haxe.org/doc/advanced/magic#initialization-magic
@@ -3527,13 +3525,13 @@ class class_builder ctx (cls:tclass) =
 				);
 				);
 				writer#write ";\n"
 				writer#write ";\n"
 			in
 			in
-			PMap.iter
-				(fun _ field ->
+			List.iter
+				(fun field ->
 					match field.cf_kind with
 					match field.cf_kind with
 						| Method MethDynamic -> write_dynamic_method_initialization field
 						| Method MethDynamic -> write_dynamic_method_initialization field
 						| _ -> ()
 						| _ -> ()
 				)
 				)
-				cls.cl_statics;
+				cls.cl_ordered_statics;
 			(* `static var` initialization *)
 			(* `static var` initialization *)
 			let write_var_initialization field =
 			let write_var_initialization field =
 				let write_assign expr =
 				let write_assign expr =
@@ -3544,8 +3542,8 @@ class class_builder ctx (cls:tclass) =
 					Do not generate fields for RTTI meta, because this generator uses another way to store it.
 					Do not generate fields for RTTI meta, because this generator uses another way to store it.
 					Also skip initialization for `inline var` fields as those are generated as PHP class constants.
 					Also skip initialization for `inline var` fields as those are generated as PHP class constants.
 				*)
 				*)
-				let is_auto_meta_var = field.cf_name = "__meta__" && (has_rtti_meta ctx.pgc_common wrapper#get_module_type) in
-				if (is_var_with_nonconstant_expr field) && (not is_auto_meta_var) && (not (is_inline_var field)) then begin
+				let is_auto_meta_var() = field.cf_name = "__meta__" && (has_rtti_meta ctx.pgc_common wrapper#get_module_type) in
+				if (is_var_with_nonconstant_expr field) && (not (is_auto_meta_var())) && (not (is_inline_var field)) then begin
 					(match field.cf_expr with
 					(match field.cf_expr with
 						| None -> ()
 						| None -> ()
 						(* There can be not-inlined blocks when compiling with `-debug` *)
 						(* There can be not-inlined blocks when compiling with `-debug` *)
@@ -3565,6 +3563,11 @@ class class_builder ctx (cls:tclass) =
 					);
 					);
 					writer#write ";\n"
 					writer#write ";\n"
 				end
 				end
+				else match field.cf_expr with
+					| Some ({ eexpr = TConst (TInt value) } as expr) when value = Int32.min_int ->
+						write_assign expr;
+						writer#write ";\n"
+					| _ -> ()
 			in
 			in
 			List.iter write_var_initialization cls.cl_ordered_statics
 			List.iter write_var_initialization cls.cl_ordered_statics
 		(**
 		(**
@@ -3594,6 +3597,8 @@ class class_builder ctx (cls:tclass) =
 			writer#write (visibility ^ " $" ^ (field_name field));
 			writer#write (visibility ^ " $" ^ (field_name field));
 			match field.cf_expr with
 			match field.cf_expr with
 				| None -> writer#write ";\n"
 				| None -> writer#write ";\n"
+				| Some { eexpr = TConst (TInt value) } when value = Int32.min_int ->
+					writer#write (" = " ^ (Int32.to_string value) ^ ";\n")
 				| Some expr ->
 				| Some expr ->
 					match expr.eexpr with
 					match expr.eexpr with
 						| TConst _ ->
 						| TConst _ ->

+ 9 - 0
tests/unit/src/unit/issues/Issue5289.hx

@@ -0,0 +1,9 @@
+package unit.issues;
+
+class Issue5289 extends Test {
+	static var minInt32:Int = -2147483648;
+
+	function test() {
+		eq(-2147483648, minInt32);
+	}
+}