@@ -87,34 +87,28 @@ defmodule Module.Types.Expr do
8787 def of_expr ( list , expected , expr , stack , context ) when is_list ( list ) do
8888 { prefix , suffix } = unpack_list ( list , [ ] )
8989
90- if stack . mode == :traversal do
91- { _ , context } = Enum . map_reduce ( prefix , context , & of_expr ( & 1 , term ( ) , expr , stack , & 2 ) )
92- { _ , context } = of_expr ( suffix , term ( ) , expr , stack , context )
93- { dynamic ( ) , context }
94- else
95- hd_type =
96- case list_hd ( expected ) do
97- { :ok , type } -> type
98- _ -> term ( )
99- end
90+ hd_type =
91+ case list_hd ( expected ) do
92+ { :ok , type } -> type
93+ _ -> term ( )
94+ end
10095
101- { prefix , context } = Enum . map_reduce ( prefix , context , & of_expr ( & 1 , hd_type , expr , stack , & 2 ) )
96+ { prefix , context } = Enum . map_reduce ( prefix , context , & of_expr ( & 1 , hd_type , expr , stack , & 2 ) )
10297
103- { suffix , context } =
104- if suffix == [ ] do
105- { empty_list ( ) , context }
106- else
107- tl_type =
108- case list_tl ( expected ) do
109- { :ok , type } -> type
110- :badnonemptylist -> term ( )
111- end
98+ { suffix , context } =
99+ if suffix == [ ] do
100+ { empty_list ( ) , context }
101+ else
102+ tl_type =
103+ case list_tl ( expected ) do
104+ { :ok , type } -> type
105+ :badnonemptylist -> term ( )
106+ end
112107
113- of_expr ( suffix , tl_type , expr , stack , context )
114- end
108+ of_expr ( suffix , tl_type , expr , stack , context )
109+ end
115110
116- { non_empty_list ( Enum . reduce ( prefix , & union / 2 ) , suffix ) , context }
117- end
111+ { non_empty_list ( Enum . reduce ( prefix , & union / 2 ) , suffix ) , context }
118112 end
119113
120114 # {left, right}
@@ -178,23 +172,17 @@ defmodule Module.Types.Expr do
178172 { { key_type , value_type } , context }
179173 end )
180174
181- expected =
182- if stack . mode == :traversal do
183- expected
184- else
185- # The only information we can attach to the expected types is that
186- # certain keys are expected.
187- expected_pairs =
188- Enum . flat_map ( pairs_types , fn { key_type , _value_type } ->
189- case atom_fetch ( key_type ) do
190- { :finite , [ key ] } -> [ { key , term ( ) } ]
191- _ -> [ ]
192- end
193- end )
194-
195- intersection ( expected , open_map ( expected_pairs ) )
196- end
175+ # The only information we can attach to the expected types is that
176+ # certain keys are expected.
177+ expected_pairs =
178+ Enum . flat_map ( pairs_types , fn { key_type , _value_type } ->
179+ case atom_fetch ( key_type ) do
180+ { :finite , [ key ] } -> [ { key , term ( ) } ]
181+ _ -> [ ]
182+ end
183+ end )
197184
185+ expected = intersection ( expected , open_map ( expected_pairs ) )
198186 { map_type , context } = of_expr ( map , expected , expr , stack , context )
199187
200188 try do
@@ -226,16 +214,12 @@ defmodule Module.Types.Expr do
226214 { map_type , context } = of_expr ( map , term ( ) , struct , stack , context )
227215
228216 context =
229- if stack . mode == :traversal do
217+ with { false , struct_key_type } <- map_fetch_key ( map_type , :__struct__ ) ,
218+ { :finite , [ ^ module ] } <- atom_fetch ( struct_key_type ) do
230219 context
231220 else
232- with { false , struct_key_type } <- map_fetch_key ( map_type , :__struct__ ) ,
233- { :finite , [ ^ module ] } <- atom_fetch ( struct_key_type ) do
234- context
235- else
236- _ ->
237- error ( __MODULE__ , { :badupdate , map_type , struct , context } , meta , stack , context )
238- end
221+ _ ->
222+ error ( __MODULE__ , { :badupdate , map_type , struct , context } , meta , stack , context )
239223 end
240224
241225 Enum . reduce ( pairs , { map_type , context } , fn { key , value } , { acc , context } ->
@@ -337,19 +321,14 @@ defmodule Module.Types.Expr do
337321 { patterns , _guards } = extract_head ( head )
338322 domain = Enum . map ( patterns , fn _ -> dynamic ( ) end )
339323
340- if stack . mode == :traversal do
341- { _acc , context } = of_clauses ( clauses , domain , @ pending , nil , :fn , stack , context , none ( ) )
342- { dynamic ( ) , context }
343- else
344- { acc , context } =
345- of_clauses_fun ( clauses , domain , @ pending , nil , :fn , stack , context , [ ] , fn
346- trees , body , context , acc ->
347- args = Pattern . of_domain ( trees , context )
348- add_inferred ( acc , args , body )
349- end )
350-
351- { fun_from_inferred_clauses ( acc ) , context }
352- end
324+ { acc , context } =
325+ of_clauses_fun ( clauses , domain , @ pending , nil , :fn , stack , context , [ ] , fn
326+ trees , body , context , acc ->
327+ args = Pattern . of_domain ( trees , context )
328+ add_inferred ( acc , args , body )
329+ end )
330+
331+ { fun_from_inferred_clauses ( acc ) , context }
353332 end
354333
355334 def of_expr ( { :try , _meta , [ [ do: body ] ++ blocks ] } , expected , expr , stack , original ) do
@@ -469,11 +448,7 @@ defmodule Module.Types.Expr do
469448 { args_types , context } =
470449 Enum . map_reduce ( args , context , & of_expr ( & 1 , @ pending , & 1 , stack , & 2 ) )
471450
472- if stack . mode == :traversal do
473- { dynamic ( ) , context }
474- else
475- Apply . fun_apply ( fun_type , args_types , call , stack , context )
476- end
451+ Apply . fun_apply ( fun_type , args_types , call , stack , context )
477452 end
478453
479454 def of_expr ( { { :. , _ , [ callee , key_or_fun ] } , meta , [ ] } = call , expected , expr , stack , context )
@@ -528,19 +503,13 @@ defmodule Module.Types.Expr do
528503 # var
529504 def of_expr ( var , expected , expr , stack , context ) when is_var ( var ) do
530505 case stack do
531- % { mode: :traversal } -> { dynamic ( ) , context }
532506 % { refine_vars: false } -> { Of . var ( var , context ) , context }
533507 % { } -> Of . refine_body_var ( var , expected , expr , stack , context )
534508 end
535509 end
536510
537511 ## Tuples
538512
539- defp of_tuple ( elems , _expected , expr , % { mode: :traversal } = stack , context ) do
540- { _types , context } = Enum . map_reduce ( elems , context , & of_expr ( & 1 , term ( ) , expr , stack , & 2 ) )
541- { dynamic ( ) , context }
542- end
543-
544513 defp of_tuple ( elems , expected , expr , stack , context ) do
545514 of_tuple ( elems , 0 , [ ] , expected , expr , stack , context )
546515 end
@@ -741,14 +710,8 @@ defmodule Module.Types.Expr do
741710 defp dynamic_unless_static ( { _ , _ } = output , % { mode: :static } ) , do: output
742711 defp dynamic_unless_static ( { type , context } , % { mode: _ } ) , do: { dynamic ( type ) , context }
743712
744- defp of_clauses ( clauses , domain , expected , expr , info , % { mode: mode } = stack , context , acc ) do
745- fun =
746- if mode == :traversal do
747- fn _ , _ , _ , _ -> dynamic ( ) end
748- else
749- fn _trees , result , _context , acc -> union ( result , acc ) end
750- end
751-
713+ defp of_clauses ( clauses , domain , expected , expr , info , stack , context , acc ) do
714+ fun = fn _trees , result , _context , acc -> union ( result , acc ) end
752715 of_clauses_fun ( clauses , domain , expected , expr , info , stack , context , acc , fun )
753716 end
754717
0 commit comments