From d6073d78109f026ef96b29af4ce748242df2389d Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Wed, 11 Mar 2015 14:03:58 +0100 Subject: Fix compilation of match specs with maps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous compilation was just plain wrong with push/pop mismatches. Reported-by: Björn-Egil Dahlberg --- erts/emulator/beam/erl_db_util.c | 63 ++++++++++++++++++++------------- erts/emulator/test/match_spec_SUITE.erl | 3 ++ 2 files changed, 42 insertions(+), 24 deletions(-) (limited to 'erts/emulator') diff --git a/erts/emulator/beam/erl_db_util.c b/erts/emulator/beam/erl_db_util.c index 7eb80e3bb1..1d986f2447 100644 --- a/erts/emulator/beam/erl_db_util.c +++ b/erts/emulator/beam/erl_db_util.c @@ -214,8 +214,8 @@ typedef enum { matchPushT, matchPushL, matchPushM, - matchPushK, matchPop, + matchSwap, matchBind, matchCmp, matchEqBin, @@ -225,6 +225,7 @@ typedef enum { matchEq, matchList, matchMap, + matchKey, matchSkip, matchPushC, matchConsA, /* Car is below Cdr */ @@ -1399,22 +1400,26 @@ restart: } goto error; } - DMC_PUSH(text, matchPushK); - ++(context.stack_used); + DMC_PUSH(text, matchKey); DMC_PUSH(text, dmc_private_copy(&context, key)); - } - if (context.stack_used > context.stack_need) { - context.stack_need = context.stack_used; - } - for (i = num_iters; i--; ) { - Eterm value = map_get_values(map_val(t))[i]; - DMC_PUSH(text, matchPop); - --(context.stack_used); - res = dmc_one_term(&context, &heap, &stack, &text, - value); - ASSERT(res != retFail); - if (res == retRestart) { - goto restart; + { + int old_stack = ++(context.stack_used); + Eterm value = map_get_values(map_val(t))[i]; + res = dmc_one_term(&context, &heap, &stack, &text, + value); + ASSERT(res != retFail); + if (res == retRestart) { + goto restart; + } + if (old_stack != context.stack_used) { + ASSERT(old_stack + 1 == context.stack_used); + DMC_PUSH(text, matchSwap); + } + if (context.stack_used > context.stack_need) { + context.stack_need = context.stack_used; + } + DMC_PUSH(text, matchPop); + --(context.stack_used); } } break; @@ -1960,17 +1965,23 @@ restart: } *sp++ = map_val_rel(*ep++, base); break; - case matchPushK: + case matchKey: t = (Eterm) *pc++; tp = erts_maps_get_rel(t, make_map_rel(ep, base), base); if (!tp) { FAIL(); } - *sp++ = tp; + *sp++ = ep; + ep = tp; break; case matchPop: ep = *(--sp); break; + case matchSwap: + tp = sp[-1]; + sp[-1] = sp[-2]; + sp[-2] = tp; + break; case matchBind: n = *pc++; variables[n].term = *ep++; @@ -5302,6 +5313,12 @@ void db_match_dis(Binary *bp) ++t; erts_printf("Map\t%beu\n", n); break; + case matchKey: + ++t; + p = (Eterm) *t; + ++t; + erts_printf("Key\t%p (%T)\n", t, p); + break; case matchPushT: ++t; n = *t; @@ -5318,16 +5335,14 @@ void db_match_dis(Binary *bp) ++t; erts_printf("PushM\t%beu\n", n); break; - case matchPushK: - ++t; - p = (Eterm) *t; - ++t; - erts_printf("PushK\t%p (%T)\n", t, p); - break; case matchPop: ++t; erts_printf("Pop\n"); break; + case matchSwap: + ++t; + erts_printf("Swap\n"); + break; case matchBind: ++t; n = *t; diff --git a/erts/emulator/test/match_spec_SUITE.erl b/erts/emulator/test/match_spec_SUITE.erl index fc4a5028e1..02ef4f8e3f 100644 --- a/erts/emulator/test/match_spec_SUITE.erl +++ b/erts/emulator/test/match_spec_SUITE.erl @@ -924,6 +924,9 @@ maps(Config) when is_list(Config) -> table), {ok,#{foo := 3},[],[]} = erlang:match_spec_test({}, [{{},[],[#{foo => {'+',1,2}}]}], table), + {ok,"camembert",[],[]} = + erlang:match_spec_test(#{b => "camembert",c => "cabécou"}, + [{#{b => '$1',c => "cabécou"},[],['$1']}], table), ok. empty_list(Config) when is_list(Config) -> -- cgit v1.2.3 From 36576d7de20e4e8cb7b3943cfe1b7e272b5e3971 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Wed, 11 Mar 2015 14:21:37 +0100 Subject: Properly collect variables in match specs with maps --- erts/emulator/beam/erl_db_util.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'erts/emulator') diff --git a/erts/emulator/beam/erl_db_util.c b/erts/emulator/beam/erl_db_util.c index 1d986f2447..748be93fe3 100644 --- a/erts/emulator/beam/erl_db_util.c +++ b/erts/emulator/beam/erl_db_util.c @@ -3286,7 +3286,14 @@ int db_has_variable(Eterm node) { while(arity--) { ESTACK_PUSH(s,*(++tuple)); } - } + } else if (is_map(node)) { + Eterm *values = map_get_values(map_val(node)); + int size = map_get_size(map_val(node)); + ESTACK_PUSH(s, ((map_t *) map_val(node))->keys); + while (size--) { + ESTACK_PUSH(s, *(values++)); + } + } break; case TAG_PRIMARY_IMMED1: if (node == am_Underscore || db_is_variable(node) >= 0) { -- cgit v1.2.3 From 12408cf3657a88540d7afde88e68639c72212299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn-Egil=20Dahlberg?= Date: Thu, 12 Mar 2015 09:55:04 +0100 Subject: erts: Strengthen maps match spec compilation tests --- erts/emulator/test/match_spec_SUITE.erl | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'erts/emulator') diff --git a/erts/emulator/test/match_spec_SUITE.erl b/erts/emulator/test/match_spec_SUITE.erl index 02ef4f8e3f..b231c2bbd9 100644 --- a/erts/emulator/test/match_spec_SUITE.erl +++ b/erts/emulator/test/match_spec_SUITE.erl @@ -927,6 +927,11 @@ maps(Config) when is_list(Config) -> {ok,"camembert",[],[]} = erlang:match_spec_test(#{b => "camembert",c => "cabécou"}, [{#{b => '$1',c => "cabécou"},[],['$1']}], table), + + {ok,#{a :="camembert",b := "hi"},[],[]} = + erlang:match_spec_test(#{<<"b">> =>"camembert","c"=>"cabécou", "wat"=>"hi", b=><<"other">>}, + [{#{<<"b">> => '$1',"wat" => '$2'},[],[#{a=>'$1',b=>'$2'}]}], + table), ok. empty_list(Config) when is_list(Config) -> -- cgit v1.2.3