Browse Source

Fix dce full with array join (#6868)

* [dce] Keep toString method of T when Array<T>.join() is called

* [dce] Add a test case for Array<T>.join() keeping T.toString method
Alfredo Beaumont 7 years ago
parent
commit
2f9767d1ad

+ 6 - 0
src/optimization/dce.ml

@@ -503,6 +503,12 @@ and expr dce e =
 		check_feature dce ft;
 		expr dce e;
 
+	(* keep toString method of T when array<T>.join() is called *)
+	| TCall ({eexpr = TField(_, FInstance({cl_path = ([],"Array")}, pl, {cf_name="join"}))} as ef, args) ->
+		List.iter (fun e -> to_string dce e) pl;
+		expr dce ef;
+		List.iter (expr dce) args;
+
 	(* keep toString method when the class is argument to Std.string or haxe.Log.trace *)
 	| TCall ({eexpr = TField({eexpr = TTypeExpr (TClassDecl ({cl_path = (["haxe"],"Log")} as c))},FStatic (_,{cf_name="trace"}))} as ef, ((e2 :: el) as args))
 	| TCall ({eexpr = TField({eexpr = TTypeExpr (TClassDecl ({cl_path = ([],"Std")} as c))},FStatic (_,{cf_name="string"}))} as ef, ((e2 :: el) as args)) ->

+ 13 - 0
tests/misc/projects/Issue6841/Main.hx

@@ -0,0 +1,13 @@
+class A {
+	public function new() {}
+
+	function toString()
+		return 'a';
+}
+
+class Main {
+	static function main() {
+		var foos: Array<A> = [new A(), new A(), new A()];
+		Sys.print(foos.join(''));
+	}
+}

+ 19 - 0
tests/misc/projects/Issue6841/Test.hx

@@ -0,0 +1,19 @@
+class Test {
+	static function error(msg, code) {
+		Sys.stderr().writeString(msg);
+		Sys.exit(code);
+	}
+
+	static function main() {
+		var proc = new sys.io.Process("haxe", ["-dce", "full", "-x", "Main.hx"]);
+		var stderr = proc.stderr.readAll().toString();
+		var exit = proc.exitCode();
+		if (exit != 0) {
+			error(stderr, exit);
+		} else {
+			var stdout = proc.stdout.readAll().toString();
+			if (stdout.indexOf("aaa") == -1)
+				error("toString NOT kept\n", 1);
+		}
+	}
+}

+ 1 - 0
tests/misc/projects/Issue6841/compile.hxml

@@ -0,0 +1 @@
+--run Test