Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Do not use Elixir modules to infer their own signatures #14254

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SHARE_PREFIX ?= $(PREFIX)/share
MAN_PREFIX ?= $(SHARE_PREFIX)/man
CANONICAL := main/
ELIXIRC := bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
ELIXIRC_MIN_SIG := $(ELIXIRC) -e 'Code.put_compiler_option :infer_signatures, []'
ERLC := erlc -I lib/elixir/include
ERL_MAKE := erl -make
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
Expand Down Expand Up @@ -97,17 +98,17 @@ $(KERNEL): lib/elixir/src/* lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir
"$(MAKE)" unicode; \
fi
@ echo "==> elixir (compile)";
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/**/*.ex" -o ebin;
$(Q) cd lib/elixir && ../../$(ELIXIRC_MIN_SIG) "lib/**/*.ex" -o ebin;

$(APP): lib/elixir/src/elixir.app.src lib/elixir/ebin VERSION $(GENERATE_APP)
$(Q) $(GENERATE_APP) $(VERSION)

unicode: $(UNICODE)
$(UNICODE): lib/elixir/unicode/*
@ echo "==> unicode (compile)";
$(Q) $(ELIXIRC) lib/elixir/unicode/unicode.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC) lib/elixir/unicode/tokenizer.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC) lib/elixir/unicode/security.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC_MIN_SIG) lib/elixir/unicode/unicode.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC_MIN_SIG) lib/elixir/unicode/tokenizer.ex -o lib/elixir/ebin;
$(Q) $(ELIXIRC_MIN_SIG) lib/elixir/unicode/security.ex -o lib/elixir/ebin;

$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
$(eval $(call APP_TEMPLATE,logger,Logger))
Expand Down
38 changes: 14 additions & 24 deletions lib/elixir/lib/module/parallel_checker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ defmodule Module.ParallelChecker do
{^ref, :cache} ->
Process.link(pid)

module_tuple =
{mode, module_tuple} =
cond do
is_binary(info) ->
location =
Expand All @@ -86,13 +86,15 @@ defmodule Module.ParallelChecker do
{:ok, module_map} <- backend.debug_info(:elixir_v1, module, data, []) do
cache_from_module_map(table, module_map)
else
_ -> nil
_ -> {:not_found, nil}
end

is_tuple(info) ->
info
end

# We only make the module available now, so they are not visible during inference
:ets.insert(table, {module, mode})
josevalim marked this conversation as resolved.
Show resolved Hide resolved
send(checker, {ref, :cached})

receive do
Expand Down Expand Up @@ -416,21 +418,22 @@ defmodule Module.ParallelChecker do
true ->
{mode, exports} = info_exports(module)
deprecated = info_deprecated(module)
cache_info(table, module, exports, deprecated, %{}, mode)
cache_info(table, module, exports, deprecated, %{})
mode

false ->
# Or load exports from chunk
with {^module, binary, _filename} <- object_code,
{:ok, {^module, [exports: exports]}} <- :beam_lib.chunks(binary, [:exports]) do
cache_info(table, module, exports, %{}, %{}, :erlang)
cache_info(table, module, exports, %{}, %{})
:erlang
else
_ ->
:ets.insert(table, {module, :not_found})
nil
_ -> :not_found
end
end
end

:ets.insert(table, {module, mode})
unlock(checker, module, mode)
end
end
Expand Down Expand Up @@ -461,26 +464,15 @@ defmodule Module.ParallelChecker do
behaviour_exports(map) ++
for({function, :def, _meta, _clauses} <- map.definitions, do: function)

cache_info(
table,
map.module,
exports,
Map.new(map.deprecated),
map.signatures,
elixir_mode(map.attributes)
)

module_map_to_module_tuple(map)
cache_info(table, map.module, exports, Map.new(map.deprecated), map.signatures)
{elixir_mode(map.attributes), module_map_to_module_tuple(map)}
end

defp cache_info(table, module, exports, deprecated, sigs, mode) do
defp cache_info(table, module, exports, deprecated, sigs) do
Enum.each(exports, fn fa ->
reason = Map.get(deprecated, fa)
:ets.insert(table, {{module, fa}, reason, Map.get(sigs, fa, :none)})
end)

:ets.insert(table, {module, mode})
mode
end

defp cache_chunk(table, module, contents) do
Expand All @@ -497,9 +489,7 @@ defmodule Module.ParallelChecker do
)
end)

mode = Map.get(contents, :mode, :elixir)
:ets.insert(table, {module, mode})
mode
Map.get(contents, :mode, :elixir)
end

defp behaviour_exports(%{defines_behaviour: true}), do: [{:behaviour_info, 1}]
Expand Down
Loading