From adecd830aa2b8a6a2a24478fad13d4563bdb66f5 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Tue, 30 Dec 2025 00:02:53 -0500 Subject: [PATCH 1/2] raise ArgumentError for invalid Enum.count_until As a follow up to #15027, provide a nicer error message when invoking `Enum.count_until` with an invalid limit --- lib/elixir/lib/enum.ex | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index 2a9af55ac1..bca2dc26f1 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -780,6 +780,10 @@ defmodule Enum do end end + def count_until(_enumerable, _limit) do + raise ArgumentError, "Enum.count_until/2 only accepts limits greater than 0" + end + @doc """ Counts the elements in the enumerable for which `fun` returns a truthy value, stopping at `limit`. @@ -801,6 +805,10 @@ defmodule Enum do end end + def count_until(_enumerable, _fun, _limit) do + raise ArgumentError, "Enum.count_until/3 only accepts limits greater than 0" + end + @doc """ Enumerates the `enumerable`, returning a list where all consecutive duplicate elements are collapsed to a single element. From 379ed02cb5c48de19368d5e81c1a49a0af51bc06 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Tue, 30 Dec 2025 11:25:54 -0500 Subject: [PATCH 2/2] better message, tests --- lib/elixir/lib/enum.ex | 8 ++++---- lib/elixir/test/elixir/enum_test.exs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index bca2dc26f1..eeb2578776 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -780,8 +780,8 @@ defmodule Enum do end end - def count_until(_enumerable, _limit) do - raise ArgumentError, "Enum.count_until/2 only accepts limits greater than 0" + def count_until(_enumerable, limit) do + raise ArgumentError, "expected limit to be greater than 0, got: #{limit}" end @doc """ @@ -805,8 +805,8 @@ defmodule Enum do end end - def count_until(_enumerable, _fun, _limit) do - raise ArgumentError, "Enum.count_until/3 only accepts limits greater than 0" + def count_until(_enumerable, _fun, limit) do + raise ArgumentError, "expected limit to be greater than 0, got: #{limit}" end @doc """ diff --git a/lib/elixir/test/elixir/enum_test.exs b/lib/elixir/test/elixir/enum_test.exs index 7396f04663..4a53bc47c7 100644 --- a/lib/elixir/test/elixir/enum_test.exs +++ b/lib/elixir/test/elixir/enum_test.exs @@ -225,6 +225,16 @@ defmodule EnumTest do assert count_until_stream.([1, 2], 2) == 2 end + test "count_until/2 with invalid limit" do + assert_raise ArgumentError, "expected limit to be greater than 0, got: 0", fn -> + Enum.count_until([1, 2, 3], 0) + end + + assert_raise ArgumentError, "expected limit to be greater than 0, got: -22", fn -> + Enum.count_until([1, 2, 3], -22) + end + end + test "count_until/3" do assert Enum.count_until([1, 2, 3, 4, 5, 6], fn x -> rem(x, 2) == 0 end, 2) == 2 assert Enum.count_until([1, 2], fn x -> rem(x, 2) == 0 end, 2) == 1 @@ -243,6 +253,16 @@ defmodule EnumTest do assert count_until_stream.([], fn x -> rem(x, 2) == 0 end, 2) == 0 end + test "count_until/3 with invalid limit" do + assert_raise ArgumentError, "expected limit to be greater than 0, got: 0", fn -> + Enum.count_until([1, 2, 3], fn x -> rem(x, 2) == 0 end, 0) + end + + assert_raise ArgumentError, "expected limit to be greater than 0, got: -22", fn -> + Enum.count_until([1, 2, 3], fn x -> rem(x, 2) == 0 end, -22) + end + end + test "dedup/1" do assert Enum.dedup([1, 1, 2, 1, 1, 2, 1]) == [1, 2, 1, 2, 1] assert Enum.dedup([2, 1, 1, 2, 1]) == [2, 1, 2, 1]