Browse Source

[overloads] fail properly on ambiguous rest overloads

closes #10205
Simon Krajewski 4 years ago
parent
commit
5d3ccee5c1

+ 15 - 8
src/codegen/overloads.ml

@@ -249,19 +249,26 @@ struct
 		| [] -> []
 		| [v] -> [v]
 		| compatible ->
+			let rate_arg t e = match e.eexpr with
+				(* if the argument is an implicit cast, we need to start with a penalty *)
+				(* The penalty should be higher than any other implicit cast - other than Dynamic *)
+				(* since Dynamic has a penalty of max_int, we'll impose max_int - 1 to it *)
+				| TMeta( (Meta.ImplicitCast,_,_), _) -> (max_int - 1, 0)
+				| _ -> rate_conv 0 t e.etype
+			in
 			(* convert compatible into ( rate * compatible_type ) list *)
 			let rec mk_rate acc elist args = match elist, args with
 				| [], [] -> acc
 				| _ :: elist, (_,true,_) :: args -> mk_rate acc elist args
+				| elist, [n,o,t] when ExtType.is_rest (follow t) ->
+					let t = match follow t with
+						| TAbstract({a_path=["haxe"],"Rest"},[t]) -> t
+						| _ -> die "" __LOC__
+					in
+					let rates = List.map (rate_arg t) elist in
+					acc @ rates
 				| e :: elist, (n,o,t) :: args ->
-					(* if the argument is an implicit cast, we need to start with a penalty *)
-					(* The penalty should be higher than any other implicit cast - other than Dynamic *)
-					(* since Dynamic has a penalty of max_int, we'll impose max_int - 1 to it *)
-					(match e.eexpr with
-						| TMeta( (Meta.ImplicitCast,_,_), _) ->
-							mk_rate ((max_int - 1, 0) :: acc) elist args
-						| _ ->
-							mk_rate (rate_conv 0 t e.etype :: acc) elist args)
+					mk_rate ((rate_arg t e) :: acc) elist args
 				| _ -> die "" __LOC__
 			in
 

+ 8 - 0
tests/misc/projects/Issue10205/Main.hx

@@ -0,0 +1,8 @@
+class Main {
+	static function main() {
+		test('hello');
+	}
+
+	overload extern static inline function test(s:String) {}
+	overload extern static inline function test(s:String, ...r:String) {}
+}

+ 1 - 0
tests/misc/projects/Issue10205/compile-fail.hxml

@@ -0,0 +1 @@
+--main Main

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

@@ -0,0 +1,3 @@
+Main.hx:3: characters 3-16 : Ambiguous overload, candidates follow
+Main.hx:6: characters 41-45 : ... (s : String) -> Void
+Main.hx:7: characters 41-45 : ... (s : String, r : haxe.Rest<String>) -> Void