File 0116-Improve-error-messages-for-lists-seq-3.patch of Package erlang
From 4c213fe478dc41a146b804fd12258230d248a77a Mon Sep 17 00:00:00 2001
From: nixxquality <nixx@is-fantabulo.us>
Date: Tue, 7 Dec 2021 12:11:13 +0100
Subject: [PATCH] Improve error messages for lists:seq/3
---
lib/stdlib/src/erl_stdlib_errors.erl | 15 ++++++++++++++-
lib/stdlib/src/lists.erl | 20 ++++++++++----------
2 files changed, 24 insertions(+), 11 deletions(-)
diff --git a/lib/stdlib/src/erl_stdlib_errors.erl b/lib/stdlib/src/erl_stdlib_errors.erl
index 91d8cb9362..c2e41fd496 100644
--- a/lib/stdlib/src/erl_stdlib_errors.erl
+++ b/lib/stdlib/src/erl_stdlib_errors.erl
@@ -189,7 +189,20 @@ format_lists_error(keysearch, Args) ->
format_lists_error(member, [_Key, List]) ->
[[], must_be_list(List)];
format_lists_error(reverse, [List, _Acc]) ->
- [must_be_list(List)].
+ [must_be_list(List)];
+format_lists_error(seq, [First, Last, Inc]) ->
+ case [must_be_integer(First), must_be_integer(Last), must_be_integer(Inc)] of
+ [[], [], []] ->
+ IncError = if
+ (Inc =< 0 andalso First - Inc =< Last) ->
+ <<"not a positive increment">>;
+ (Inc >= 0 andalso First - Inc >= Last) ->
+ <<"not a negative increment">>
+ end,
+ [[], [], IncError];
+ Errors -> Errors
+ end.
+
format_maps_error(filter, Args) ->
format_maps_error(map, Args);
diff --git a/lib/stdlib/src/lists.erl b/lib/stdlib/src/lists.erl
index 63d55f5630..11c9e25711 100644
--- a/lib/stdlib/src/lists.erl
+++ b/lib/stdlib/src/lists.erl
@@ -259,16 +259,16 @@ seq_loop(0, _, L) ->
Incr :: integer(),
Seq :: [integer()].
-seq(First, Last, Inc)
- when is_integer(First), is_integer(Last), is_integer(Inc) ->
- if
- Inc > 0, First - Inc =< Last;
- Inc < 0, First - Inc >= Last ->
- N = (Last - First + Inc) div Inc,
- seq_loop(N, Inc*(N-1)+First, Inc, []);
- Inc =:= 0, First =:= Last ->
- seq_loop(1, First, Inc, [])
- end.
+seq(First, Last, Inc)
+ when is_integer(First), is_integer(Last), is_integer(Inc),
+ (Inc > 0 andalso First - Inc =< Last) orelse
+ (Inc < 0 andalso First - Inc >= Last) ->
+ N = (Last - First + Inc) div Inc,
+ seq_loop(N, Inc * (N - 1) + First, Inc, []);
+seq(Same, Same, 0) when is_integer(Same) ->
+ [Same];
+seq(First, Last, Inc) ->
+ erlang:error(badarg, [First, Last, Inc], [{error_info, #{module => erl_stdlib_errors}}]).
seq_loop(N, X, D, L) when N >= 4 ->
Y = X-D, Z = Y-D, W = Z-D,
--
2.31.1