Skip to content

Commit f8842b9

Browse files
committed
Make inline const work in range patterns
1 parent 954b5a8 commit f8842b9

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

compiler/rustc_parse/src/parser/pat.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,13 @@ impl<'a> Parser<'a> {
315315
PatKind::Box(pat)
316316
} else if self.check_inline_const() {
317317
// Parse `const pat`
318-
PatKind::Lit(self.parse_const_block(lo.to(self.token.span))?)
318+
let const_expr = self.parse_const_block(lo.to(self.token.span))?;
319+
320+
if let Some(re) = self.parse_range_end() {
321+
self.parse_pat_range_begin_with(const_expr, re)?
322+
} else {
323+
PatKind::Lit(const_expr)
324+
}
319325
} else if self.can_be_ident_pat() {
320326
// Parse `ident @ pat`
321327
// This can give false positives and parse nullary enums,
@@ -716,17 +722,20 @@ impl<'a> Parser<'a> {
716722
}
717723

718724
/// Is the token `dist` away from the current suitable as the start of a range patterns end?
719-
fn is_pat_range_end_start(&self, dist: usize) -> bool {
720-
self.look_ahead(dist, |t| {
721-
t.is_path_start() // e.g. `MY_CONST`;
725+
fn is_pat_range_end_start(&mut self, dist: usize) -> bool {
726+
self.check_inline_const()
727+
|| self.look_ahead(dist, |t| {
728+
t.is_path_start() // e.g. `MY_CONST`;
722729
|| t.kind == token::Dot // e.g. `.5` for recovery;
723730
|| t.can_begin_literal_maybe_minus() // e.g. `42`.
724731
|| t.is_whole_expr()
725-
})
732+
})
726733
}
727734

728735
fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
729-
if self.check_path() {
736+
if self.check_inline_const() {
737+
self.parse_const_block(self.token.span)
738+
} else if self.check_path() {
730739
let lo = self.token.span;
731740
let (qself, path) = if self.eat_lt() {
732741
// Parse a qualified path
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// build-pass
2+
3+
#![allow(incomplete_features)]
4+
#![feature(inline_const)]
5+
fn main() {
6+
const N: u32 = 10;
7+
let x: u32 = 3;
8+
9+
match x {
10+
const { N - 1 } ..= 10 => {},
11+
_ => {},
12+
}
13+
14+
match x {
15+
const { N - 1 } ..= const { N + 1 } => {},
16+
_ => {},
17+
}
18+
19+
match x {
20+
1 ..= const { N + 1 } => {},
21+
_ => {},
22+
}
23+
}

0 commit comments

Comments
 (0)