File 5605-erl_pp-Handle-map-comprehensions.patch of Package erlang

From 9a20ba5ecdea21b1a00a5b8b6ca3bec49721885d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Thu, 19 Jan 2023 12:31:37 +0100
Subject: [PATCH 05/12] erl_pp: Handle map comprehensions

---
 lib/stdlib/src/erl_pp.erl        | 13 +++++++++----
 lib/stdlib/test/erl_pp_SUITE.erl | 25 +++++++++++++++++++++++--
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/lib/stdlib/src/erl_pp.erl b/lib/stdlib/src/erl_pp.erl
index 191aa75698..d7e80f71bb 100644
--- a/lib/stdlib/src/erl_pp.erl
+++ b/lib/stdlib/src/erl_pp.erl
@@ -580,12 +580,13 @@ lexpr({cons,_,H,T}, _, Opts) ->
 lexpr({lc,_,E,Qs}, _Prec, Opts) ->
     Lcl = {list,[{step,[lexpr(E, Opts),leaf(" ||")],lc_quals(Qs, Opts)}]},
     {list,[{seq,$[,[],[[]],[{force_nl,leaf(" "),[Lcl]}]},$]]};
-    %% {list,[{step,$[,Lcl},$]]};
 lexpr({bc,_,E,Qs}, _Prec, Opts) ->
     P = max_prec(),
     Lcl = {list,[{step,[lexpr(E, P, Opts),leaf(" ||")],lc_quals(Qs, Opts)}]},
     {list,[{seq,'<<',[],[[]],[{force_nl,leaf(" "),[Lcl]}]},'>>']};
-    %% {list,[{step,'<<',Lcl},'>>']};
+lexpr({mc,_,E,Qs}, _Prec, Opts) ->
+    Lcl = {list,[{step,[map_field(E, Opts),leaf(" ||")],lc_quals(Qs, Opts)}]},
+    {list,[{seq,'#{',[],[[]],[{force_nl,leaf(" "),[Lcl]}]},$}]};
 lexpr({tuple,_,Elts}, _, Opts) ->
     tuple(Elts, Opts);
 lexpr({record_index, _, Name, F}, Prec, Opts) ->
@@ -956,6 +957,9 @@ clauses(Type, Opts, Cs) ->
 lc_quals(Qs, Opts) ->
     {prefer_nl,[$,],lexprs(Qs, fun lc_qual/2, Opts)}.
 
+lc_qual({m_generate,_,Pat,E}, Opts) ->
+    Pl = map_field(Pat, Opts),
+    {list,[{step,[Pl,leaf(" <-")],lexpr(E, 0, Opts)}]};
 lc_qual({b_generate,_,Pat,E}, Opts) ->
     Pl = lexpr(Pat, 0, Opts),
     {list,[{step,[Pl,leaf(" <=")],lexpr(E, 0, Opts)}]};
@@ -1367,7 +1371,7 @@ wordtable() ->
     L = [begin {leaf,Sz,S} = leaf(W), {S,Sz} end ||
             W <- [" ->"," =","<<",">>","[]","after","begin","case","catch",
                   "end","fun","if","of","receive","try","when"," ::","..",
-                  " |","maybe","else"]],
+                  " |","maybe","else","#{"]],
     list_to_tuple(L).
 
 word(' ->', WT) -> element(1, WT);
@@ -1390,7 +1394,8 @@ word(' ::', WT) -> element(17, WT);
 word('..', WT) -> element(18, WT);
 word(' |', WT) -> element(19, WT);
 word('maybe', WT) -> element(20, WT);
-word('else', WT) -> element(21, WT).
+word('else', WT) -> element(21, WT);
+word('#{', WT) -> element(22, WT).
 
 %% Make up an unique variable name for Name that won't clash with any
 %% name in Used. We first try by converting the name to uppercase and
diff --git a/lib/stdlib/test/erl_pp_SUITE.erl b/lib/stdlib/test/erl_pp_SUITE.erl
index c8c1a206ca..fa12f07fd2 100644
--- a/lib/stdlib/test/erl_pp_SUITE.erl
+++ b/lib/stdlib/test/erl_pp_SUITE.erl
@@ -56,7 +56,7 @@
           otp_10302/1, otp_10820/1, otp_11100/1, otp_11861/1, pr_1014/1,
           otp_13662/1, otp_14285/1, otp_15592/1, otp_15751/1, otp_15755/1,
           otp_16435/1, gh_5093/1,
-          eep49/1]).
+          eep49/1, eep58/1]).
 
 %% Internal export.
 -export([ehook/6]).
@@ -88,7 +88,7 @@ groups() ->
        otp_8473, otp_8522, otp_8567, otp_8664, otp_9147,
        otp_10302, otp_10820, otp_11100, otp_11861, pr_1014, otp_13662,
        otp_14285, otp_15592, otp_15751, otp_15755, otp_16435,
-       gh_5093, eep49]}].
+       gh_5093, eep49, eep58]}].
 
 init_per_suite(Config) ->
     Config.
@@ -1393,6 +1393,27 @@ eep49(_Config) ->
                 "    end.\n"),
     ok.
 
+eep58(_Config) ->
+    assert_same("lc_map(Map) ->\n"
+                "    [ \n"
+                "     {K, V} ||\n"
+                "         K := V <- Map\n"
+                "    ].\n"),
+
+    assert_same("bc_map(Map) ->\n"
+                "    << \n"
+                "      <<K:32,V:32>> ||\n"
+                "          K := V <- Map\n"
+                "    >>.\n"),
+
+    assert_same("mc(Map) ->\n"
+                "    #{ \n"
+                "      K => V + 1 ||\n"
+                "          K := V <- Map\n"
+                "    }.\n"),
+
+    ok.
+
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 compile(Config, Tests) ->
-- 
2.35.3

openSUSE Build Service is sponsored by