Skip to content

Commit a726dd8

Browse files
author
José Valim
committed
Improve cannot pipe error
1 parent 58d54af commit a726dd8

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

lib/elixir/lib/macro.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ defmodule Macro do
7474
def pipe(expr, call_args, position)
7575

7676
def pipe(expr, {:&, _, _} = call_args, _integer) do
77-
raise ArgumentError, "cannot pipe #{to_string expr} into #{to_string call_args}"
77+
bad_pipe(expr, call_args)
7878
end
7979

8080
def pipe(expr, {call, line, atom}, integer) when is_atom(atom) do
@@ -86,7 +86,12 @@ defmodule Macro do
8686
end
8787

8888
def pipe(expr, call_args, _integer) do
89-
raise ArgumentError, "cannot pipe #{to_string expr} into #{to_string call_args}"
89+
bad_pipe(expr, call_args)
90+
end
91+
92+
defp bad_pipe(expr, call_args) do
93+
raise ArgumentError, "cannot pipe #{to_string expr} into #{to_string call_args}, " <>
94+
"can only pipe into local calls foo(), remote calls Foo.bar() or anonymous functions calls foo.()"
9095
end
9196

9297
@doc """

lib/elixir/test/elixir/kernel_test.exs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,9 @@ defmodule KernelTest do
359359
assert Enum.map([1, 2, 3], &(&1 |> twice |> twice)) == [4, 8, 12]
360360
end
361361

362-
test "non-call" do
362+
test "anonymous functions" do
363363
assert 1 |> (&(&1*2)).() == 2
364364
assert [1] |> (&hd(&1)).() == 1
365-
366-
import CompileAssertion
367-
assert_compile_fail ArgumentError, "cannot pipe 1 into 2", "1 |> 2"
368365
end
369366

370367
defp twice(a), do: a * 2

lib/elixir/test/elixir/macro_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ defmodule MacroTest do
489489
assert Macro.pipe(1, quote(do: foo), -1) == quote(do: foo(1))
490490
assert Macro.pipe(2, quote(do: foo(1)), -1) == quote(do: foo(1, 2))
491491

492-
assert_raise ArgumentError, "cannot pipe 1 into 2", fn ->
492+
assert_raise ArgumentError, ~r"cannot pipe 1 into 2", fn ->
493493
Macro.pipe(1, 2, 0)
494494
end
495495
end

0 commit comments

Comments
 (0)