Skip to content

Commit 12f62e4

Browse files
authored
Re-add simplified purging logic (elixir-lang#13455)
1 parent bcf747f commit 12f62e4

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/elixir/src/elixir_compiler.erl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ compile(Quoted, ArgsList, CompilerOpts, #{line := Line} = E) ->
4545
{Expanded, SE, EE} = elixir_expand:expand(Block, elixir_env:env_to_ex(E), E),
4646
elixir_env:check_unused_vars(SE, EE),
4747

48-
{Module, Fun} =
48+
{Module, Fun, LabelledLocals} =
4949
elixir_erl_compiler:spawn(fun() -> spawned_compile(Expanded, CompilerOpts, E) end),
5050

5151
Args = list_to_tuple(ArgsList),
52-
{dispatch(Module, Fun, Args), SE, EE}.
52+
{dispatch(Module, Fun, Args, LabelledLocals), SE, EE}.
5353

5454
spawned_compile(ExExprs, CompilerOpts, #{line := Line, file := File} = E) ->
5555
{Vars, S} = elixir_erl_var:from_env(E),
@@ -61,12 +61,22 @@ spawned_compile(ExExprs, CompilerOpts, #{line := Line, file := File} = E) ->
6161

6262
{Module, Binary} = elixir_erl_compiler:noenv_forms(Forms, File, [nowarn_nomatch | CompilerOpts]),
6363
code:load_binary(Module, "", Binary),
64-
{Module, Fun}.
64+
{Module, Fun, is_purgeable(Binary)}.
6565

66-
dispatch(Module, Fun, Args) ->
66+
is_purgeable(<<"FOR1", _Size:32, "BEAM", Rest/binary>>) ->
67+
do_is_purgeable(Rest).
68+
69+
do_is_purgeable(<<>>) -> true;
70+
do_is_purgeable(<<"LocT", 4:32, 0:32, _/binary>>) -> true;
71+
do_is_purgeable(<<"LocT", _:32, _/binary>>) -> false;
72+
do_is_purgeable(<<_:4/binary, Size:32, Beam/binary>>) ->
73+
<<_:(4 * trunc((Size+3) / 4))/binary, Rest/binary>> = Beam,
74+
do_is_purgeable(Rest).
75+
76+
dispatch(Module, Fun, Args, Purgeable) ->
6777
Res = Module:Fun(Args),
6878
code:delete(Module),
69-
return_compiler_module(Module),
79+
Purgeable andalso return_compiler_module(Module),
7080
Res.
7181

7282
code_fun(nil) -> '__FILE__';

lib/elixir/test/elixir/code_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,20 @@ defmodule Code.SyncTest do
647647
end
648648

649649
test "purges compiler modules" do
650-
quoted = quote(do: Agent.start_link(fn -> :ok end))
650+
quoted = quote(do: :ok)
651651
Code.compile_quoted(quoted)
652652

653653
{:ok, claimed} = Code.purge_compiler_modules()
654654
assert claimed > 0
655655

656656
{:ok, claimed} = Code.purge_compiler_modules()
657657
assert claimed == 0
658+
659+
quoted = quote(do: Agent.start_link(fn -> :ok end))
660+
Code.compile_quoted(quoted)
661+
662+
{:ok, claimed} = Code.purge_compiler_modules()
663+
assert claimed == 0
658664
end
659665

660666
test "returns previous options when setting compiler options" do

0 commit comments

Comments
 (0)