Skip to content

Commit f27df20

Browse files
author
José Valim
committed
Fix warnings on OTP 20
1 parent f252c9e commit f27df20

File tree

4 files changed

+21
-27
lines changed

4 files changed

+21
-27
lines changed

lib/elixir/src/elixir_errors.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ handle_file_warning(_, _File, {_Line, v3_kernel, bad_call}) -> ok;
140140
%% We handle unused local warnings ourselves
141141
handle_file_warning(_, _File, {_Line, erl_lint, {unused_function, _}}) -> ok;
142142

143+
%% Properly format keys using inspect.
144+
handle_file_warning(_, File, {Line, v3_core, {map_key_repeated, Key}}) ->
145+
Message = io_lib:format("key ~ts will be overridden in map", ['Elixir.Kernel':inspect(Key)]),
146+
warn(Line, File, Message);
147+
143148
%% Make no_effect clauses pretty
144149
handle_file_warning(_, File, {Line, sys_core_fold, {no_effect, {erlang, F, A}}}) ->
145150
{Fmt, Args} = case erl_internal:comp_op(F, A) of

lib/elixir/src/elixir_map.erl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ translate_map(Meta, Args, S) ->
6666
translate_map(Meta, Assocs, TUpdate, US).
6767

6868
translate_struct(_Meta, Name, {'%{}', MapMeta, Assocs}, S) when is_tuple(Name) ->
69-
translate_map(MapMeta, Assocs ++ [{'__struct__', Name}], nil, S);
69+
translate_map(MapMeta, [{'__struct__', Name} | delete_struct_key(Assocs)], nil, S);
7070

7171
translate_struct(Meta, Name, {'%{}', MapMeta, Args}, S) ->
72-
{Assocs, TUpdate, US} = extract_assoc_update(Args, S),
72+
{TAssocs, TUpdate, US} = extract_assoc_update(Args, S),
73+
Assocs = delete_struct_key(TAssocs),
7374
Operation = operation(TUpdate, S),
7475

7576
Struct = case Operation of
@@ -98,11 +99,11 @@ translate_struct(Meta, Name, {'%{}', MapMeta, Args}, S) ->
9899
{clause, Generated, [Var], [], [elixir_utils:erl_call(Ann, erlang, error, [Error])]}
99100
]}, TS};
100101
match ->
101-
translate_map(MapMeta, Assocs ++ [{'__struct__', Name}], nil, US);
102+
translate_map(MapMeta, [{'__struct__', Name} | Assocs], nil, US);
102103
expand ->
103-
Keys = [K || {K, _} <- Assocs],
104+
Keys = ['__struct__'] ++ [K || {K, _} <- Assocs],
104105
{StructAssocs, _} = elixir_quote:escape(maps:to_list(maps:without(Keys, Struct)), false),
105-
translate_map(MapMeta, StructAssocs ++ Assocs ++ [{'__struct__', Name}], nil, US)
106+
translate_map(MapMeta, [{'__struct__', Name}] ++ StructAssocs ++ Assocs, nil, US)
106107
end.
107108

108109
%% Helpers
@@ -190,6 +191,9 @@ extract_assoc_update([{'|', _Meta, [Update, Args]}], S) ->
190191
{Args, TArg, SA};
191192
extract_assoc_update(Args, SA) -> {Args, nil, SA}.
192193

194+
delete_struct_key(Assocs) ->
195+
lists:keydelete('__struct__', 1, Assocs).
196+
193197
extract_key_val_op(_TUpdate, #elixir_scope{context=match}) ->
194198
{map_field_exact,
195199
fun(X, Acc) -> elixir_translator:translate(X, Acc#elixir_scope{extra=map_key}) end,

lib/elixir/test/elixir/exception_test.exs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,6 @@ defmodule ExceptionTest do
263263
assert Exception.format_exit(reason) |> String.starts_with?(expected_to_start_with)
264264
end
265265

266-
test "format_exit with call with exception" do
267-
Process.flag(:trap_exit, true)
268-
# Fake reason to prevent error_logger printing to stdout
269-
exit_reason = {%ArgumentError{}, [{:not_a_real_module, :function, 0, []}]}
270-
exit_fun = fn() -> receive do: (_ -> exit(exit_reason)) end
271-
reason =
272-
try do
273-
:gen_fsm.sync_send_event(spawn_link(exit_fun), :hello)
274-
catch
275-
:exit, reason -> reason
276-
end
277-
278-
formatted = Exception.format_exit(reason)
279-
assert formatted =~ ~r"exited in: :gen_fsm\.sync_send_event\(#PID<\d+\.\d+\.\d+>, :hello\)"
280-
assert formatted =~ ~r"\s{4}\*\* \(EXIT\) an exception was raised:\n"
281-
assert formatted =~ ~r"\s{8}\*\* \(ArgumentError\) argument error\n"
282-
assert formatted =~ ~r"\s{12}:not_a_real_module\.function/0"
283-
end
284-
285266
test "format_exit with nested calls" do
286267
Process.flag(:trap_exit, true)
287268
# Fake reason to prevent error_logger printing to stdout

lib/elixir/test/elixir/map_test.exs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,13 @@ defmodule MapTest do
8787
end
8888

8989
test "maps with duplicate keys" do
90-
assert %{a: :b, a: :c} == %{a: :c}
91-
assert %{1 => 2, 1 => 3} == %{1 => 3}
92-
assert %{:a => :b, a: :c} == %{a: :c}
90+
ExUnit.CaptureIO.capture_io :stderr, fn ->
91+
defmodule DuplicateKeys do
92+
assert %{a: :b, a: :c} == %{a: :c}
93+
assert %{1 => 2, 1 => 3} == %{1 => 3}
94+
assert %{:a => :b, a: :c} == %{a: :c}
95+
end
96+
end
9397
end
9498

9599
test "update maps" do

0 commit comments

Comments
 (0)