Skip to content

Incompatible type warning #10485

@TBK145

Description

@TBK145

Environment

Current behavior

We have a function for validating a certain struct, but our doctest seems to generate a warning. The warning itself is obvious, but the test is also to show it's wrong.

defmodule Foo do
  defstruct [:bar]

  @doc """
  Returns if the argument is a valid Foo struct or not.

  iex> Foo.is_valid?(%{})
  false
  iex> Foo.is_valid?(%Foo{})
  false
  iex> Foo.is_valid?(%Foo{bar: "baz"})
  true
  """
  @spec is_valid?(any()) :: boolean()
  defguard is_valid?(term)
           when is_struct(term, __MODULE__) and is_map_key(term, :bar) and not is_nil(term.bar)
end

I think that the code and tests are valid, but the doctest generates this warning:

warning: undefined field "bar" in expression:

    # (for doctest at) lib/foo.ex:7
    arg0.bar

expected one of the following fields: 

where "arg0" was given the type map() in:

    # (for doctest at) lib/foo.ex:7
    {arg0} = {%{}}

Conflict found at
  (for doctest at) lib/foo.ex:7: FooTest."doctest Foo.is_valid?/1 (1)"/1

It will show the error for any type other than %Foo{}.

Expected behavior

No warnings when running the doctest.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions