Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warning: module attribute @my_value was set but never used when used in a specific way in a macro #10579

Closed
dylan-chong opened this issue Dec 16, 2020 · 1 comment

Comments

@dylan-chong
Copy link
Contributor

dylan-chong commented Dec 16, 2020

Precheck

done

Environment

  • Elixir & Erlang/OTP versions (elixir --version):
Erlang/OTP 22 [erts-10.7.2.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Elixir 1.11.0-rc.0 (8e9ece0) (compiled with Erlang/OTP 21)
  • Operating system: mac os 10.14.6

Current behaviour

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.
defmodule Scratch do
  def expand_ast(ast, env) do
    Macro.prewalk(ast, fn
      {_, _, _} = node ->
        Macro.expand(node, env)

      node ->
        node
    end)
  end

  defmacro save_this_somewhere(value) do
    expanded_value = expand_ast(value, __CALLER__) # Seemingly we need this for some reason
    Module.put_attribute(__CALLER__.module, :storage, {:unquote, [], [expanded_value]})
  end

  def __after_compile__(env, _) do
    value = Module.get_attribute(env.module, :storage)

    Module.create(
      Scratch.Storage,
      quote do
        def get_stored_value() do
          unquote(Macro.escape(value, unquote: true))
        end
      end,
      __ENV__
    )

    Scratch.Storage.get_stored_value
    |> IO.inspect(label: "Stored value is")
  end
end

defmodule Test do
  import Scratch

  @my_value 1
  save_this_somewhere(@my_value)  # WE ARE TOTALLY USING @my_value

  @after_compile Scratch
end

Expected behavior

We should not get a warning. We definitely used @my_value


@dylan-chong dylan-chong changed the title warning: module attribute @my_value was set but never used when used in after compile warning: module attribute @my_value was set but never used when used in a specific way in a macro Dec 16, 2020
josevalim added a commit that referenced this issue Dec 17, 2020
@dylan-chong
Copy link
Contributor Author

Thanks for doing this so quickly @josevalim !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant