Skip to content

Commit

Permalink
Merge branch 'hasse/dialyzer/maps_remove/OTP-16055/ERL-1002'
Browse files Browse the repository at this point in the history
* hasse/dialyzer/maps_remove/OTP-16055/ERL-1002:
  dialyzer: Handle maps:remove/2 better
  • Loading branch information
uabboli committed Sep 30, 2019
2 parents ae08744 + 212a5ef commit a62aed8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/dialyzer/test/map_SUITE_data/results/maps_remove
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

maps_remove.erl:22: Function get/2 has no local return
maps_remove.erl:23: The call maps:get(K::'a',M::#{}) will never return since the success typing arguments are (any(),map())
maps_remove.erl:7: Function t1/0 has no local return
23 changes: 23 additions & 0 deletions lib/dialyzer/test/map_SUITE_data/src/maps_remove.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%% ERL-1002, maps:remove

-module(maps_remove).

-export([t1/0]).

t1() ->
A = new(),
B = put(a, 1, A),
C = remove(a, B),
get(a, C).

new() ->
maps:new().

put(K, V, M) ->
maps:put(K, V, M).

remove(K, M) ->
maps:remove(K, M).

get(K, M) ->
maps:get(K, M).
8 changes: 8 additions & 0 deletions lib/hipe/cerl/erl_bif_types.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
t_map_is_key/3,
t_map_entries/2,
t_map_put/3,
t_map_remove/3,
t_map_update/3,
t_map_pairwise_merge/4
]).
Expand Down Expand Up @@ -1700,6 +1701,11 @@ type(maps, put, 3, Xs, Opaques) ->
fun ([Key, Value, Map]) ->
t_map_put({Key, Value}, Map, Opaques)
end, Opaques);
type(maps, remove, 2, Xs, Opaques) ->
strict(maps, remove, 2, Xs,
fun ([Key, Map]) ->
t_map_remove(Key, Map, Opaques)
end, Opaques);
type(maps, size, 1, Xs, Opaques) ->
strict(maps, size, 1, Xs,
fun ([Map]) ->
Expand Down Expand Up @@ -2648,6 +2654,8 @@ arg_types(maps, merge, 2) ->
[t_map(), t_map()];
arg_types(maps, put, 3) ->
[t_any(), t_any(), t_map()];
arg_types(maps, remove, 2) ->
[t_any(), t_map()];
arg_types(maps, size, 1) ->
[t_map()];
arg_types(maps, update, 3) ->
Expand Down
22 changes: 22 additions & 0 deletions lib/hipe/cerl/erl_types.erl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
t_map_update/2, t_map_update/3,
t_map_pairwise_merge/4,
t_map_put/2, t_map_put/3,
t_map_remove/3,
t_matchstate/0,
t_matchstate/2,
t_matchstate_present/1,
Expand Down Expand Up @@ -1925,6 +1926,27 @@ map_put({Key, Value}, ?map(Pairs,DefK,DefV), Opaques) ->
end
end.

-spec t_map_remove(erl_type(), erl_type(), opaques()) -> erl_type().

t_map_remove(Key, Map, Opaques) ->
do_opaque(Map, Opaques, fun(UM) -> map_remove(Key, UM) end).

map_remove(_, ?none) -> ?none;
map_remove(_, ?unit) -> ?none;
map_remove(Key, Map) ->
%% ?map(lists:keydelete(Key, 1, Pairs), DefK, DefV).
case is_singleton_type(Key) of
false -> Map;
true ->
?map(Pairs,DefK,DefV) = Map,
case lists:keyfind(Key, 1, Pairs) of
false -> Map;
{Key, _, _} ->
Pairs1 = lists:keydelete(Key, 1, Pairs),
t_map(Pairs1, DefK, DefV)
end
end.

-spec t_map_update({erl_type(), erl_type()}, erl_type()) -> erl_type().

t_map_update(KV, Map) ->
Expand Down
3 changes: 2 additions & 1 deletion lib/stdlib/src/maps.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2013-2018. All Rights Reserved.
%% Copyright Ericsson AB 2013-2019. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -100,6 +100,7 @@ merge(_,_) -> erlang:nif_error(undef).
put(_,_,_) -> erlang:nif_error(undef).


%% Shadowed by erl_bif_types: maps:remove/2
-spec remove(Key,Map1) -> Map2 when
Key :: term(),
Map1 :: map(),
Expand Down

0 comments on commit a62aed8

Please sign in to comment.