@@ -325,7 +325,7 @@ defmodule Module.Types.Expr do
325325 else
326326 clauses
327327 end
328- |> of_redundant_clauses ( [ case_type ] , expected , info , stack , context , none ( ) )
328+ |> of_clauses ( [ case_type ] , expected , info , stack , context , none ( ) )
329329 |> dynamic_unless_static ( stack )
330330 end
331331
@@ -336,10 +336,9 @@ defmodule Module.Types.Expr do
336336 domain = Enum . map ( patterns , fn _ -> dynamic ( ) end )
337337
338338 { acc , context } =
339- of_clauses_fun ( clauses , domain , @ pending , nil , :fn , stack , context , [ ] , fn
340- trees , body , context , acc ->
341- args = Pattern . of_domain ( trees , stack , context )
342- add_inferred ( acc , args , body )
339+ of_clauses_fun ( clauses , domain , @ pending , :fn , stack , context , [ ] , fn
340+ args_types , body , acc ->
341+ add_inferred ( acc , args_types , body )
343342 end )
344343
345344 { fun_from_inferred_clauses ( acc ) , context }
@@ -353,7 +352,7 @@ defmodule Module.Types.Expr do
353352 if else_block do
354353 { type , context } = of_expr ( body , @ pending , body , stack , original )
355354 info = { :try_else , meta , body , type }
356- of_redundant_clauses ( else_block , [ type ] , expected , info , stack , context , none ( ) )
355+ of_clauses ( else_block , [ type ] , expected , info , stack , context , none ( ) )
357356 else
358357 of_expr ( body , expected , expr , stack , original )
359358 end
@@ -378,7 +377,7 @@ defmodule Module.Types.Expr do
378377
379378 { :catch , clauses } , { acc , context } ->
380379 args = [ @ try_catch , dynamic ( ) ]
381- of_redundant_clauses ( clauses , args , expected , :try_catch , stack , context , acc )
380+ of_clauses ( clauses , args , expected , :try_catch , stack , context , acc )
382381 end )
383382 |> dynamic_unless_static ( stack )
384383
@@ -399,7 +398,7 @@ defmodule Module.Types.Expr do
399398 acc_context
400399
401400 { :do , clauses } , { acc , context } ->
402- of_redundant_clauses ( clauses , [ dynamic ( ) ] , expected , :receive , stack , context , acc )
401+ of_clauses ( clauses , [ dynamic ( ) ] , expected , :receive , stack , context , acc )
403402
404403 { :after , [ { :-> , meta , [ [ timeout ] , body ] } ] = after_expr } , { acc , context } ->
405404 { timeout_type , context } = of_expr ( timeout , @ timeout_type , after_expr , stack , context )
@@ -427,7 +426,7 @@ defmodule Module.Types.Expr do
427426 # TODO: We need to type check against dynamic() instead of using reduce_type
428427 # because this is recursive. We need to infer the block type first.
429428 args = [ dynamic ( ) ]
430- of_redundant_clauses ( block , args , expected , :for_reduce , stack , context , reduce_type )
429+ of_clauses ( block , args , expected , :for_reduce , stack , context , reduce_type )
431430 else
432431 # TODO: Use the collectable protocol for the output
433432 into = Keyword . get ( opts , :into , [ ] )
@@ -683,7 +682,7 @@ defmodule Module.Types.Expr do
683682
684683 defp with_option ( { :else , clauses } , stack , context , _original ) do
685684 { _ , context } =
686- of_clauses ( clauses , [ dynamic ( ) ] , @ pending , nil , :with_else , stack , context , none ( ) )
685+ of_clauses ( clauses , [ dynamic ( ) ] , @ pending , :with_else , stack , context , none ( ) )
687686
688687 context
689688 end
@@ -717,29 +716,12 @@ defmodule Module.Types.Expr do
717716 defp dynamic_unless_static ( { _ , _ } = output , % { mode: :static } ) , do: output
718717 defp dynamic_unless_static ( { type , context } , % { mode: _ } ) , do: { dynamic ( type ) , context }
719718
720- defp of_clauses ( clauses , domain , expected , expr , info , stack , context , acc ) do
721- fun = fn _trees , result , _context , acc -> union ( result , acc ) end
722- of_clauses_fun ( clauses , domain , expected , expr , info , stack , context , acc , fun )
719+ defp of_clauses ( clauses , domain , expected , clause_info , stack , context , acc ) do
720+ fun = fn _args_types , result , acc -> union ( result , acc ) end
721+ of_clauses_fun ( clauses , domain , expected , clause_info , stack , context , acc , fun )
723722 end
724723
725- defp of_clauses_fun ( clauses , domain , expected , expr , info , stack , original , acc , fun ) do
726- % { failed: failed? } = original
727-
728- Enum . reduce ( clauses , { acc , original } , fn { :-> , meta , [ head , body ] } , { acc , context } ->
729- { failed? , context } = reset_failed ( context , failed? )
730- { patterns , guards } = extract_head ( head )
731-
732- { trees , _precise? , context } =
733- Pattern . of_head ( patterns , guards , domain , info , meta , stack , context )
734-
735- { result , context } = of_expr ( body , expected , expr || body , stack , context )
736-
737- { fun . ( trees , result , context , acc ) ,
738- context |> set_failed ( failed? ) |> Of . reset_vars ( original ) }
739- end )
740- end
741-
742- defp of_redundant_clauses ( clauses , domain , expected , clause_info , stack , original , acc ) do
724+ defp of_clauses_fun ( clauses , domain , expected , clause_info , stack , original , acc , fun ) do
743725 % { failed: failed? } = original
744726
745727 { result , _previous , context } =
@@ -766,24 +748,24 @@ defmodule Module.Types.Expr do
766748 { trees , precise? , context }
767749 end
768750
751+ args_types =
752+ Enum . map ( trees , fn { tree , _ , _ } ->
753+ Pattern . of_pattern_tree ( tree , stack , context )
754+ end )
755+
769756 { previous , context } =
770757 if context . failed do
771758 { previous , context }
772759 else
773- clause_type =
774- Enum . map ( trees , fn { tree , _ , _ } ->
775- tree
776- |> Pattern . of_pattern_tree ( stack , context )
777- |> upper_bound ( )
778- end )
760+ upper_types = Enum . map ( args_types , & upper_bound / 1 )
779761
780762 cond do
781763 stack . mode != :infer and previous != [ ] and
782- Pattern . args_subtype? ( clause_type , previous ) ->
764+ Pattern . args_subtype? ( upper_types , previous ) ->
783765 { previous , Pattern . badpattern_warn ( clause , info , stack , context ) }
784766
785767 precise? ->
786- { [ clause_type | previous ] , context }
768+ { [ upper_types | previous ] , context }
787769
788770 true ->
789771 { previous , context }
@@ -792,7 +774,7 @@ defmodule Module.Types.Expr do
792774
793775 { result , context } = of_expr ( body , expected , body , stack , context )
794776
795- { union ( result , acc ) , previous ,
777+ { fun . ( args_types , result , acc ) , previous ,
796778 context |> set_failed ( failed? ) |> Of . reset_vars ( original ) }
797779 end )
798780
0 commit comments