No escape hatch for typing error

I’m currently migrating my codebase to Elixir 1.18.2. I was able to solve all the typing issues, except for one. The following code:

defmodule Run do
  def test do
    try do
      Code.compile_string("a + ")
    rescue
      e ->
        case e do
          %{column: column, snippet: %{offset: offset}} ->
            {:error, column + offset}

          _ ->
            {:error, :unknown}
        end
    end
  end
end

Gives me the following compilation warning:

This could be solved by adding guards, that will allow for infering the types in a future release. But at the moment I’m not able to migrate this code and there is no scapehatch for it.

Ah, I was able to workaround this warning by creating a ‘sum/2’ function. Since the type checking doesn’t flow throught functions, it works fine.

Sorry, can you show the full code that raises no warning?

Sure

defmodule Run do
  def test do
    try do
      Code.compile_string("a + ")
    rescue
      e ->
        case e do
          %{column: column, snippet: %{offset: offset}} ->
            {:error, sum(column, offset)}

          _ ->
            {:error, :unknown}
        end
    end
  end

  def sum(a, b) do
    a + b
  end
end
1 Like