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

Missing "unreachable pattern" warning on char ranges #79307

Closed
Nadrieril opened this issue Nov 22, 2020 · 5 comments · Fixed by #117611
Closed

Missing "unreachable pattern" warning on char ranges #79307

Nadrieril opened this issue Nov 22, 2020 · 5 comments · Fixed by #117611
Labels
A-exhaustiveness-checking Relating to exhaustiveness / usefulness checking of patterns A-unicode Area: Unicode C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Nadrieril
Copy link
Member

I tried this code (playground):

match 'a' {
    '\u{0}'..='\u{D7FF}' | '\u{E000}'..='\u{10_FFFF}' => {},
    '\u{D7FF}'..='\u{E000}' => {}, // should be unreachable
}

The first pattern by itself is exhaustive, because it covers all the valid chars. The second pattern looks like it includes more chars than the first, but it doesn't, because those extra chars are all invalid. So the second pattern should be marked unreachable.

This fails on all rust versions on the playground, and I think it's been that way since we implemented exhaustive_integer_patterns. I discovered it while working on the exhaustiveness algorithm.

@rustbot modify labels: +A-exhaustiveness-checking

@Nadrieril Nadrieril added the C-bug Category: This is a bug. label Nov 22, 2020
@rustbot rustbot added the A-exhaustiveness-checking Relating to exhaustiveness / usefulness checking of patterns label Nov 22, 2020
@leonardo-m
Copy link

Are such intervals ever going to change in the UNICODE world?

@Nadrieril
Copy link
Member Author

Hm, good question, I have no idea. But they are already hardcoded in the compiler since the following is exhaustive:

match 'a' {
    '\u{0}'..='\u{D7FF}' | '\u{E000}'..='\u{10_FFFF}' => {}
}

So this issue is more about making the two consistent. If unicode changes it would change the above too.

@jyn514 jyn514 added A-unicode Area: Unicode T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 22, 2020
@Dylan-DPC
Copy link
Member

Current error:

warning: multiple patterns overlap on their endpoints
 --> src/main.rs:4:5
  |
3 |     '\u{0}'..='\u{D7FF}' | '\u{E000}'..='\u{10_FFFF}' => {},
  |     --------------------   -------------------------- this range overlaps on `'\u{e000}'`...
  |     |
  |     this range overlaps on `'\u{d7ff}'`...
4 |     '\u{D7FF}'..='\u{E000}' => {}, // should be unreachable
  |     ^^^^^^^^^^^^^^^^^^^^^^^ ... with this range
  |
  = note: you likely meant to write mutually exclusive ranges
  = note: `#[warn(overlapping_range_endpoints)]` on by default

@Dylan-DPC
Copy link
Member

Closing this as the warning message now indicates that the 2nd arm is unreachable.

@Nadrieril
Copy link
Member Author

Nadrieril commented Apr 12, 2023

That's an unrelated lint, it does not indicate that the 2nd arm is unreachable (only my comment in the code does). This still shows the issue. (thx so much for triaging tho!)

@Nadrieril Nadrieril reopened this Apr 12, 2023
@bors bors closed this as completed in ee80c8d Nov 26, 2023
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Nov 27, 2023
Rewrite exhaustiveness in one pass

This is at least my 4th attempt at this in as many years x) Previous attempts were all too complicated or too slow. But we're finally here!

The previous version of the exhaustiveness algorithm computed reachability for each arm then exhaustiveness of the whole match. Since each of these steps does roughly the same things, this rewrites the algorithm to do them all in one go. I also think this makes things much simpler.

I also rewrote the documentation of the algorithm in depth. Hopefully it's up-to-date and easier to follow now. Plz comment if anything's unclear.

r? `@oli-obk` I think you're one of the rare other people to understand the exhaustiveness algorithm?

cc `@varkor` I know you're not active anymore, but if you feel like having a look you might enjoy this :D

Fixes rust-lang/rust#79307
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-exhaustiveness-checking Relating to exhaustiveness / usefulness checking of patterns A-unicode Area: Unicode C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants