Open Fl4m3Ph03n1x opened 2 years ago
Thanks, @Fl4m3Ph03n1x! This is an interesting example of a false negative :+1: Marking it as a bug.
It should complain that the specification for the return type is incorrect.
Or that the returned value is of a wrong type if we assume that specs are the source of truth (they are for Gradient / Gradualizer). In any case, we should get a warning hinting at the discrepancy between the spec and the actual value.
I have a hunch that [Book]
in
@spec recommended_books(String.t()) :: [Book]
is interpreted as a list of atoms Book
which is a valid spec, since a singleton atom is a valid type. However, Gradient should then report a warning that the returned list [Book.t()]
does not match the declared type [Book]
. This needs further investigation.
Background
Let's imagine I have the following struct in a file called
book.ex
:Now let's define another module that actually uses this:
The careful reader will see that the specification for the
recommended_books
functions is wrong. It should be returns[Book.t()]
and not[Book]
, which is the struct.Actual behaviour
Expected behaviour
It should complain that the specification for the return type is incorrect. Dialyzer does this (although, in quite a cryptic manner):