@@ -90,7 +90,7 @@ defmodule Module.Types.Infer do
9090 if subtype? ( source , target , context ) do
9191 { :ok , source , context }
9292 else
93- error ( { :unable_unify , source , target } , stack , context )
93+ error ( :unable_unify , { source , target , stack } , context )
9494 end
9595 end
9696
@@ -106,9 +106,9 @@ defmodule Module.Types.Infer do
106106
107107 if recursive_type? ( type , [ ] , context ) do
108108 if var_source? do
109- error ( { :unable_unify , { :var , var } , type } , stack , context )
109+ error ( :unable_unify , { { :var , var } , type , stack } , context )
110110 else
111- error ( { :unable_unify , type , { :var , var } } , stack , context )
111+ error ( :unable_unify , { type , { :var , var } , stack } , context )
112112 end
113113 else
114114 { :ok , { :var , var } , context }
@@ -151,7 +151,7 @@ defmodule Module.Types.Infer do
151151 else
152152 left = { :map , [ { :required , { :atom , :__struct__ } , left_module } ] }
153153 right = { :map , [ { :required , { :atom , :__struct__ } , right_module } ] }
154- error ( { :unable_unify , left , right } , stack , context )
154+ error ( :unable_unify , { left , right , stack } , context )
155155 end
156156 else
157157 _ -> :ok
@@ -190,7 +190,7 @@ defmodule Module.Types.Infer do
190190 { :ok , { :map , pairs } , context }
191191 else
192192 { :error , :unify } ->
193- error ( { :unable_unify , { :map , source_pairs } , { :map , target_pairs } } , stack , context )
193+ error ( :unable_unify , { { :map , source_pairs } , { :map , target_pairs } , stack } , context )
194194
195195 { :error , context } ->
196196 { :error , context }
@@ -208,7 +208,7 @@ defmodule Module.Types.Infer do
208208 { :error , _reason } ->
209209 source_map = { :map , [ { :required , source_key , source_value } ] }
210210 target_map = { :map , [ { target_kind , target_key , target_value } ] }
211- error ( { :unable_unify , source_map , target_map } , stack , context )
211+ error ( :unable_unify , { source_map , target_map , stack } , context )
212212 end
213213 else
214214 { :error , _reason } -> nil
@@ -228,7 +228,7 @@ defmodule Module.Types.Infer do
228228 { :error , _reason } ->
229229 source_map = { :map , [ { source_kind , source_key , source_value } ] }
230230 target_map = { :map , [ { :required , target_key , target_value } ] }
231- error ( { :unable_unify , source_map , target_map } , stack , context )
231+ error ( :unable_unify , { source_map , target_map , stack } , context )
232232 end
233233 else
234234 { :error , _reason } -> nil
@@ -248,7 +248,7 @@ defmodule Module.Types.Infer do
248248 { :error , _reason } ->
249249 source_map = { :map , [ { :optional , source_key , source_value } ] }
250250 target_map = { :map , [ { :optional , target_key , target_value } ] }
251- error ( { :unable_unify , source_map , target_map } , stack , context )
251+ error ( :unable_unify , { source_map , target_map , stack } , context )
252252 end
253253 else
254254 _ -> nil
@@ -268,7 +268,7 @@ defmodule Module.Types.Infer do
268268 { :error , _reason } ->
269269 source_map = { :map , [ { :optional , source_key , source_value } ] }
270270 target_map = { :map , [ { :optional , target_key , target_value } ] }
271- error ( { :unable_unify , source_map , target_map } , stack , context )
271+ error ( :unable_unify , { source_map , target_map , stack } , context )
272272 end
273273 else
274274 _ -> nil
@@ -499,63 +499,7 @@ defmodule Module.Types.Infer do
499499 def unify_kinds ( _ , :required ) , do: :required
500500 def unify_kinds ( :optional , :optional ) , do: :optional
501501
502- # Collect relevant information from context and traces to report error
503- # TODO: We should do this lazily since in some cases unification will error
504- # but we continue attempting unifying other types
505- defp error ( { :unable_unify , left , right } , stack , context ) do
506- { fun , arity } = context . function
507- line = get_meta ( stack . last_expr ) [ :line ]
508- location = { context . file , line , { context . module , fun , arity } }
509-
510- traces = type_traces ( stack , context )
511- traces = tag_traces ( traces , context )
512-
513- error = { :unable_unify , left , right , { location , stack . last_expr , traces } }
514- context = update_in ( context . warnings , & [ { Module.Types , error , location } | & 1 ] )
515- { :error , context }
516- end
517-
518- # Collect relevant traces from context.traces using stack.unify_stack
519- defp type_traces ( stack , context ) do
520- # TODO: Do we need the unify_stack or is enough to only get the last variable
521- # in the stack since we get related variables anyway?
522- stack =
523- stack . unify_stack
524- |> Enum . uniq ( )
525- |> Enum . flat_map ( & [ & 1 | related_variables ( & 1 , context . types ) ] )
526- |> Enum . uniq ( )
527-
528- Enum . flat_map ( stack , fn var_index ->
529- with % { ^ var_index => traces } <- context . traces ,
530- % { ^ var_index => expr_var } <- context . types_to_vars do
531- Enum . map ( traces , & { expr_var , & 1 } )
532- else
533- _other -> [ ]
534- end
535- end )
536- end
537-
538- defp related_variables ( var , types ) do
539- Enum . flat_map ( types , fn
540- { related_var , { :var , ^ var } } ->
541- [ related_var | related_variables ( related_var , types ) ]
542-
543- _ ->
544- [ ]
545- end )
546- end
547-
548- # Tag if trace is for a concrete type or type variable
549- defp tag_traces ( traces , context ) do
550- Enum . flat_map ( traces , fn { var , { type , expr , location } } ->
551- with { :var , var_index } <- type ,
552- % { ^ var_index => expr_var } <- context . types_to_vars do
553- [ { var , { :var , expr_var , expr , location } } ]
554- else
555- _ -> [ { var , { :type , type , expr , location } } ]
556- end
557- end )
558- end
502+ defp error ( type , reason , context ) , do: { :error , { type , reason , context } }
559503
560504 # TODO: We should check if structs have keys that do not belong to them.
561505 # This might not be the best place to do it since it will only be
0 commit comments