Skip to content

Commit c0b007a

Browse files
author
José Valim
committed
Never allow a comprehension to behave like a guard
1 parent 2fee17e commit c0b007a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,19 @@ defmodule Kernel.SpecialForms do
813813
iex> lc <<r::8, g::8, b::8>> inbits pixels, do: {r, g, b}
814814
[{213,45,132},{64,76,32},{76,0,0},{234,32,15}]
815815
816+
Note: Differently from Erlang, Elixir comprehensions filters
817+
never behave as guards when it comes to errors. Errors in
818+
list comprehensions will always be raised. Consider this
819+
Erlang example:
820+
821+
erl> [I || I <- [1,2,3], hd(I)].
822+
[]
823+
824+
In Elixir, it will raise:
825+
826+
iex> lc i inlist [1,2,3], hd(i), do: i
827+
** (ArgumentError) argument error
828+
816829
"""
817830
defmacro lc(args)
818831

lib/elixir/src/elixir_translator.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,5 +713,7 @@ translate_comprehension_clause(_Meta, {inlist, Meta, [Left, Right]}, S) ->
713713
{ { generate, ?line(Meta), TLeft, TRight }, SL };
714714

715715
translate_comprehension_clause(Meta, X, S) ->
716+
Line = ?line(Meta),
716717
{ TX, TS } = translate_each(X, S),
717-
elixir_tree_helpers:convert_to_boolean(?line(Meta), TX, true, false, TS).
718+
{ BX, BS } = elixir_tree_helpers:convert_to_boolean(Line, TX, true, false, TS),
719+
{ { match, Line, { var, Line, '_' }, BX }, BS }.

0 commit comments

Comments
 (0)