LionOps

LionOps

I’m still relatively new to Elixir, so I’m hoping someone can shed some light on what’s up with my mode and/or dialyzer:

defmodule Eclipse.Router.Connection do
  @moduledoc """
  Server for a connection
  """

  use GenServer, restart: :transient

  @local_opts [:binary, packet: :raw, active: false, reuseaddr: true, reuseport: true]
  @remote_opts [:binary, :inet, active: false, packet: :raw]

  @type state :: %{
          socket: Eclipse.Router.Socket.t()
        }

  @impl GenServer
  @spec init({Eclipse.Config.Connections.t(), :local}) :: {:ok, state()}
  def init({%Eclipse.Config.Connections{} = config, :local}) do
    {:ok, socket} = :gen_tcp.listen(config.local_port, @local_opts)
    {:ok, client} = :gen_tcp.accept(socket)
    {:ok, %{socket: Eclipse.Router.Socket.new(client)}}
  end

  @spec init({Eclipse.Config.Connections.t(), :remote}) :: {:ok, state()}
  def init({%Eclipse.Config.Connections{} = config, :remote}) do
    {:ok, socket} = :gen_tcp.connect(config.remote_hostname, config.remote_port, @remote_opts)
    {:ok, Eclipse.Router.Socket.new(socket)}
  end
end

The above code passes, but the second init has the wrong success typing. {:ok, Eclipse.Router.Socket.new(socket)} as opposed to {:ok, %{socket: Eclipse.Router.Socket.new(socket)}}

If I comment out the first init, I get the expected errors.

Some guidance is appreciated on what I’m doing wrong.

Showing Posts 1 to 9

dimitarvp

dimitarvp

How is it wrong? Check what you’re returning in the second function variant – it’s what Dialyzer is finding as well. You are returning a value without putting it in a map.

sabiwara

sabiwara

Elixir Core Team

By default dialyzer’s success typing is pretty loose and is happy as long as one branch is OK, here is a minimal example:

  # no error
  @spec foo(:int) :: integer()
  @spec foo(:str) :: binary()
  def foo(:int), do: 123
  def foo(:str), do: nil  # should be a binary!

But after adding the following flags to your mix.exs (this is the config I personally recommend and use):

 dialyzer: [flags: [:missing_return, :extra_return]]

Dialyzer is now able to catch these errors:

lib/repro.ex:19:extra_range
The type specification has too many types for the function.

Function:
Repro.foo/1

Extra type:
binary()

Success typing:
nil | 123

________________________________________________________________________________
lib/repro.ex:19:missing_range
The type specification is missing types returned by function.

Function:
Repro.foo/1

Type specification return types:
binary() | integer()

Missing from spec:
nil

That being said, even if dialyzer is able to understand this particular error with the right flags, it is important to keep in mind that it remains quite limited and that there are a whole range of type errors it won’t be able to catch.

LionOps

LionOps OP

state is a map, though, is it not? Both function variants have the same return spec, but return different things. Dialyzer will error on the second variant if I comment out the first variant, or if I remove the map from the first variant, it’ll error there.

LionOps

LionOps OP

I had those flags in place, and it’s not catching the error. If I’m understanding things correctly, I’ve setup dialxyr correctly and have the correct spec, but it’s not equipped to catch the error.

dimitarvp

dimitarvp

Your first variant has this as a return value:

The second variant however has this:

Maybe I misunderstand the problem but the difference is pretty clear.

LionOps

LionOps OP

Yes, the variants are different. My problem is that it’s not clear to dialyzer that the second function variant doesn’t match the spec.

I would expect dialyzer to error for the second variant because the return does not match the spec; however, it seems that as long as the first instance is correct, dialyzer is happy. That is a surprising behavior.

My mix config:

...
  def project do
    [
      app: :eclipse,
      version: "0.1.0",
      elixir: "~> 1.17",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      dialyzer: dialyzer()
    ]
  end
...
  defp dialyzer do
    [
      flags: [
        "-Werror_handling",
        "-Wextra_return",
        "-Wmissing_return",
        "-Wunderspecs",
        "-Wunknown",
        "-Wunmatched_returns"
      ]
    ]
  end

I don’t think it’s a configuration issue because the flags I’m using are in the output:

Starting Dialyzer
[
  check_plt: false,
  init_plt: ~c"eclipse/_build/dev/dialyxir_erlang-27.1.1_elixir-1.17.3_deps-dev.plt",
  files: [~c"eclipse/_build/dev/lib/eclipse/ebin/Elixir.Eclipse.Application.beam",
   ~c"eclipse/_build/dev/lib/eclipse/ebin/Elixir.Eclipse.Config.Client.beam",
   ~c"eclipse/_build/dev/lib/eclipse/ebin/Elixir.Eclipse.Config.Connections.beam",
   ~c"eclipse/_build/dev/lib/eclipse/ebin/Elixir.Eclipse.Launcher.beam",
   ~c"eclipse/_build/dev/lib/eclipse/ebin/Elixir.Eclipse.Router.Connection.beam",
   ...],
  warnings: [:error_handling, :extra_return, :missing_return, :underspecs, ...]
]
Total errors: 0, Skipped: 0, Unnecessary Skips: 0
done in 0m1.73s
done (passed successfully)

I would expect my code to not pass dialzyer, but it does. I’m trying to understand why.

sabiwara

sabiwara

Elixir Core Team

Indeed this seems like a bug or limitation in dialyzer, despite the [:missing_return, :extra_return] flags.

It seems it avoids “diving in” when containers superficially seem to have the correct type:

  @spec bar(:a | :b) :: {:ok, integer()}
  def bar(:a), do: {:ok, 1}
  def bar(:b), do: {:ok, ""}

No warning on {:ok, ""}, but it would catch it if it is the wrong atom {:error, 2} or wrong tuple size {:ok, 1, 2}.

For maps, it seems it is fine as long as the other close returns a map:

  @spec foo(:a | :b) :: %{foo: %{bar: integer()}}
  def foo(:a), do: %{foo: %{bar: 1}}
  def foo(:b), do: %{baz: "whatever"}

but fails when the top-level is not a map.

Will open up an issue to OTP.

sabiwara

sabiwara

Elixir Core Team
sabiwara

sabiwara

Elixir Core Team

Update from the issue:

This is a known limitation, extra_return and missing_return are best-effort and won’t catch much. :-/

— All posts loaded —

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
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
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
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
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
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews