lok0613

lok0613

This dialyzer warning just happened randomly once I apply defmacro in my module.
I got 2 modules, the issues only appear in StoreFront module.

defmodule Store do

  defmacro __using__(_opt) do
    quote do
      import unquote(__MODULE__)
      @before_compile unquote(__MODULE__)
    end
  end

  defmacro fruits(do: block) do
    fn_name = String.to_atom("run_fruits")
    quote do
      @fn_names unquote(fn_name)
      def unquote(fn_name)(), do: unquote(block)
    end
  end

  defmacro apple(clause) do
    quote do
      if unquote(clause) in [true, :ok] do
        :ok
      else
        :failure
      end
    end
  end

  defmacro __before_compile__(_env) do
    quote do
      def run() do
        apply(__MODULE__, :run_fruits, [])
        |> IO.inspect
      end
    end
  end

end
defmodule StoreFront do
  use Store

  fruits do
    apple true # emit warning
    apple :ok # emit warning
    apple false # emit warning
  end

  apple true # no warning

end

The complete dialyzer warning messages:

The pattern 
          'false' can never match the type 
          'true'ElixirLS Dialyzer
The test 
          'ok' =:= 
          'true' can never evaluate to 'true'

Showing Posts 1 to 10

michallepicki

michallepicki

I think here dialyzer is detecting clauses that can never match (dead code). Dialyzer can see that some of the checks in the code are redundant. Your macros are probably generating a little bit more code that is really needed compared to if the code was written by hand (Dialyzer was designed for Erlang and I think Erlang only has simple substitution macros). That’s probably fine but I didn’t analyze your code too deeply.

NobbZ

NobbZ

“Expand” your macros, as well as elixirs stdlib macros until only defmodule and def are left in your StoreFront module. Then you will roughly see what dialyzer sees. That will help to understand the warnings.

lok0613

lok0613 OP

That’s not fine, coz it gives me warning…
How come a normal defmacro call is fine but not the nested defmacro..?

lok0613

lok0613 OP

{:__block__, [],
 [
   {:apple, [line: 5], [true]},
   {:apple, [line: 6], [:ok]},
   {:apple, [line: 7], [false]}
 ]}

It has nothing strange when I do “Macro.expand/2” here. I was matching all those cases in the if statement and even else can handle the rest…

NobbZ

NobbZ

No, not Macro.expand. Write the code as if you didn’t use macros, but as if you write the generated code directly.

lok0613

lok0613 OP

Alright, I can refactor as functions and it doesn’t emit any dialyzer warning.

def run_fruits() do
    apple(true)
    apple(:ok)
    apple(false)
  end

  def apple(value) do
    if value in [true, :ok] do
      :ok
    else
      :failure
    end
  end

  def run() do
    run_fruits()
    |> IO.inspect()
  end

I don’t get it.. wts wrong with the macros?

NobbZ

NobbZ

Writing functions which do roughly what your macro does, is not writing the code the way it had been generated by the macro call.

So it becomes this:

defmodule StoreFront do
  use Store  # its okay to leave this as is, as it is not mentioned in the warnings

  @fn_names :run_fruits
  def run_fruits() do
    case true === true or true === :ok do # apple true
      x in [false, nil] -> :failure
      _ -> :ok
    end

    case :ok === true or :ok === :ok do # apple :ok
      x in [false, nil] -> :failure
      _ -> :ok
    end

    case false === true or false === :ok do
      x in [false, nil] -> :failure
      _ -> :ok
    end
  end

  # apple true # no need to expand this, dialyzer won't see it anyway, as it does not expand into a function
end

And dialyzer is wondering why you say or true === :ok, when you already know that true === true statically.
It asks why you have a x in [false, nil] clause when you already know in advance, that the condition will always be true.

lok0613

lok0613 OP

I’m trying to narrow the case little bit.

defmacro apple(clause) do   quote do
    if unquote(clause) == :ok do
      :ok
    else
       :failure
    end
  end
end

Only comparing :ok.

fruits do
  apple :ok # emit warning
 end

apple :ok # not emit warning

It still emit dialyzer wanring even I’m actually checking :ok == :ok.

NobbZ

NobbZ

What warning does it emit?

Again, you say if true do … end, dialyzer is asking you, why you don’t just write .

Qqwy

Qqwy

TypeCheck Core Team

To check what code you exactly end up with, you might want to use the following incancation by the way:

    f = './_build/dev/lib/your_library_name/ebin/Elixir.YourModuleName.beam'
    result = :beam_lib.chunks(f,[:abstract_code])
    {:ok,{_,[{:abstract_code,{_,ac}}]}} = result
    IO.puts :erl_prettypr.format(:erl_syntax.form_list(ac))

(where your_library_name and YourModuleName are replaced by the mix project and module name you’re working on, respectively)

This will show you the core Erlang that ends up being generated after all macros (both your macros and the built-in ones) are expanded. This is actually what Dialyzer is looking at.

(If someone knows a more concise or clean way to look at the core erlang of a module, I’d love to know, by he way!)

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews