@@ -874,11 +874,13 @@ defmodule Module.Types.Pattern do
874874 end
875875
876876 defp of_remote ( fun , _meta , [ left , right ] , call , expected , stack , context )
877- when fun in [ :andalso , :orelse ] do
878- { both_domain , abort_domain } =
877+ when fun in [ :and , :or , : andalso, :orelse ] do
878+ { both_domain , abort_domain , always_rhs? } =
879879 case fun do
880- :andalso -> { @ atom_true , @ atom_false }
881- :orelse -> { @ atom_false , @ atom_true }
880+ :andalso -> { @ atom_true , @ atom_false , false }
881+ :orelse -> { @ atom_false , @ atom_true , false }
882+ :and -> { @ atom_true , @ atom_false , true }
883+ :or -> { @ atom_false , @ atom_true , true }
882884 end
883885
884886 # For example, if the expected type is true for andalso, then it can
@@ -887,7 +889,23 @@ defmodule Module.Types.Pattern do
887889 if subtype? ( expected , both_domain ) do
888890 of_logical_both ( left , both_domain , right , expected , abort_domain , call , stack , context )
889891 else
890- of_logical_cond ( left , right , expected , abort_domain , call , stack , context )
892+ cond_context = % { context | conditional_vars: % { } }
893+
894+ # Compute the sure types, which are stored directly in the context
895+ { _type , context } = of_guard ( left , boolean ( ) , call , stack , context )
896+
897+ # andalso/orelse may not execute the rhs, so we cannot get sure types from it
898+ context =
899+ case always_rhs? do
900+ true ->
901+ { _ , context } = of_guard ( right , boolean ( ) , call , stack , context )
902+ context
903+
904+ false ->
905+ context
906+ end
907+
908+ of_logical_cond ( left , right , expected , abort_domain , call , stack , context , cond_context )
891909 end
892910 end
893911
@@ -912,13 +930,7 @@ defmodule Module.Types.Pattern do
912930 end
913931 end
914932
915- defp of_logical_cond ( left , right , expected , to_abort , call , stack , context ) do
916- cond_context = % { context | conditional_vars: % { } }
917-
918- # First we do pass to find the surely types, which are stored directly in the context
919- { _left_type , context } = of_guard ( left , boolean ( ) , call , stack , context )
920-
921- # Now we find the conditional ones
933+ defp of_logical_cond ( left , right , expected , to_abort , call , stack , context , cond_context ) do
922934 { left_type , left_context } = of_guard ( left , expected , call , stack , cond_context )
923935 { right_type , right_context } = of_guard ( right , expected , call , stack , cond_context )
924936
0 commit comments