You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following code produces a warning: module attribute @my_value was set but never used warning
defmodule Macros do
defmacro save_this_somewhere(value) do
Module.put_attribute(__CALLER__.module, :storage, value)
loaded_value = Module.get_attribute(__CALLER__.module, :storage)
loaded_value |> IO.inspect(label: "loaded_value")
nil
end
end
defmodule Test do
import Macros
@my_value 1
save_this_somewhere(@my_value)
end
Expected behaviour
No warning
More complex version, but original version of the bug report
Not too important to look this section now that i think about it but it's a simplified architecture of my app, which triggered the warning. Just ignore this really
Current behavior
When we:
use a module attribute in a macro
save the value dynamically using Module.put_attribute
load the value dynamically using Module.get_attribute
but don't actually output any code into the compiled module that uses the module attribute
we get an warning: module attribute @my_value was set but never used warning.
defmoduleScratchdodefexpand_ast(ast,env)doMacro.prewalk(ast,fn{_,_,_}=node->Macro.expand(node,env)node->nodeend)enddefmacrosave_this_somewhere(value)doexpanded_value=expand_ast(value,__CALLER__)# Seemingly we need this for some reasonModule.put_attribute(__CALLER__.module,:storage,{:unquote,[],[expanded_value]})enddef__after_compile__(env,_)dovalue=Module.get_attribute(env.module,:storage)Module.create(Scratch.Storage,quotedodefget_stored_value()dounquote(Macro.escape(value,unquote: true))endend,__ENV__)Scratch.Storage.get_stored_value|>IO.inspect(label: "Stored value is")endenddefmoduleTestdoimportScratch@my_value1save_this_somewhere(@my_value)# WE ARE TOTALLY USING @my_value@after_compileScratchend
Expected behavior
We should not get a warning. We definitely used @my_value
The text was updated successfully, but these errors were encountered:
dylan-chong
changed the title
warning: module attribute @my_value was set but never used when used in after compilewarning: module attribute @my_value was set but never used when used in a specific way in a macro
Dec 16, 2020
Precheck
done
Environment
Current behaviour
The following code produces a
warning: module attribute @my_value was set but never used
warningExpected behaviour
No warning
More complex version, but original version of the bug report
Not too important to look this section now that i think about it but it's a simplified architecture of my app, which triggered the warning. Just ignore this really
Current behavior
When we:
Module.put_attribute
Module.get_attribute
warning: module attribute @my_value was set but never used
warning.Expected behavior
We should not get a warning. We definitely used
@my_value
The text was updated successfully, but these errors were encountered: