Sfoglia il codice sorgente

[typer] flatten abstract froms for top down inference on functions

closes #10980
Simon Krajewski 2 anni fa
parent
commit
0935beff78

+ 15 - 1
src/typing/typer.ml

@@ -1357,7 +1357,21 @@ and type_local_function ctx kind f with_type p =
 					| [t2] ->
 						if not (List.exists (shallow_eq t) stack) then loop (t :: stack) t2
 					| l ->
-						handle_abstract_matrix l
+						(* For cases like nested EitherType, we want a flat list of all possible candidates.
+						   This might be controversial because those could be considered transitive casts,
+						   but it's unclear if that's a bad thing for this kind of inference (issue #10982). *)
+						let rec loop acc l = match l with
+							| t :: l ->
+								begin match follow t with
+								| TAbstract(a,tl) ->
+									loop acc (l @ get_abstract_froms ctx a tl)
+								| _ ->
+									loop (t :: acc) l
+								end
+							| [] ->
+								List.rev acc
+						in
+						handle_abstract_matrix (loop [] l)
 				end
 			| _ -> ())
 		in

+ 18 - 0
tests/misc/projects/Issue10980/Main.hx

@@ -0,0 +1,18 @@
+import haxe.extern.EitherType as Or;
+
+typedef Callback = Or<
+		Or<
+			() -> Void,
+			(a:Int) -> Void
+		>,
+		(a:Int, b:Int) -> Void
+	>;
+
+class Main {
+	static function main() {
+		final foo:Callback = a -> {
+			$type(a); // Unknown<0> instead of Int
+			return;
+		}
+	}
+}

+ 3 - 0
tests/misc/projects/Issue10980/compile.hxml

@@ -0,0 +1,3 @@
+-cp src
+-main Main
+--interp

+ 1 - 0
tests/misc/projects/Issue10980/compile.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:14: characters 10-11 : Warning : Int