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

Improved exhaustive integer matching #58851

Closed
fintelia opened this issue Mar 1, 2019 · 3 comments
Closed

Improved exhaustive integer matching #58851

fintelia opened this issue Mar 1, 2019 · 3 comments
Labels
needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it.

Comments

@fintelia
Copy link
Contributor

fintelia commented Mar 1, 2019

During the implementation/stabilization process for exhaustive integer matching, there was a request to support matches of the form:

fn foo(i: u8) {
    match i & 0x3 {
        0 => {}
        1 => {}
        2 => {}
        3 => {}
    }
}

It seems that this should be feasible to implement, if the exact structure is constrained enough. Specifically the addition would be useful even if the match expression had to be a single bitwise and between an unsigned integer literal/constant and an expression, and even if the constant had to be exactly one less than a power of two. In the compiler, this should then be a matter of recognizing the pattern and using an adjusted upper bound for the exhaustive match instead of std::{u8, u16, ...}::MAX.

I've been finding this exact pattern coming up frequently in a crate I've been working on.

@leonardo-m
Copy link

This could be handled with a more general value range analysis in the front-end.

@varkor
Copy link
Member

varkor commented Mar 2, 2019

I think this should be moved to the RFCs repository. I certainly wouldn't feel comfortable special-casing certain forms of expression like this, and even a limited form of value analysis is a complex feature. This has come up in the past, but I can't find references to other issues right now.

@oli-obk oli-obk added the needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. label Mar 3, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Mar 3, 2019

I agree, this needs an RFC. I think the relation to generic value arguments and thus integer subtypes is something that should be part of this discussion, too.

@oli-obk oli-obk closed this as completed Mar 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it.
Projects
None yet
Development

No branches or pull requests

4 participants