I’m having trouble understanding why function deprecation warnings don’t always show up when I compile code that uses deprecated functions.
Let me explain with an example: HashDict.new/0
is a deprecated function. Let’s say I have a file called deprecated_test.ex
. The file contains a module and a function definition that uses the deprecated function:
# deprecated_test.ex
defmodule Foo do
def foo, do: HashDict.new()
end
If I compile the file with elixirc
, I get a nice deprecation warning:
angelika in ~/Documents
$ elixirc deprecated_test.ex
warning: HashDict.new/0 is deprecated. Use maps and the Map module instead
deprecated_test.ex:2: Foo.foo/0
However, if I start iex
, load the same file and compile it with Code.compile_string/1
, I don’t get the warning:
iex(2)> code = File.read!("./deprecated_test.ex")
"defmodule Foo do\n def foo, do: HashDict.new()\nend\n"
iex(3)> Code.compile_string(code)
[
{Foo,
<<70, 79, 82, 49, 0, 0, 4, 208, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0,
153, 0, 0, 0, 16, 10, 69, 108, 105, 120, 105, 114, 46, 70, 111, 111, 8, 95,
95, 105, 110, 102, 111, 95, 95, 10, 97, 116, ...>>}
]
I thought this means that Code.compile_string/1
won’t give me deprecation warnings at all, but that’s not true.
I get a deprecation warning about HashDict.new/0
if I try different code snippets:
iex(4)> Code.compile_string("fn -> HashDict.new() end")
warning: HashDict.new/0 is deprecated. Use maps and the Map module instead
nofile:1
[]
or
iex(15)> Code.compile_string("ref = &HashDict.new/0")
warning: HashDict.new/0 is deprecated. Use maps and the Map module instead
nofile:1
warning: variable "ref" is unused (if the variable is not meant to be used, prefix it with an underscore)
nofile:1
[]
Why is this happening? Can I somehow ensure that I will always get exactly the same deprecation warnings when compiling code with Code.compile_string/1
and/or Code.compile_quoted/1
as I would get with elixirc
?