Ver Fonte

[php7] wrap empty array declaration in parentheses if used directly with array access (fixes #6090)

Alexander Kuzmenko há 8 anos atrás
pai
commit
d5ecfb1421
2 ficheiros alterados com 18 adições e 1 exclusões
  1. 11 1
      src/generators/genphp7.ml
  2. 7 0
      tests/unit/src/unit/issues/Issue6090.hx

+ 11 - 1
src/generators/genphp7.ml

@@ -1651,7 +1651,17 @@ class code_writer (ctx:Common.context) hx_type_path php_name =
 		*)
 		method write_expr_array_decl exprs =
 			match exprs with
-				| [] -> self#write ("new " ^ (self#use array_type_path) ^ "()")
+				| [] ->
+					let decl () = self#write ("new " ^ (self#use array_type_path) ^ "()") in
+					(* Wrap into parentheses if trying to access items of empty array declaration *)
+					(match self#parent_expr with
+						| Some { eexpr = TArray _ } ->
+							self#write "(";
+							decl();
+							self#write ")"
+						| _ ->
+							decl()
+					)
 				| [expr] ->
 					self#write ((self#use array_type_path) ^ "::wrap([");
 					self#write_expr expr;

+ 7 - 0
tests/unit/src/unit/issues/Issue6090.hx

@@ -0,0 +1,7 @@
+package unit.issues;
+
+class Issue6090 extends Test {
+	public function test() {
+		eq(null, [][0]);
+	}
+}