Browse Source

don't rewrite `+ 1` into `++` unops for string
(fixes #8513)

Aleksandr Kuzmenko 6 years ago
parent
commit
700605cfda

+ 7 - 2
src/optimization/analyzerTexprTransformer.ml

@@ -737,8 +737,13 @@ and func ctx i =
 			begin match e1.eexpr,e2.eexpr with
 				| TLocal v1,TLocal v2 when v1 == v2 && not v1.v_capture && is_valid_assign_op op ->
 					begin match op,e3.eexpr with
-						| OpAdd,TConst (TInt i32) when Int32.to_int i32 = 1 -> {e with eexpr = TUnop(Increment,Prefix,e1)}
-						| OpSub,TConst (TInt i32) when Int32.to_int i32 = 1 -> {e with eexpr = TUnop(Decrement,Prefix,e1)}
+						| (OpAdd|OpSub) as op,TConst (TInt i32) when Int32.to_int i32 = 1 && ExtType.is_numeric (follow v1.v_type) ->
+							let op = match op with
+								| OpAdd -> Increment
+								| OpSub -> Decrement
+								| _ -> assert false
+							in
+							{e with eexpr = TUnop(op,Prefix,e1)}
 						| _ -> {e with eexpr = TBinop(OpAssignOp op,e1,e3)}
 					end
 				| _ ->

+ 12 - 0
tests/unit/src/unit/issues/Issue8513.hx

@@ -0,0 +1,12 @@
+package unit.issues;
+
+import utest.Assert;
+
+class Issue8513 extends unit.Test {
+	function test() {
+		var s = '';
+		s = s + 1;
+		s += 1;
+		eq('11', s);
+	}
+}