Skip to content

Commit 9a3695c

Browse files
author
José Valim
committed
Only accept stacktraces in Module.get_attribute/3
1 parent 9499b9f commit 9a3695c

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

lib/elixir/lib/exception.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,8 @@ defmodule Exception do
286286
end
287287

288288
case trace do
289-
[entry|_] ->
290-
format_stacktrace_entry(entry)
291-
_ ->
292-
"nofile:0: "
289+
[entry|_] -> format_stacktrace_entry(entry)
290+
_ -> "nofile:0: "
293291
end
294292
end
295293

lib/elixir/lib/module.ex

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -745,12 +745,11 @@ defmodule Module do
745745
746746
Module.get_attribute(__MODULE__, :foo, true)
747747
748-
Notice the third argument is used to indicate if a warning
749-
should be emitted when the attribute was not previously defined.
750-
`warn` may also be a stacktrace, which will be used in the
751-
warning. The default value for `warn` is false for direct calls
752-
but the `@foo` macro sets it to the proper stacktrace automatically,
753-
warning every time `@foo` is used but not set previously.
748+
Notice the third argument may be given to indicate a stacktrace
749+
to be emitted when the attribute was not previously defined.
750+
The default value for `warn` is nil for direct calls but the `@foo`
751+
macro sets it to the proper stacktrace automatically, warning
752+
every time `@foo` is used but not set previously.
754753
755754
## Examples
756755
@@ -764,8 +763,9 @@ defmodule Module do
764763
end
765764
766765
"""
767-
@spec get_attribute(module, atom, warn :: boolean | [tuple]) :: term
768-
def get_attribute(module, key, warn // false) when is_atom(key) do
766+
@spec get_attribute(module, atom, warn :: nil | [tuple]) :: term
767+
def get_attribute(module, key, warn // nil) when
768+
is_atom(key) and (is_list(warn) or nil?(warn)) do
769769
assert_not_compiled!(:get_attribute, module)
770770
table = data_table_for(module)
771771
@@ -777,9 +777,8 @@ defmodule Module do
777777
cond do
778778
:lists.member(key, acc) ->
779779
[]
780-
warn ->
781-
stack = is_list(warn) and warn
782-
:elixir_errors.warn "#{Exception.format_caller(stack)} undefined module attribute @#{key}, " <>
780+
is_list(warn) ->
781+
:elixir_errors.warn "#{Exception.format_caller(warn)} undefined module attribute @#{key}, " <>
783782
"please remove access to @#{key} or explicitly set it to nil before access\n"
784783
nil
785784
true ->

0 commit comments

Comments
 (0)