rust-lang / rust-lang/rust

Inconsistent behavior when indexing slice/BTreeSet with `1..=0`

Open
#128,502 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use std::collections::BTreeSet;

fn main() {
    assert_eq!(&[123][1..=0], []);
    
    let set = BTreeSet::from_iter([123]);
    assert_eq!(set.range(1..=0).copied().collect::<Vec<i32>>(), &[]);
}

I expected to see this happen: Either both should panic since the range start is greater than the range end, or both should yield 0 items.

Instead, this happened: The slice indexing assertion passes, but the BTreeSet operation panics:

range start is greater than range end in BTreeSet
Meta

Reproduces on stable 1.80 and current nightly.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer comparing slice indexing with BTreeSet::range(1..=0), and confirm the behavior on stable and nightly Rust. Trace the relevant standard-library implementations and existing range tests, then determine which behavior should be made consistent. Done means the chosen behavior is consistent and covered by regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.