Skip to content

Fix false positive with nested shadowed alias #13564

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 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 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) when is_atom(module) do
:gen_server.cast(pid, {:add_alias, module, meta, warn})
def add_alias(pid, module, meta, warn, function) when is_atom(module) do
:gen_server.cast(pid, {:add_alias, module, meta, warn, function})
end

@doc false
Expand Down Expand Up @@ -225,12 +225,16 @@ defmodule Kernel.LexicalTracker do
end
end

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

_ ->
state
end

{:noreply, put_in(state.aliases[module], meta)}
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}) ->
?tracker:add_alias(Pid, New, Meta, should_warn(Meta, Opts)),
trace({alias, Meta, _Old, New, Opts}, #{lexical_tracker := Pid, function := Function}) ->
?tracker:add_alias(Pid, New, Meta, should_warn(Meta, Opts), Function),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super massive nitpick:

Suggested change
?tracker:add_alias(Pid, New, Meta, should_warn(Meta, Opts), Function),
?tracker:add_alias(Pid, New, Meta, should_warn(Meta, Opts), Function /= nil),

So we send less data to the GenServer. :)

Copy link
Contributor Author

@sabiwara sabiwara May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

493727c too much, WDYT?
Since both variables were about how to warn. Might also be less confusing than the double bool.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much in my opinion. I feel like I need to undo the warn logic when I have to understand its impact in the lexical tracker. While checking for warn and function? had a clearer meaning to me. WDYT?

Copy link
Contributor Author

@sabiwara sabiwara May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine either way.

I liked the idea of having the emitting part decide what to warn, with atom names to explain, and the receiving part just following instructions.

The downside with the non-bool atom is that I needed to come up with names (not fully satisfied with :except_unused_shadowed), and also that we'd probably need guards to make sure we don't emit something else and end up with some random accidental behavior. Finally, consistency with other messages of the module is also a strong point to keep booleans.

Will revert to the double bool!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok;
trace({alias_expansion, _Meta, Lookup, _Result}, #{lexical_tracker := Pid}) ->
?tracker:alias_dispatch(Pid, Lookup),
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)
D.add_alias(config[:pid], String, 1, true, nil)
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)
D.add_alias(config[:pid], String, 1, true, nil)
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)
D.add_alias(config[:pid], String, 1, true, nil)
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)
D.add_alias(config[:pid], String, 1, false, nil)
assert D.collect_unused_aliases(config[:pid]) == []
end

Expand Down
17 changes: 17 additions & 0 deletions lib/elixir/test/elixir/kernel/warning_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,23 @@ defmodule Kernel.WarningTest do
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