Repository navigation
Conversation
286a0d5 to
124d88b
Compare
petermm
left a comment
There was a problem hiding this comment.
PR Review: 286a0d56 — avoid exceptions in maps:find/2 and maps:get/3
Verdict
Approve with one non-blocking test suggestion.
I found no correctness, compatibility, or maintainability defects in the production change. The new implementations preserve the OTP behavior for hits, misses, and non-map inputs while avoiding the exception path on missing keys. Oracle independently reviewed the commit and reached the same conclusion.
Findings
[Suggestion] Add direct coverage for every rewritten maps:get/3 branch
Location: tests/libs/estdlib/test_maps.erl:82-90
The existing tests exercise maps:get/3 only for a missing key. The hit path and the new explicit non-map clause are not directly tested: the assertions currently at lines 88-89 call maps:get/2, not maps:get/3. maps:find/2 covers all three branches, but its generic bad-map helper does not verify that the invalid argument is retained in the exception reason.
This is not evidence of a production bug—the implementation matches OTP and the suite passes—but focused assertions would protect the exact behavior this commit changes.
diff --git a/tests/libs/estdlib/test_maps.erl b/tests/libs/estdlib/test_maps.erl
--- a/tests/libs/estdlib/test_maps.erl
+++ b/tests/libs/estdlib/test_maps.erl
@@ -84,9 +84,11 @@ test_get() ->
ok = check_bad_map(fun() -> maps:get(bar, id(not_a_map)) end),
ok = check_bad_key(fun() -> maps:get(bar, id(#{foo => bar})) end, bar),
?ASSERT_MATCH(maps:get(gnu, id(#{foo => bar}), gnat), gnat),
+ ?ASSERT_MATCH(maps:get(foo, id(#{foo => bar}), gnat), bar),
+ ?ASSERT_ERROR(maps:get(gnu, id({hello}), gnat), {badmap, {hello}}),
?ASSERT_ERROR(maps:get({hello}, id(#{foo => bar})), {badkey, {hello}}),
?ASSERT_ERROR(maps:get(gnu, id({hello})), {badmap, {hello}}),
ok.
@@ -223,6 +225,7 @@ test_find() ->
?ASSERT_MATCH(maps:find(b, #{a => 1, b => 2, c => 3}), {ok, 2}),
?ASSERT_MATCH(maps:find(c, #{a => 1, b => 2, c => 3}), {ok, 3}),
?ASSERT_MATCH(maps:find(foo, #{a => 1, b => 2, c => 3}), error),
ok = check_bad_map(fun() -> maps:find(foo, id(not_a_map)) end),
+ ?ASSERT_ERROR(maps:find(foo, id({hello})), {badmap, {hello}}),
ok.Review notes
erlang:is_map_key/2anderlang:map_get/2use the same exact-key lookup semantics in AtomVM.- Maps are immutable, so the membership check followed by retrieval cannot race with a mutation.
- The successful path now performs two lookups, but that is the intended tradeoff for eliminating exception construction on misses; it is not a correctness issue.
- OTP 29 confirms both functions raise
error:{badmap, Map}for non-map arguments.
Verification
git diff HEAD^ HEAD --check— passed.cmake --build build --target test_estdlib -j2— passed.build/src/AtomVM build/tests/libs/estdlib/test_estdlib.avm— passed;test_mapsreturnedokand the suite returnedok.
124d88b to
e66b254
Compare
bettio
left a comment
There was a problem hiding this comment.
Sorry for reporting one last issue: would you mind doing: git commit --amend --signoff so it passes DCO check?
By the way, there is a minor non-blocking change that can be done to CHANGELOG.md
| calling it with a port now raises `badarg` (previous versions accepted any id-carrying | ||
| term, so it could be used to read port information; there is no `erlang:port_info/2` | ||
| in AtomVM yet to migrate such code to) | ||
| - `maps:find/2` and `maps:get/3` no longer raise and catch an exception on a missing key |
There was a problem hiding this comment.
Let's reframe this as an optimization. Also let's append this at the end of the ### Changed 0.7.0-beta.0 section. We keep them in some sort of chronological order.
| Default | ||
| end. | ||
| get(Key, Map, Default) when is_map(Map) -> | ||
| case erlang:is_map_key(Key, Map) of |
There was a problem hiding this comment.
We're doing two lookups here.
OTP pattern only does one:
get(Key, Map, Default) when is_map(Map) ->
case Map of
#{Key := Value} -> Value;
#{} -> Default
end;
|
Also AtomVM properly raises badmap before this change. |
This is one third of #2388
Today, maps:find/2 and maps:get/3 caught
catch error:{badkey, _}and so every time you looked up a missing key, an exception was raised which generated stacktraces.Instead we can use erlang:is_map_key/2 and erlang:map_get/2, keeping the
{badmap, Map}error for non-map arguments (as OTP does).SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later