Skip to content

Iterating through an unbounded range causes a panic #69066

Closed
@jyn514

Description

@jyn514

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 ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions