Bläddra i källkod

[python] check var names when transforming instead of printing (closes #5425)

Dan Korostelev 9 år sedan
förälder
incheckning
da6cf0e9a8
2 ändrade filer med 28 tillägg och 17 borttagningar
  1. 18 17
      src/generators/genpy.ml
  2. 10 0
      tests/unit/src/unit/issues/Issue5425.hx

+ 18 - 17
src/generators/genpy.ml

@@ -273,18 +273,21 @@ module Transformer = struct
 
 
 	let rec transform_function tf ae is_value =
 	let rec transform_function tf ae is_value =
 		let p = tf.tf_expr.epos in
 		let p = tf.tf_expr.epos in
-		let assigns = List.fold_left (fun acc (v,value) -> match value with
-			| None | Some TNull ->
-				acc
-			| Some ct ->
-				let a_local = mk (TLocal v) v.v_type p in
-				let a_null = mk (TConst TNull) v.v_type p in
-				let a_cmp = mk (TBinop(OpEq,a_local,a_null)) !t_bool p in
-				let a_value = mk (TConst(ct)) v.v_type p in
-				let a_assign = mk (TBinop(OpAssign,a_local,a_value)) v.v_type p in
-				let a_if = mk (TIf(a_cmp,a_assign,None)) !t_void p in
-				a_if :: acc
-		) [] tf.tf_args in
+		let assigns = List.fold_left (fun acc (v,value) ->
+			KeywordHandler.check_var_declaration v;
+			match value with
+				| None | Some TNull ->
+					acc
+				| Some ct ->
+					let a_local = mk (TLocal v) v.v_type p in
+					let a_null = mk (TConst TNull) v.v_type p in
+					let a_cmp = mk (TBinop(OpEq,a_local,a_null)) !t_bool p in
+					let a_value = mk (TConst(ct)) v.v_type p in
+					let a_assign = mk (TBinop(OpAssign,a_local,a_value)) v.v_type p in
+					let a_if = mk (TIf(a_cmp,a_assign,None)) !t_void p in
+					a_if :: acc
+			) [] tf.tf_args
+		in
 		let body = match assigns with
 		let body = match assigns with
 			| [] ->
 			| [] ->
 				tf.tf_expr
 				tf.tf_expr
@@ -309,6 +312,7 @@ module Transformer = struct
 			lift_expr fn
 			lift_expr fn
 
 
 	and transform_var_expr ae eo v =
 	and transform_var_expr ae eo v =
+		KeywordHandler.check_var_declaration v;
 		let b,new_expr = match eo with
 		let b,new_expr = match eo with
 			| None ->
 			| None ->
 				[],None
 				[],None
@@ -889,7 +893,7 @@ module Transformer = struct
 			lift_expr ~blocks:blocks r
 			lift_expr ~blocks:blocks r
 		| (false, TTry(etry, catches)) ->
 		| (false, TTry(etry, catches)) ->
 			let etry = trans false [] etry in
 			let etry = trans false [] etry in
-			let catches = List.map (fun(v,e) -> v, trans false [] e) catches in
+			let catches = List.map (fun(v,e) -> KeywordHandler.check_var_declaration v; v, trans false [] e) catches in
 			let blocks = List.flatten (List.map (fun (_,e) -> e.a_blocks) catches) in
 			let blocks = List.flatten (List.map (fun (_,e) -> e.a_blocks) catches) in
 			let catches = List.map (fun(v,e) -> v, e.a_expr) catches in
 			let catches = List.map (fun(v,e) -> v, e.a_expr) catches in
 			let r = { a_expr with eexpr = TTry(etry.a_expr, catches)} in
 			let r = { a_expr with eexpr = TTry(etry.a_expr, catches)} in
@@ -903,7 +907,7 @@ module Transformer = struct
 			let temp_local = { a_expr with eexpr = TLocal(temp_var)} in
 			let temp_local = { a_expr with eexpr = TLocal(temp_var)} in
 			let mk_temp_assign right = { a_expr with eexpr = TBinop(OpAssign, temp_local, right)} in
 			let mk_temp_assign right = { a_expr with eexpr = TBinop(OpAssign, temp_local, right)} in
 			let etry = mk_temp_assign etry in
 			let etry = mk_temp_assign etry in
-			let catches = List.map (fun (v,e)-> v, mk_temp_assign e) catches in
+			let catches = List.map (fun (v,e)-> KeywordHandler.check_var_declaration v; v, mk_temp_assign e) catches in
 			let new_try = { a_expr with eexpr = TTry(etry, catches)} in
 			let new_try = { a_expr with eexpr = TTry(etry, catches)} in
 			let block = [temp_var_def; new_try; temp_local] in
 			let block = [temp_var_def; new_try; temp_local] in
 			let new_block = { a_expr with eexpr = TBlock(block)} in
 			let new_block = { a_expr with eexpr = TBlock(block)} in
@@ -1089,7 +1093,6 @@ module Printer = struct
 		let had_kw_args = ref false in
 		let had_kw_args = ref false in
 		let sl = List.map (fun (v,cto) ->
 		let sl = List.map (fun (v,cto) ->
 			let check_err () = if !had_var_args || !had_kw_args then error "Arguments after KwArgs/VarArgs are not allowed" p in
 			let check_err () = if !had_var_args || !had_kw_args then error "Arguments after KwArgs/VarArgs are not allowed" p in
-			KeywordHandler.check_var_declaration v;
 			let name = handle_keywords v.v_name in
 			let name = handle_keywords v.v_name in
 			match follow v.v_type with
 			match follow v.v_type with
 				| TAbstract({a_path = ["python"],"KwArgs"},_) ->
 				| TAbstract({a_path = ["python"],"KwArgs"},_) ->
@@ -1325,7 +1328,6 @@ module Printer = struct
 			| TFunction tf ->
 			| TFunction tf ->
 				print_function pctx tf None e.epos
 				print_function pctx tf None e.epos
 			| TVar (v,eo) ->
 			| TVar (v,eo) ->
-				KeywordHandler.check_var_declaration v;
 				print_var pctx v eo
 				print_var pctx v eo
 			| TBlock [] ->
 			| TBlock [] ->
 				Printf.sprintf "pass"
 				Printf.sprintf "pass"
@@ -1442,7 +1444,6 @@ module Printer = struct
 			| _ -> false
 			| _ -> false
 		end in
 		end in
 		let print_catch pctx i (v,e) =
 		let print_catch pctx i (v,e) =
-			KeywordHandler.check_var_declaration v;
 			let is_empty_expr = begin match e.eexpr with
 			let is_empty_expr = begin match e.eexpr with
 				| TBlock [] -> true
 				| TBlock [] -> true
 				| _ -> false
 				| _ -> false

+ 10 - 0
tests/unit/src/unit/issues/Issue5425.hx

@@ -0,0 +1,10 @@
+package unit.issues;
+
+class Issue5425 extends unit.Test {
+	function test() {
+        var self = 1;
+        function f() self = 2;
+        f();
+        eq(2, self);
+	}
+}