Explorar o código

Disallow duplicate argument name (#11978)

* Disallow duplicate argument name

* [tests] add test for 11417

* Move check, let externs do their thing

* Update test
Rudy Ges hai 7 meses
pai
achega
51917bc869

+ 5 - 1
src/typing/functionArguments.ml

@@ -100,7 +100,11 @@ object(self)
 					loop ((v,None) :: acc) false syntax typed
 				| ((_,pn),opt,m,_,_) :: syntax,(name,eo,t) :: typed ->
 					delay ctx.g PTypeField (fun() -> self#check_rest (typed = []) eo opt t pn);
-					if not is_extern then Naming.check_local_variable_name ctx.com name TVOArgument pn;
+					if not is_extern then begin
+						Naming.check_local_variable_name ctx.com name TVOArgument pn;
+						if name <> "_" && List.exists (fun (v,_) -> v.v_name = name) acc then
+							raise_typing_error ("Duplicate argument name \"" ^ name ^ "\"") pn;
+					end;
 					let eo = type_function_arg_value ctx t eo do_display in
 					let v = make_local name (VUser TVOArgument) t m pn in
 					if do_display && DisplayPosition.display_position#enclosed_in pn then

+ 2 - 0
tests/misc/projects/Issue11417/Main.hx

@@ -0,0 +1,2 @@
+function foo(_:Int, _:Int) {} // OK
+function bar(i:Int, i:Int) {} // Not OK

+ 8 - 0
tests/misc/projects/Issue11417/Main1.hx

@@ -0,0 +1,8 @@
+function baz() {
+	var foo = function(i:Int, i:Int) {};
+	function bar(i:Int, i:Int) {}
+	function bar1(i:Int, j:Int, i:Int) {}
+	function bar2(i:Int, ?i:Int) {}
+	function bar3(i:Int, i = 42) {}
+	var lambda = (i, i) -> {};
+}

+ 1 - 0
tests/misc/projects/Issue11417/Main2.hx

@@ -0,0 +1 @@
+extern function foo(i:Int, i:Int):Void; // Duplicate arg name is fine for externs

+ 3 - 0
tests/misc/projects/Issue11417/compile-fail.hxml

@@ -0,0 +1,3 @@
+Main
+-D message.reporting=pretty
+-D message.no-color

+ 6 - 0
tests/misc/projects/Issue11417/compile-fail.hxml.stderr

@@ -0,0 +1,6 @@
+[ERROR] Main.hx:2: characters 21-22
+
+ 2 | function bar(i:Int, i:Int) {} // Not OK
+   |                     ^
+   | Duplicate argument name "i"
+

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

@@ -0,0 +1 @@
+Main2

+ 3 - 0
tests/misc/projects/Issue11417/compile1-fail.hxml

@@ -0,0 +1,3 @@
+Main1
+-D message.reporting=pretty
+-D message.no-color

+ 36 - 0
tests/misc/projects/Issue11417/compile1-fail.hxml.stderr

@@ -0,0 +1,36 @@
+[ERROR] Main1.hx:2: characters 28-29
+
+ 2 |  var foo = function(i:Int, i:Int) {};
+   |                            ^
+   | Duplicate argument name "i"
+
+[ERROR] Main1.hx:3: characters 22-23
+
+ 3 |  function bar(i:Int, i:Int) {}
+   |                      ^
+   | Duplicate argument name "i"
+
+[ERROR] Main1.hx:4: characters 30-31
+
+ 4 |  function bar1(i:Int, j:Int, i:Int) {}
+   |                              ^
+   | Duplicate argument name "i"
+
+[ERROR] Main1.hx:5: characters 24-25
+
+ 5 |  function bar2(i:Int, ?i:Int) {}
+   |                        ^
+   | Duplicate argument name "i"
+
+[ERROR] Main1.hx:6: characters 23-24
+
+ 6 |  function bar3(i:Int, i = 42) {}
+   |                       ^
+   | Duplicate argument name "i"
+
+[ERROR] Main1.hx:7: characters 19-20
+
+ 7 |  var lambda = (i, i) -> {};
+   |                   ^
+   | Duplicate argument name "i"
+