ソースを参照

[python] fix enum ctors with the same arg names as enum itself (fixes #8118)

Aleksandr Kuzmenko 6 年 前
コミット
a2692907ea

+ 8 - 4
src/generators/genpy.ml

@@ -2251,10 +2251,14 @@ module Generator = struct
 		List.iter (fun ef ->
 			match follow ef.ef_type with
 			| TFun(args, _) ->
+				let arg_name hx_name =
+					let name = handle_keywords hx_name in
+					if name = p_name then p_name ^ "_" ^ name
+					else name
+				in
 				let print_args args =
 					let had_optional = ref false in
 					let sl = List.map (fun (n,o,_) ->
-						let name = handle_keywords n in
 						let arg_value = if !had_optional then
 							"= None"
 						else if o then begin
@@ -2263,7 +2267,7 @@ module Generator = struct
 						end else
 							""
 						in
-						Printf.sprintf "%s%s" name arg_value
+						Printf.sprintf "%s%s" (arg_name n) arg_value
 					) args in
 					String.concat "," sl
 				in
@@ -2271,8 +2275,8 @@ module Generator = struct
 				let param_str = print_args args in
 				let args_str =
 					match args with
-					| [(n,_,_)] -> (handle_keywords n) ^ ","
-					| args -> String.concat "," (List.map (fun (n,_,_) -> handle_keywords n) args)
+					| [(n,_,_)] -> (arg_name n) ^ ","
+					| args -> String.concat "," (List.map (fun (n,_,_) -> arg_name n) args)
 				in
 				newline ctx;
 				newline ctx;

+ 3 - 0
tests/unit/src/RootEnum.hx

@@ -0,0 +1,3 @@
+enum RootEnum {
+	A(RootEnum:Int);
+}

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

@@ -0,0 +1,9 @@
+package unit.issues;
+
+class Issue8118 extends unit.Test {
+	static var v:RootEnum;
+	function test() {
+		v = A(10);
+		eq(10, switch v { case A(i): i; });
+	}
+}