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

Bogus "unreachable pattern" error with ranges #1170

Closed
elly opened this issue Nov 11, 2011 · 2 comments
Closed

Bogus "unreachable pattern" error with ranges #1170

elly opened this issue Nov 11, 2011 · 2 comments
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@elly
Copy link
Contributor

elly commented Nov 11, 2011

This rust program:
[str-escape] ~/s/rust$ cat test.rs
use std;

import std::str;

fn escape_char(c: char) -> str {
alt c {
'"' { """ }
'' { "\" }
'\n' { "\n" }
'\t' { "\t" }
'\r' { "\r" }
'\x00' to '\x1f' { #fmt["\x%02x", c as uint] }
v { str::from_char(c) }
}
}

fails to compile with this error:
test.rs:12:8: 12:24 error: unreachable pattern
test.rs:12 '\x00' to '\x1f' { #fmt["\x%02x", c as uint] }
^~~~~~~~~~~~~~~~
error: aborting due to previous errors

Inspection suggests that the logic in pattern_supersedes() in /src/comp/middle/check_alt.rs is wrong:
alt a.node {
pat_wild. | pat_bind(_) { ret true; }
pat_lit(la) {
alt b.node {
pat_lit(lb) { ret util::common::lit_eq(la, lb); }
pat_range(beginb, endb) {
ret util::common::lit_type_eq(la, beginb) &&
util::common::lit_in_range(la, beginb, endb);
}

^ This returns true if a (a previous pattern) is contained in b (the current pattern) - i.e., this will match for alt x { 1 { ... } 1 to 10 { ... } }.

@jdm
Copy link
Contributor

jdm commented Nov 11, 2011

The logic I had in mind when writing that check was that any overlapping patterns should be an error, and that any ranges that overlap should explicitly exclude previous patterns. I can follow the logic that any previous patterns should be exempt, though, and I would be willing to be convinced that that makes more sense.

@marijnh
Copy link
Contributor

marijnh commented Nov 12, 2011

I'm also in favour of changing this to only complain when the range is really unreachable.

XAMPPRocky added a commit to XAMPPRocky/rust that referenced this issue Apr 30, 2021
* No-op register_jit on Windows

Co-authored-by: bjorn3 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

3 participants