Skip to content

Commit 3f2b44f

Browse files
author
Yuki Ito
committed
Make sure unintialized @Attribute warnings points to the correct file
1 parent 12dabed commit 3f2b44f

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

lib/elixir/lib/exception.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,13 @@ defmodule Exception do
282282
trace = trace || try do
283283
throw(:stacktrace)
284284
catch
285-
:stacktrace -> Enum.drop(:erlang.get_stacktrace, 1)
285+
:stacktrace -> Enum.drop(:erlang.get_stacktrace, 2)
286286
end
287287

288-
if entry = Enum.at(trace, 1) do
289-
format_stacktrace_entry(entry)
290-
else
288+
case trace do
289+
[entry|_] ->
290+
format_stacktrace_entry(entry)
291+
_ ->
291292
"nofile:0: "
292293
end
293294
end

lib/elixir/lib/kernel.ex

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,17 +2063,18 @@ defmodule Kernel do
20632063

20642064
# @attribute or @attribute()
20652065
defp do_at(args, name, function?, env) when is_atom(args) or args == [] do
2066+
stack =
2067+
case bootstraped?(Path) do
2068+
true -> env.stacktrace
2069+
false -> []
2070+
end
2071+
20662072
case function? do
20672073
true ->
2068-
stack =
2069-
case bootstraped?(Path) do
2070-
true -> env.stacktrace
2071-
false -> []
2072-
end
20732074
attr = Module.get_attribute(env_module(env), name, stack)
20742075
:erlang.element(1, :elixir_quote.escape(attr, false))
20752076
false ->
2076-
quote do: Module.get_attribute(__MODULE__, unquote(name), true)
2077+
quote do: Module.get_attribute(__MODULE__, unquote(name), unquote(Macro.escape(stack)))
20772078
end
20782079
end
20792080

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
defmodule AttributeWarning do
2+
@foo
3+
end

lib/elixir/test/elixir/kernel/warning_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule Kernel.WarningTest do
44
use ExUnit.Case
55
import ExUnit.CaptureIO
66

7+
import PathHelpers
8+
79
defp capture_err(fun) do
810
capture_io(:stderr, fun)
911
end
@@ -329,6 +331,14 @@ defmodule Kernel.WarningTest do
329331
purge Sample
330332
end
331333

334+
test :undefined_module_attribute_with_file do
335+
assert capture_err(fn ->
336+
Code.load_file(fixture_path("attribute_warning.ex"))
337+
end) =~ "attribute_warning.ex:2: AttributeWarning (module) undefined module attribute @foo, please remove access to @foo or explicitly set it to nil before access"
338+
after
339+
purge AttributeWarning
340+
end
341+
332342
test :in_guard_empty_list do
333343
assert capture_err(fn ->
334344
Code.eval_string """

0 commit comments

Comments
 (0)