Skip to content

char open ended range isn't bounded by char::MAX #111514

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

Open
ChrisDenton opened this issue May 12, 2023 · 5 comments
Open

char open ended range isn't bounded by char::MAX #111514

ChrisDenton opened this issue May 12, 2023 · 5 comments
Labels
A-Unicode Area: Unicode C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@ChrisDenton
Copy link
Member

I tried this code:

for _ in '\0'.. {}

I expected to see this happen: Runs without error.

Instead, this happened:

thread 'main' panicked at 'overflow in `Step::forward`', /rustc/2a8221dbdfd180a2d56d4b0089f4f3952d8c2bcd\library\core\src\iter\range.rs:88:45

Meta

rustc --version --verbose:

rustc 1.71.0-nightly (2a8221dbd 2023-05-11)
binary: rustc
commit-hash: 2a8221dbdfd180a2d56d4b0089f4f3952d8c2bcd
commit-date: 2023-05-11
host: x86_64-pc-windows-msvc
release: 1.71.0-nightly
LLVM version: 16.0.2
Backtrace

   0: std::panicking::begin_panic_handler
             at /rustc/2a8221dbdfd180a2d56d4b0089f4f3952d8c2bcd/library\std\src\panicking.rs:578
   1: core::panicking::panic_fmt
             at /rustc/2a8221dbdfd180a2d56d4b0089f4f3952d8c2bcd/library\core\src\panicking.rs:67
   2: core::panicking::panic_display
             at /rustc/2a8221dbdfd180a2d56d4b0089f4f3952d8c2bcd/library\core\src\panicking.rs:150
   3: core::panicking::panic_str
             at /rustc/2a8221dbdfd180a2d56d4b0089f4f3952d8c2bcd/library\core\src\panicking.rs:134
   4: core::option::expect_failed
             at /rustc/2a8221dbdfd180a2d56d4b0089f4f3952d8c2bcd/library\core\src\option.rs:1932
   5: core::iter::range::Step::forward
   6: core::iter::range::<impl core::iter::traits::iterator::Iterator for core::ops::range::RangeFrom<A>>::next
   7: core::fmt::Arguments::new_v1
   8: core::ops::function::FnOnce::call_once

@ChrisDenton ChrisDenton added C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 12, 2023
@compiler-errors
Copy link
Member

Why is this char specific -- are any open-ended ranges bounded? This code also panics with overflow:

fn main() {
    for x in 0u8.. {
        println!("{x}");
    }
}

@ChrisDenton
Copy link
Member Author

ChrisDenton commented May 12, 2023

Because a char isn't an integer. It is a finite set, whatever the underlying representation may be. It even has a hole in the middle when represented as an integer.

@SkiFire13
Copy link
Contributor

u8 is also not an integer but the finite set of numbers between 0 and 255 though

@joboet
Copy link
Member

joboet commented May 12, 2023

This is documented behaviour. Would it however be a breaking change to adjust the iterator implementation to stop on overflow? The current behaviour (panicking in debug and wrapping in release) is really unintuitive, and I can't imagine anyone relying on it, because it makes iteration over RangeFrom essentially useless.
Nevermind, that can't be implemented.

@ChrisDenton
Copy link
Member Author

u8 is also not an integer but the finite set of numbers between 0 and 255 though

I think a char is materially different from a primitive integer. A u8 can only hold a subset of integers (and behaviour on overflow can be configured). A char can hold every single Unicode scalar value that can ever exist. It can't overflow because there's nothing else to... flow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Unicode Area: Unicode C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants