Browse Source

handle negation of Float/Int in `make_unop` (closes #5303)

Otherwise we miss the recursive cases.
Simon Krajewski 9 years ago
parent
commit
485c55c9ea
2 changed files with 15 additions and 7 deletions
  1. 6 7
      src/syntax/parser.ml
  2. 9 0
      tests/unit/src/unit/issues/Issue5303.hx

+ 6 - 7
src/syntax/parser.ml

@@ -137,9 +137,14 @@ let rec make_binop op e ((v,p2) as e2) =
 		EBinop (op,e,e2) , punion (pos e) (pos e2)
 
 let rec make_unop op ((v,p2) as e) p1 =
+	let neg s =
+		if s.[0] = '-' then String.sub s 1 (String.length s - 1) else "-" ^ s
+	in
 	match v with
 	| EBinop (bop,e,e2) -> EBinop (bop, make_unop op e p1 , e2) , (punion p1 p2)
 	| ETernary (e1,e2,e3) -> ETernary (make_unop op e1 p1 , e2, e3), punion p1 p2
+	| EConst (Int i) when op = Neg -> EConst (Int (neg i)),punion p1 p2
+	| EConst (Float j) when op = Neg -> EConst (Float (neg j)),punion p1 p2
 	| _ -> EUnop (op,Prefix,e), punion p1 p2
 
 let rec make_meta name params ((v,p2) as e) p1 =
@@ -1306,13 +1311,7 @@ and expr = parser
 	| [< '(Kwd Function,p1); e = parse_function p1 false; >] -> e
 	| [< '(Unop op,p1) when is_prefix op; e = expr >] -> make_unop op e p1
 	| [< '(Binop OpSub,p1); e = expr >] ->
-		let neg s =
-			if s.[0] = '-' then String.sub s 1 (String.length s - 1) else "-" ^ s
-		in
-		(match make_unop Neg e p1 with
-		| EUnop (Neg,Prefix,(EConst (Int i),pc)),p -> EConst (Int (neg i)),p
-		| EUnop (Neg,Prefix,(EConst (Float j),pc)),p -> EConst (Float (neg j)),p
-		| e -> e)
+		make_unop Neg e p1
 	(*/* removed unary + : this cause too much syntax errors go unnoticed, such as "a + + 1" (missing 'b')
 						without adding anything to the language
 	| [< '(Binop OpAdd,p1); s >] ->

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

@@ -0,0 +1,9 @@
+package unit.issues;
+
+class Issue5303 extends unit.Test {
+	function test() {
+		var a:Int = 0;
+		a = -2147483648 - a;
+		// we only care if this type-checks
+	}
+}