Skip to content

Commit 1459fbb

Browse files
committed
Merge pull request #1766 from pguyot/w29/fix-lists-seq-2
Fix `lists:seq/2,3` when they should return an empty list These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 6686c4a + 17d5780 commit 1459fbb

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Fixed
1515

1616
- Fixed a bug where binary matching could fail due to a missing preservation of the matched binary.
17+
- Fixed a bug where `lists:seq/2` wouldn't return the empty list in valid cases.
1718

1819
### Changed
1920

libs/estdlib/src/lists.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,9 @@ join_1(_Sep, []) ->
601601
%%-----------------------------------------------------------------------------
602602
-spec seq(From :: integer(), To :: integer()) -> list().
603603
seq(From, To) when is_integer(From) andalso is_integer(To) andalso From =< To ->
604-
seq_r(From, To, 1, []).
604+
seq_r(From, To, 1, []);
605+
seq(From, To) when is_integer(From) andalso is_integer(To) andalso From =:= To + 1 ->
606+
[].
605607

606608
%%-----------------------------------------------------------------------------
607609
%% @param From from integer
@@ -623,6 +625,8 @@ seq(From, To, Incr) when
623625
error(badarg);
624626
seq(To, To, 0) ->
625627
[To];
628+
seq(From, To, Incr) when From =:= To + Incr ->
629+
[];
626630
seq(From, To, Incr) ->
627631
Last = From + ((To - From) div Incr) * Incr,
628632
seq_r(From, Last, Incr, []).

tests/libs/estdlib/test_lists.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ test_seq() ->
273273
?ASSERT_MATCH(lists:seq(5, 1, -1), [5, 4, 3, 2, 1]),
274274
?ASSERT_MATCH(lists:seq(1, 1, 0), [1]),
275275
?ASSERT_MATCH(lists:seq(1, 1), [1]),
276+
?ASSERT_MATCH(lists:seq(1, 0), []),
277+
?ASSERT_MATCH(lists:seq(1, 0, 1), []),
276278

277279
?ASSERT_ERROR(lists:seq(foo, 1)),
278280
?ASSERT_ERROR(lists:seq(1, bar)),

0 commit comments

Comments
 (0)