elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Structs nested in domain-key maps are not inferred

Open sabiwara opened this issue 2 months ago • 2 comments

Elixir and Erlang/OTP versions

Erlang/OTP 28 [erts-16.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.19.3 (compiled with Erlang/OTP 28)

Also main.

Current behavior

defmodule Repro do
  def update(%{"foo" => %Date{year: year} = date}) do
    %Date{date | year: year + 1}
  end
end
    warning: a struct for Date is expected on struct update:

        %Date{date | year: year + 1}

    but got type:

        dynamic()

    where "date" was given the type:

        # type: dynamic()
        # from: lib/repro.ex:2:14
        %{"foo" => %Date{year: year} = date}

Expected behavior

No warning, like if we use atom keys in the map (def update(%{foo: %Date{year: year} = date}) do).

Even for domain keys, I think variables matching a nested pattern should be inferred with this pattern? Not sure if it is just a temporary state that will be addressed by https://github.com/elixir-lang/elixir/issues/14558 or if it is just a bug.

Originally reported here.

sabiwara avatar Nov 15 '25 23:11 sabiwara

Might be this TODO. Judging from the way the code currently works, there might not be an easy fix here, since we don't have a way to represent domain keys in paths yet...

sabiwara avatar Nov 16 '25 00:11 sabiwara

I have to rewrite this part of the code... so I will look into this (there is already some work in progress in here too).

josevalim avatar Nov 16 '25 01:11 josevalim