Skip to content

Commit

Permalink
Merge pull request #434 from erszcz/lc-generators-being-none
Browse files Browse the repository at this point in the history
Add examples of list comprehension generators being none()
  • Loading branch information
erszcz authored Aug 22, 2022
2 parents f647ae7 + f75b0b8 commit 4248daf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-module(lc_cannot_glb_different_variants).

%% Compare with:
%% - test/should_fail/lc_generator_should_fail.erl
%% - test/should_pass/lc_generator_not_none.erl

-export([g/2]).

-type t() :: {a, d}
| {a, [t()]}.

-spec g(integer(), t()) -> [t()].
g(1, {a, d} = T) ->
[T];
g(_, {a, Ts}) ->
%% `typechecker:add_type_pat({a, Ts}, {a, d} | {a, [t()]}, Env)' at `typechecker.erl:4310'
%% calls `glb(d, [t()])' to find the type of `Ts'.
%% The call determines that `Ts :: none()', as `d' and `[t()]' are incompatible.
%% This leads to a warning being reported for the following line.
[ T || T <- Ts ];
g(_, _) ->
[].
20 changes: 20 additions & 0 deletions test/should_fail/lc_generator_should_fail.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-module(lc_generator_should_fail).

%% Compare with:
%% - test/known_problems/should_pass/lc_cannot_glb_different_variants.erl
%% - test/should_pass/lc_generator_not_none.erl

-export([g/1]).

-type t() :: {a, d}
| {a, [t()]}.

-spec g(t()) -> [t()].
g({a, Ts}) ->
%% If we didn't use a list comprehension and just returned an empty list,
%% this would rightly fail with a nonexhaustiveness warning.
%[].
%% If we use a comprehension, glb(d, [t()]) determines that Ts :: none().
%% This glb() call is done from typechecker:add_type_pat({a, Ts}, {a, d} | {a, [t()]}, Env)
%% at typechecker.erl:4310
[ T || T <- Ts ].
16 changes: 16 additions & 0 deletions test/should_pass/lc_generator_not_none.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-module(lc_generator_not_none).

%% Compare with:
%% - test/known_problems/should_pass/lc_cannot_glb_different_variants.erl
%% - test/should_fail/lc_generator_should_fail.erl

-export([g/1]).

-type t() :: {a, d}
| {a, [t()]}.

-spec g(t()) -> [t()].
g({a, d} = T) ->
[T];
g({a, Ts}) ->
[ T || T <- Ts ].

0 comments on commit 4248daf

Please sign in to comment.