Skip to content

Commit

Permalink
Merge pull request #6965 from bjorng/bjorn/compiler/fix-lost-map-exce…
Browse files Browse the repository at this point in the history
…ption/GH-6960/OTP-18497

Fix lost exception from map update
  • Loading branch information
bjorng authored Mar 10, 2023
2 parents 2454b14 + a5cb4f6 commit 5063a62
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
28 changes: 13 additions & 15 deletions lib/compiler/src/sys_core_fold.erl
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ expr(#c_tuple{anno=Anno,es=Es0}=Tuple, Ctxt, Sub) ->
ann_c_tuple(Anno, Es)
end;
expr(#c_map{anno=Anno,arg=V0,es=Es0}=Map, Ctxt, Sub) ->
Es = pair_list(Es0, Ctxt, descend(Map, Sub)),
%% Warn for useless building, but always build the map
%% anyway to preserve a possible exception.
case Ctxt of
effect ->
warn_useless_building(Map, Sub),
make_effect_seq(Es, Sub);
value ->
V = expr(V0, Ctxt, Sub),
ann_c_map(Anno,V,Es)
end;
effect -> warn_useless_building(Map, Sub);
value -> ok
end,
Es = pair_list(Es0, descend(Map, Sub)),
V = expr(V0, value, Sub),
ann_c_map(Anno, V, Es);
expr(#c_binary{segments=Ss}=Bin0, Ctxt, Sub) ->
%% Warn for useless building, but always build the binary
%% anyway to preserve a possible exception.
Expand Down Expand Up @@ -490,14 +490,12 @@ ifes_list(_FVar, [], _Safe) ->
expr_list(Es, Ctxt, Sub) ->
[expr(E, Ctxt, Sub) || E <- Es].

pair_list(Es, Ctxt, Sub) ->
[pair(E, Ctxt, Sub) || E <- Es].
pair_list(Es, Sub) ->
[pair(E, Sub) || E <- Es].

pair(#c_map_pair{key=K,val=V}, effect, Sub) ->
make_effect_seq([K,V], Sub);
pair(#c_map_pair{key=K0,val=V0}=Pair, value=Ctxt, Sub) ->
K = expr(K0, Ctxt, Sub),
V = expr(V0, Ctxt, Sub),
pair(#c_map_pair{key=K0,val=V0}=Pair, Sub) ->
K = expr(K0, value, Sub),
V = expr(V0, value, Sub),
Pair#c_map_pair{key=K,val=V}.

bitstr_list(Es, Sub) ->
Expand Down
20 changes: 18 additions & 2 deletions lib/compiler/test/core_fold_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
no_no_file/1,configuration/1,supplies/1,
redundant_stack_frame/1,export_from_case/1,
empty_values/1,cover_letrec_effect/1,
receive_effect/1]).
receive_effect/1,map_effect/1]).

-export([foo/0,foo/1,foo/2,foo/3]).

Expand All @@ -50,7 +50,7 @@ groups() ->
no_no_file,configuration,supplies,
redundant_stack_frame,export_from_case,
empty_values,cover_letrec_effect,
receive_effect]}].
receive_effect,map_effect]}].


init_per_suite(Config) ->
Expand Down Expand Up @@ -700,4 +700,20 @@ receive_effect(_Config) ->
do_receive_effect() ->
{} = receive _ -> {} = {} end.

map_effect(_Config) ->
{'EXIT',{{badkey,key},_}} = catch map_effect_1(),

{'EXIT',{{badkey,key},_}} = catch map_effect_2(#{}),
{'EXIT',{{badmap,no_map},_}} = catch map_effect_2(no_map),

ok.

map_effect_1() ->
#{}#{key := value},
ok.

map_effect_2(Map) ->
Map#{key := value},
ok.

id(I) -> I.

0 comments on commit 5063a62

Please sign in to comment.