Skip to content

Commit e7cb210

Browse files
committed
Do not add compile time deps on args to Application.compile_env/2, closes #11052
1 parent 4a4657e commit e7cb210

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/elixir/lib/application.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,22 @@ defmodule Application do
507507
raise "Application.compile_env/3 cannot be called inside functions, only in the module body"
508508
end
509509

510+
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
511+
510512
quote do
511513
Application.__compile_env__(unquote(app), unquote(key_or_path), unquote(default), __ENV__)
512514
end
513515
end
514516

517+
defp expand_key_or_path({:__aliases__, _, _} = alias, env),
518+
do: Macro.expand(alias, %{env | function: {:__info__, 1}})
519+
520+
defp expand_key_or_path(list, env) when is_list(list),
521+
do: Enum.map(list, &expand_key_or_path(&1, env))
522+
523+
defp expand_key_or_path(other, _env),
524+
do: other
525+
515526
@doc false
516527
def __compile_env__(app, key_or_path, default, env) do
517528
case fetch_compile_env(app, key_or_path, env) do
@@ -533,6 +544,8 @@ defmodule Application do
533544
raise "Application.compile_env!/2 cannot be called inside functions, only in the module body"
534545
end
535546

547+
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
548+
536549
quote do
537550
Application.__compile_env__!(unquote(app), unquote(key_or_path), __ENV__)
538551
end

lib/elixir/test/elixir/kernel/lexical_tracker_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,20 @@ defmodule Kernel.LexicalTrackerTest do
202202
assert URI in exports
203203
refute URI in runtime
204204
end
205+
206+
test "compile_env! does not add a compile dependency" do
207+
{{compile, exports, runtime, _}, _binding} =
208+
Code.eval_string("""
209+
defmodule Kernel.LexicalTrackerTest.CompileEnvStruct do
210+
Application.compile_env(:elixir, URI)
211+
Application.compile_env(:elixir, [:foo, URI, :bar])
212+
Kernel.LexicalTracker.references(__ENV__.lexical_tracker)
213+
end |> elem(3)
214+
""")
215+
216+
refute URI in compile
217+
refute URI in exports
218+
assert URI in runtime
219+
end
205220
end
206221
end

0 commit comments

Comments
 (0)