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'
Trending in Questions
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
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
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
“Expand” your macros, as well as elixirs stdlib macros until only
defmoduleanddefare left in yourStoreFrontmodule. Then you will roughly see what dialyzer sees. That will help to understand the warnings.lok0613
That’s not fine, coz it gives me warning…
How come a normal defmacro call is fine but not the nested defmacro..?
lok0613
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
No, not Macro.expand. Write the code as if you didn’t use macros, but as if you write the generated code directly.
lok0613
Alright, I can refactor as functions and it doesn’t emit any dialyzer warning.
I don’t get it.. wts wrong with the macros?
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:
And dialyzer is wondering why you say
or true === :ok, when you already know thattrue === truestatically.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
I’m trying to narrow the case little bit.
Only comparing
:ok.It still emit dialyzer wanring even I’m actually checking
:ok == :ok.NobbZ
What warning does it emit?
Again, you say
if true do … end, dialyzer is asking you, why you don’t just write….Qqwy
To check what code you exactly end up with, you might want to use the following incancation by the way:
(where
your_library_nameandYourModuleNameare 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!)