Skip to content

Commit b52946c

Browse files
committed
Merge pull request #3723 from lexmag/dialyzer-trick
Make it clearer that unimplemented callbacks always stop
2 parents a2bcbf8 + 55c002e commit b52946c

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

lib/elixir/lib/gen_event.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,10 @@ defmodule GenEvent do
341341
@doc false
342342
def handle_call(msg, state) do
343343
# We do this to trick dialyzer to not complain about non-local returns.
344-
case :random.uniform(1) do
345-
1 -> exit({:bad_call, msg})
346-
2 -> {:remove_handler, :ok}
344+
reason = {:bad_call, msg}
345+
case :erlang.phash2(1, 1) do
346+
0 -> exit(reason)
347+
1 -> {:remove_handler, reason}
347348
end
348349
end
349350

@@ -376,7 +377,7 @@ defmodule GenEvent do
376377
section in the `GenServer` module docs.
377378
378379
If the event manager is successfully created and initialized, the function
379-
returns `{:ok, pid}`, where pid is the pid of the server. If a process with
380+
returns `{:ok, pid}`, where pid is the pid of the server. If a process with
380381
the specified server name already exists, the function returns
381382
`{:error, {:already_started, pid}}` with the pid of that process.
382383

lib/elixir/lib/gen_event/stream.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@ defmodule GenEvent.Stream do
2222

2323
@doc false
2424
def handle_event(event, _state) do
25-
exit({:bad_event, event})
25+
# We do this to trick dialyzer to not complain about non-local returns.
26+
case :erlang.phash2(1, 1) do
27+
0 -> exit({:bad_event, event})
28+
1 -> :remove_handler
29+
end
2630
end
2731

2832
@doc false
2933
def handle_call(msg, _state) do
3034
# We do this to trick dialyzer to not complain about non-local returns.
31-
case :random.uniform(1) do
32-
1 -> exit({:bad_call, msg})
33-
2 -> {:remove_handler, :ok}
35+
reason = {:bad_call, msg}
36+
case :erlang.phash2(1, 1) do
37+
0 -> exit(reason)
38+
1 -> {:remove_handler, reason}
3439
end
3540
end
3641

lib/elixir/lib/gen_server.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,10 @@ defmodule GenServer do
409409
@doc false
410410
def handle_call(msg, _from, state) do
411411
# We do this to trick Dialyzer to not complain about non-local returns.
412-
case :random.uniform(1) do
413-
1 -> exit({:bad_call, msg})
414-
2 -> {:noreply, state}
412+
reason = {:bad_call, msg}
413+
case :erlang.phash2(1, 1) do
414+
0 -> exit(reason)
415+
1 -> {:stop, reason, state}
415416
end
416417
end
417418

@@ -423,9 +424,10 @@ defmodule GenServer do
423424
@doc false
424425
def handle_cast(msg, state) do
425426
# We do this to trick Dialyzer to not complain about non-local returns.
426-
case :random.uniform(1) do
427-
1 -> exit({:bad_cast, msg})
428-
2 -> {:noreply, state}
427+
reason = {:bad_cast, msg}
428+
case :erlang.phash2(1, 1) do
429+
0 -> exit(reason)
430+
1 -> {:stop, reason, state}
429431
end
430432
end
431433

@@ -474,7 +476,7 @@ defmodule GenServer do
474476
## Return values
475477
476478
If the server is successfully created and initialized, the function returns
477-
`{:ok, pid}`, where pid is the pid of the server. If a process with the
479+
`{:ok, pid}`, where pid is the pid of the server. If a process with the
478480
specified server name already exists, the function returns
479481
`{:error, {:already_started, pid}}` with the pid of that process.
480482

0 commit comments

Comments
 (0)