Do not try to accidentally load elixir.beam (#739)

This commit is contained in:
José Valim 2021-12-03 22:50:45 +01:00 committed by GitHub
parent 64fce545d3
commit 18d8eccc9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -107,20 +107,23 @@ defmodule Livebook.Intellisense.Docs do
:protocol | :implementation | :exception | :struct | :behaviour | nil :protocol | :implementation | :exception | :struct | :behaviour | nil
def get_module_subtype(module) do def get_module_subtype(module) do
cond do cond do
module_has_function?(module, :__protocol__, 1) -> not ensure_loaded?(module) ->
nil
function_exported?(module, :__protocol__, 1) ->
:protocol :protocol
module_has_function?(module, :__impl__, 1) -> function_exported?(module, :__impl__, 1) ->
:implementation :implementation
module_has_function?(module, :__struct__, 0) -> function_exported?(module, :__struct__, 0) ->
if module_has_function?(module, :exception, 1) do if function_exported?(module, :exception, 1) do
:exception :exception
else else
:struct :struct
end end
module_has_function?(module, :behaviour_info, 1) -> function_exported?(module, :behaviour_info, 1) ->
:behaviour :behaviour
true -> true ->
@ -128,7 +131,9 @@ defmodule Livebook.Intellisense.Docs do
end end
end end
defp module_has_function?(module, func, arity) do # In case insensitive file systems, attempting to load
Code.ensure_loaded?(module) and function_exported?(module, func, arity) # Elixir will log a warning in the terminal as it wrongly
end # loads elixir.beam, so we explicitly list it.
defp ensure_loaded?(Elixir), do: false
defp ensure_loaded?(module), do: Code.ensure_loaded?(module)
end end