Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples of list comprehension generators being none() #434

Merged
merged 5 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ].