Dialyzer complaint about MapSet.member not getting proper type as argument, possible specs bug in MapSet

I think that your scenario is perfectly valid, and I agree that it shouldn’t lead to a dialyzer error, but unfortunately you’re hitting the limitation of the dialyzer, MapSet typespec, and the fact that you’re generating the data during compilation.

A potential solution here would be to remove MapSet opaqueness in Elixir (wdyt @josevalim?). Even if that’s done, you’ll need to wait for the next Elixir version, and even then it’s only going to solve the problem for MapSet, not for other opaque types.

So in the meantime, you can reach for a bit of ugly trickery to confuse the dialyzer:

@doc false
@spec map_set :: MapSet.t
def  map_set(), do: apply(__MODULE__, :map_set_intern, [])

@doc false
def map_set_intern(), do: @map_set

And then only use map_set() to work with the mapset. I agree it’s ugly, but it should do the job (haven’t verified it though). If you take this path, I also advise adding a comment to properly explain this trickery.

3 Likes