File 0327-Fix-miscompilation-of-guard-with-or.patch of Package erlang

From 1cb06b4ee9988ee8f8f9b20de793f5ce120a6008 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Thu, 1 Jun 2023 10:41:49 +0200
Subject: [PATCH] Fix miscompilation of guard with `or`

Closes #7339
---
 lib/compiler/src/beam_ssa_bool.erl | 24 ++++++++++++++++--------
 lib/compiler/test/guard_SUITE.erl  | 12 ++++++++++++
 2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/lib/compiler/src/beam_ssa_bool.erl b/lib/compiler/src/beam_ssa_bool.erl
index 7633f8c9a1..b0d4ab04ff 100644
--- a/lib/compiler/src/beam_ssa_bool.erl
+++ b/lib/compiler/src/beam_ssa_bool.erl
@@ -117,7 +117,7 @@
 -module(beam_ssa_bool).
 -export([module/2]).
 
--import(lists, [all/2,foldl/3,keyfind/3,last/1,partition/2,
+-import(lists, [all/2,any/2,foldl/3,keyfind/3,last/1,partition/2,
                 reverse/1,reverse/2,sort/1]).
 
 -include("beam_ssa.hrl").
@@ -1007,15 +1007,23 @@ convert_to_br_node(I, Target, G0, St) ->
 %%    (element(10, T) =:= y)
 
 ensure_no_failing_instructions(First, Second, G, St) ->
-    Vs0 = covered(get_vertex(First, St), get_vertex(Second, St), G),
-    Vs = [{V,beam_digraph:vertex(G, V)} || V <- Vs0],
-    Failing = [P || {V,#b_set{op={succeeded,_}}}=P <- Vs,
-                    not eaten_by_phi(V, G)],
-    case Failing of
-        [] -> ok;
-        [_|_] -> not_possible()
+    Vs = covered(get_vertex(First, St), get_vertex(Second, St), G),
+    case any(fun(V) ->
+                     case beam_digraph:vertex(G, V) of
+                         #b_set{op=Op} ->
+                             can_fail(Op, V, G);
+                         _ ->
+                             false
+                     end
+             end, Vs) of
+        true -> not_possible();
+        false -> ok
     end.
 
+can_fail({succeeded,_}, V, G) -> not eaten_by_phi(V, G);
+can_fail(put_map, _, _) -> true;
+can_fail(_, _, _) -> false.
+
 eaten_by_phi(V, G) ->
     {br,_,Fail} = get_targets(V, G),
     case beam_digraph:vertex(G, Fail) of
diff --git a/lib/compiler/test/guard_SUITE.erl b/lib/compiler/test/guard_SUITE.erl
index f03dcf6225..047bebe978 100644
--- a/lib/compiler/test/guard_SUITE.erl
+++ b/lib/compiler/test/guard_SUITE.erl
@@ -2619,6 +2619,7 @@ beam_bool_SUITE(_Config) ->
     gh_6164(),
     gh_6184(),
     gh_7252(),
+    gh_7339(),
     ok.
 
 before_and_inside_if() ->
@@ -3193,6 +3194,17 @@ gh_7252_c(A) when ((ok > A) and ((bnot ok) =:= ok)) or (not (ok > A)) ->
 gh_7252_c(_) ->
     bar.
 
+gh_7339() ->
+    b = do_gh_7339(id(42)),
+    b = do_gh_7339(id(42.0)),
+    b = do_gh_7339(id(#{})),
+    ok.
+
+do_gh_7339(M) when is_number(M) or (not is_map(M#{a => b})) ->
+  a;
+do_gh_7339(_) ->
+  b.
+
 %%%
 %%% End of beam_bool_SUITE tests.
 %%%
-- 
2.35.3

openSUSE Build Service is sponsored by