Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterating through an unbounded range causes a panic #69066

Closed
jyn514 opened this issue Feb 11, 2020 · 3 comments
Closed

Iterating through an unbounded range causes a panic #69066

jyn514 opened this issue Feb 11, 2020 · 3 comments
Labels
C-bug Category: This is a bug.

Comments

@jyn514
Copy link
Member

jyn514 commented Feb 11, 2020

I tried this code:

fn main() {
    for i in 2147483646.. {
        println!("{}", i);
    }
}

I expected to see this happen: The numbers 2147483646 and 2147483647 should be printed, then the program should exit.

Instead, this happened: The standard library panicked.

Note that it might make sense to panic on overflow instead, but at least 2147483647 should printed first, since it's within the range of i32.

Playground

Meta

Version: stable (1.41.0), but panic also occurs on nightly (1.43.0-nightly (2020-02-10 e6ec0d1))

Backtrace

... snip ...
  13: core::panicking::panic
             at src/libcore/panicking.rs:52
  14: <i32 as core::ops::arith::Add>::add
             at /rustc/e6ec0d125eba4074122b187032474b4174fb9d31/src/libcore/ops/arith.rs:94
  15: <i32 as core::iter::range::Step>::add_one
             at /rustc/e6ec0d125eba4074122b187032474b4174fb9d31/src/libcore/iter/range.rs:67
  16: core::iter::range::<impl core::iter::traits::iterator::Iterator for core::ops::range::RangeFrom<A>>::next
             at /rustc/e6ec0d125eba4074122b187032474b4174fb9d31/src/libcore/iter/range.rs:314
  17: playground::main
             at src/main.rs:2
... snip ...

@jyn514 jyn514 added the C-bug Category: This is a bug. label Feb 11, 2020
@jyn514
Copy link
Member Author

jyn514 commented Feb 11, 2020

Note that the following code works as expected:

    for i in 2147483646..=2147483647 {
        println!("{}", i);
    }

@timvermeulen
Copy link
Contributor

The RangeFrom docs specify that it will currently panic in debug mode, and that this behavior is subject to change.

I'm not sure how intentional it is that the last value is left out, but given that RangeFrom only has a start field, it can't currently represent the state of just having returned the maximum value from next.

@ollie27
Copy link
Member

ollie27 commented Feb 12, 2020

Duplicate of #25708

@ollie27 ollie27 marked this as a duplicate of #25708 Feb 12, 2020
@ollie27 ollie27 closed this as completed Feb 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants