Search code examples
erlangdialyzer

Can't get opaque types to cause an abstraction violation in dialyzer


Here is a module that defines an opaque type:

-module(a).
-export([people/0, get_names/1]).
-export_type([person/0]).

-opaque person()      :: {person, first_name()}.
-type first_name()    :: string().

-spec people() -> People when
      People :: [person()].

people() ->
    [{person, "Joe"}, {person, "Cindy"}].

-spec get_names(People) -> Names when
      People     :: [person()],
      Names      :: [string()].

get_names(Ps) ->
    get_names(Ps, []).

get_names([ {person, Name} | Ps ], Names) ->
    get_names(Ps, [Name|Names]);
get_names([], Names) ->
    lists:reverse(Names).

And here is a module that mucks around inside a person() type:

-module(b).
-export([do/0]).

-spec do() -> Name when
      Name :: string().

do() ->
    [{person, Name} | _People] = a:people(),
    Name.

And dialyzer:

~/erlang_programs$ dialyzer b.erl 
  Checking whether the PLT /Users/7stud/.dialyzer_plt is up-to-date... yes
  Proceeding with analysis...
Unknown functions:
  a:people/0
 done in 0m0.49s
done (passed successfully)

I expected dialyzer to complain.


Solution

  • This works:

     $ dialyzer a.erl b.erl
    
     Checking whether the PLT /Users/7stud/.dialyzer_plt is up-to-date... yes
      Proceeding with analysis...
    b.erl:7: Function do/0 has no local return
    b.erl:8: The attempt to match a term of type a:person() against the pattern  
     {'person', Name} breaks the opaqueness of the term
     done in 0m0.52s
    done (warnings were emitted)