Skip to content

Revert alias shadowing warning due to false positives #13568

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

Merged
merged 2 commits into from
May 18, 2024
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
25 changes: 4 additions & 21 deletions lib/elixir/lib/kernel/lexical_tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ defmodule Kernel.LexicalTracker do
end

@doc false
def add_alias(pid, module, meta, warn, function?) when is_atom(module) do
:gen_server.cast(pid, {:add_alias, module, meta, warn, function?})
def add_alias(pid, module, meta, warn) when is_atom(module) do
:gen_server.cast(pid, {:add_alias, module, meta, warn})
end

@doc false
Expand Down Expand Up @@ -123,14 +123,7 @@ defmodule Kernel.LexicalTracker do

@doc false
def handle_call(:unused_aliases, _from, state) do
unused_aliases =
Enum.map(state.aliases, fn
{{:shadowed, module}, meta} -> {module, meta}
{module, meta} when is_atom(module) -> {module, meta}
end)
|> Enum.sort()

{:reply, unused_aliases, state}
{:reply, Enum.sort(state.aliases), state}
end

def handle_call(:unused_imports, _from, state) do
Expand Down Expand Up @@ -225,18 +218,8 @@ defmodule Kernel.LexicalTracker do
end
end

def handle_cast({:add_alias, module, meta, warn, function?}, state) do
def handle_cast({:add_alias, module, meta, warn}, state) do
if warn do
state =
case state do
# we only track shadowed unused aliases at the top-level
%{aliases: %{^module => meta}} when not function? ->
put_in(state.aliases[{:shadowed, module}], meta)

_ ->
state
end

{:noreply, put_in(state.aliases[module], meta)}
else
{:noreply, state}
Expand Down
4 changes: 2 additions & 2 deletions lib/elixir/lib/macro/env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ defmodule Macro.Env do

## Examples

iex> alias List, as: MyList, warn: false
iex> alias List, as: MyList
iex> Macro.Env.expand_alias(__ENV__, [], [:MyList])
{:alias, List}
iex> Macro.Env.expand_alias(__ENV__, [], [:MyList, :Nested])
Expand All @@ -456,7 +456,7 @@ defmodule Macro.Env do
If there is no alias or the alias starts with `Elixir.`
(which disables aliasing), then `:error` is returned:

iex> alias List, as: MyList, warn: false
iex> alias List, as: MyList
iex> Macro.Env.expand_alias(__ENV__, [], [:Elixir, MyList])
:error
iex> Macro.Env.expand_alias(__ENV__, [], [:AnotherList])
Expand Down
4 changes: 2 additions & 2 deletions lib/elixir/src/elixir_lexical.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ trace({import, Meta, Module, Opts}, #{lexical_tracker := Pid}) ->

?tracker:add_import(Pid, Module, Only, Meta, Imported and should_warn(Meta, Opts)),
ok;
trace({alias, Meta, _Old, New, Opts}, #{lexical_tracker := Pid, function := Function}) ->
?tracker:add_alias(Pid, New, Meta, should_warn(Meta, Opts), Function /= nil),
trace({alias, Meta, _Old, New, Opts}, #{lexical_tracker := Pid}) ->
?tracker:add_alias(Pid, New, Meta, should_warn(Meta, Opts)),
ok;
trace({alias_expansion, _Meta, Lookup, _Result}, #{lexical_tracker := Pid}) ->
?tracker:alias_dispatch(Pid, Lookup),
Expand Down
6 changes: 0 additions & 6 deletions lib/elixir/test/elixir/kernel/alias_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ defmodule Kernel.AliasTest do
assert Billing == :"Elixir.MyApp.Billing"
end

test "overriding parent alias with child alias" do
alias MyApp.Billing
alias Billing.Billing
assert Billing == :"Elixir.MyApp.Billing.Billing"
end

test "lexical" do
if true_fun() do
alias OMG, as: List, warn: false
Expand Down
8 changes: 4 additions & 4 deletions lib/elixir/test/elixir/kernel/lexical_tracker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule Kernel.LexicalTrackerTest do
end

test "can add aliases", config do
D.add_alias(config[:pid], String, 1, true, false)
D.add_alias(config[:pid], String, 1, true)
D.alias_dispatch(config[:pid], String)
assert D.references(config[:pid]) == {[], [], [], []}
end
Expand Down Expand Up @@ -93,18 +93,18 @@ defmodule Kernel.LexicalTrackerTest do
end

test "unused aliases", config do
D.add_alias(config[:pid], String, 1, true, false)
D.add_alias(config[:pid], String, 1, true)
assert D.collect_unused_aliases(config[:pid]) == [{String, 1}]
end

test "used aliases are not unused", config do
D.add_alias(config[:pid], String, 1, true, false)
D.add_alias(config[:pid], String, 1, true)
D.alias_dispatch(config[:pid], String)
assert D.collect_unused_aliases(config[:pid]) == []
end

test "aliases with no warn are not unused", config do
D.add_alias(config[:pid], String, 1, false, false)
D.add_alias(config[:pid], String, 1, false)
assert D.collect_unused_aliases(config[:pid]) == []
end

Expand Down
33 changes: 0 additions & 33 deletions lib/elixir/test/elixir/kernel/warning_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -894,39 +894,6 @@ defmodule Kernel.WarningTest do
purge(Sample)
end

test "unused alias due to shadowing" do
assert_warn_compile(
["nofile:2:3", "unused alias Baz"],
"""
defmodule Sample do
alias Foo.Baz
alias Bar.Baz

def baz, do: Baz
end
"""
)
after
purge(Sample)
end

test "does not warn when shadowing alias in nested block" do
assert capture_compile("""
defmodule Sample do
alias Foo.Baz

def foo do
alias Bar.Baz
Baz
end

def baz, do: Baz
end
""") == ""
after
purge(Sample)
end

test "unused inside dynamic module" do
import List, only: [flatten: 1], warn: false

Expand Down