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

[BUG] INT_MIN..=INT_MAX is broken #1180

Closed
GrigorenkoPV opened this issue Jul 24, 2024 · 2 comments
Closed

[BUG] INT_MIN..=INT_MAX is broken #1180

GrigorenkoPV opened this issue Jul 24, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@GrigorenkoPV
Copy link

main: () = {
	MIN: u8 = 1;
	MAX: u8 = 255;
	for MIN..=MAX do (i) {
		std::cout << i as u32 << std::endl;
	}
}

correctly prints numbers from 1 to 255, while

main: () = {
	MIN: u8 = 0;
	MAX: u8 = 255;
	for MIN..=MAX do (i) {
		std::cout << i as u32 << std::endl;
	}
}

prints nothing.

(Not discovered by me).

If I have read the implementation right, the boundary conditions are not considered. For example, we have for 0..=UINT32_MAX. And the program will go into an infinite loop or do nothing, because for

    range(
        T const& f, 
        T const& l, 
        bool     include_last = false
    ) 
        : first{ f }
        , last{ l } 
    { 
        if (include_last) { 
            ++last; 
        }
    }

curr will never be (or will immediately be) equal to last.

@sookach
Copy link
Contributor

sookach commented Jul 25, 2024

Thank you for the bug report and especially for the detailed explanation.

@hsutter
Copy link
Owner

hsutter commented Jul 25, 2024

Yes, thanks!

Both examples now work correctly.

The fix I decided to use is that if the range type is integral, I widen the stored values so that it's always possible to represent it as a half-open range, which keeps the code simple (or at least simpler, no range/iterator is "simple" exactly 😉 ).

So now the only numeric range I don't support is one whose end value is numeric_limits< [size_t | ptrdiff_t ] >::max(). I think not supporting that case is fine... those are typically used as invalid/sentinel values, not valid program values that you'd want to include in a closed range, I think.

Again, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants