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

Implement const expressions and patterns (RFC 2920) #77124

Merged
merged 11 commits into from
Oct 17, 2020
Prev Previous commit
Next Next commit
Parse inline const patterns
  • Loading branch information
spastorino committed Oct 16, 2020
commit 59d07c3ae5a1f53924c7580fa302c397c362f1a2
3 changes: 3 additions & 0 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ impl<'a> Parser<'a> {
let pat = self.parse_pat_with_range_pat(false, None)?;
self.sess.gated_spans.gate(sym::box_patterns, lo.to(self.prev_token.span));
PatKind::Box(pat)
} else if self.check_inline_const() {
// Parse `const pat`
PatKind::Lit(self.parse_const_expr(lo.to(self.token.span))?)
} else if self.can_be_ident_pat() {
// Parse `ident @ pat`
// This can give false positives and parse nullary enums,
Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/parser/inline_const/const_match_pat_parses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// check-pass
// compile-flags: -Z parse-only

#![feature(inline_const)]
const MMIO_BIT1: u8 = 4;
const MMIO_BIT2: u8 = 5;

fn main() {
match read_mmio() {
0 => {}
const { 1 << MMIO_BIT1 } => println!("FOO"),
const { 1 << MMIO_BIT2 } => println!("BAR"),

_ => unreachable!(),
}
}

fn read_mmio() -> u8 {
1 << 5
}
2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-66357-unexpected-unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

fn f() { |[](* }
//~^ ERROR expected one of `,` or `:`, found `(`
//~| ERROR expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*`
//~| ERROR expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `const`, `mut`, `ref`, `|`, identifier, or path, found `*`
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: expected one of `,` or `:`, found `(`
LL | fn f() { |[](* }
| ^ expected one of `,` or `:`

error: expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*`
error: expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `const`, `mut`, `ref`, `|`, identifier, or path, found `*`
--> $DIR/issue-66357-unexpected-unreachable.rs:14:14
|
LL | fn f() { |[](* }
Expand Down