Skip to content

Fix Range.disjoint?/2 for single-element ranges with a negative step#15582

Merged
josevalim merged 1 commit into
elixir-lang:mainfrom
nseaSeb:fix-range-disjoint-single-element-negative-step
Jul 10, 2026
Merged

Fix Range.disjoint?/2 for single-element ranges with a negative step#15582
josevalim merged 1 commit into
elixir-lang:mainfrom
nseaSeb:fix-range-disjoint-single-element-negative-step

Conversation

@nseaSeb

@nseaSeb nseaSeb commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

member?/2, size/1 and to_list/1 all treat a single-element range such as 1..1//-2 as the set [1], and Range.split/2 produces such ranges from library code (e.g. Range.split(5..1//-2, 2) yields 1..1//-2). Yet disjoint?/2 reported two of them as sharing no element:

Range.disjoint?(1..1//-2, 1..1//-2)  #=> true (should be false)

normalize/3 only reversed ranges when first > last, leaving a negative step in place for single-element ranges (first == last). The arithmetic-progression intersection in disjoint?/2 assumes an increasing progression, so the leftover negative step produced a wrong result. The step == -1 case was masked by the abs(step) == 1 shortcut, so only step <= -2 was affected.

Assisted-by: Claude Code:claude-opus-4-8

`member?/2`, `size/1` and `to_list/1` all treat a single-element range
such as `1..1//-2` as the set `[1]`, and `Range.split/2` produces such
ranges from library code (e.g. `Range.split(5..1//-2, 2)` yields
`1..1//-2`). Yet `disjoint?/2` reported two of them as sharing no
element:

    Range.disjoint?(1..1//-2, 1..1//-2)  #=> true (should be false)

`normalize/3` only reversed ranges when `first > last`, leaving a
negative step in place for single-element ranges (`first == last`). The
arithmetic-progression intersection in `disjoint?/2` assumes an
increasing progression, so the leftover negative step produced a wrong
result. The `step == -1` case was masked by the `abs(step) == 1`
shortcut, so only `step <= -2` was affected.

Assisted-by: Claude Code:claude-opus-4-8
@josevalim josevalim merged commit b3e4738 into elixir-lang:main Jul 10, 2026
15 checks passed
@josevalim

Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants