Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix overriding with Elixir. prefixed defmodule #13011

Merged
merged 3 commits into from
Oct 17, 2023
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
15 changes: 7 additions & 8 deletions lib/elixir/lib/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4889,14 +4889,13 @@ defmodule Kernel do
expanded = expand_module_alias(alias, env)

{expanded, with_alias} =
case is_atom(expanded) do
true ->
case alias_defmodule(alias, expanded, env) do
{full, old, new} ->
# Expand the module considering the current environment/nesting
{full, old, new} = alias_defmodule(alias, expanded, env)
meta = [defined: full, context: env.module] ++ alias_meta(alias)
{full, {:alias, meta, [old, [as: new, warn: false]]}}

false ->
nil ->
{expanded, nil}
end

Expand Down Expand Up @@ -4955,13 +4954,13 @@ defmodule Kernel do

defp expand_module_alias(other, env), do: Macro.expand(other, env)

defp alias_defmodule(_raw, module, _env) when not is_atom(module), do: nil

# defmodule Elixir.Alias
defp alias_defmodule({:__aliases__, _, [:"Elixir", _ | _]}, module, _env),
do: {module, module, nil}
defp alias_defmodule({:__aliases__, _, [:"Elixir", _ | _]}, _module, _env), do: nil

# defmodule Alias in root
defp alias_defmodule({:__aliases__, _, _}, module, %{module: nil}),
do: {module, module, nil}
defp alias_defmodule({:__aliases__, _, _}, _module, %{module: nil}), do: nil

# defmodule Alias nested
defp alias_defmodule({:__aliases__, _, [h | t]}, _module, env) when is_atom(h) do
Expand Down
31 changes: 31 additions & 0 deletions lib/elixir/test/elixir/kernel/alias_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,34 @@ defmodule Macro.AliasTest.User do
assert is_map(struct(Macro.AliasTest.User.Second, []).baz)
end
end

defmodule Kernel.AliasNestingEnvTest do
use ExUnit.Case, async: true

alias Another.AliasEnv, warn: false

def aliases_before, do: __ENV__.aliases

defmodule Elixir.AliasEnv do
def aliases_nested, do: __ENV__.aliases
end

def aliases_after, do: __ENV__.aliases

test "keeps env after overriding nested Elixir module of the same name" do
assert aliases_before() == [
{Elixir.Nested, Kernel.AliasTest.Nested},
{Elixir.AliasEnv, Another.AliasEnv}
]

assert Elixir.AliasEnv.aliases_nested() == [
{Elixir.Nested, Kernel.AliasTest.Nested},
{Elixir.AliasEnv, Another.AliasEnv}
]

assert aliases_after() == [
{Elixir.Nested, Kernel.AliasTest.Nested},
{Elixir.AliasEnv, Another.AliasEnv}
]
end
end
Loading