Skip to content

Commit 83abed9

Browse files
committed
Make inline const work for half open ranges
1 parent f8842b9 commit 83abed9

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ impl<'a> Parser<'a> {
10621062
})
10631063
} else if self.eat_keyword(kw::Unsafe) {
10641064
self.parse_block_expr(None, lo, BlockCheckMode::Unsafe(ast::UserProvided), attrs)
1065-
} else if self.check_inline_const() {
1065+
} else if self.check_inline_const(0) {
10661066
self.parse_const_block(lo.to(self.token.span))
10671067
} else if self.is_do_catch_block() {
10681068
self.recover_do_catch(attrs)

compiler/rustc_parse/src/parser/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,9 @@ impl<'a> Parser<'a> {
522522
self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const)
523523
}
524524

525-
fn check_inline_const(&mut self) -> bool {
526-
self.check_keyword(kw::Const)
527-
&& self.look_ahead(1, |t| match t.kind {
525+
fn check_inline_const(&self, dist: usize) -> bool {
526+
self.is_keyword_ahead(dist, &[kw::Const])
527+
&& self.look_ahead(dist + 1, |t| match t.kind {
528528
token::Interpolated(ref nt) => matches!(**nt, token::NtBlock(..)),
529529
token::OpenDelim(DelimToken::Brace) => true,
530530
_ => false,

compiler/rustc_parse/src/parser/pat.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<'a> Parser<'a> {
313313
let pat = self.parse_pat_with_range_pat(false, None)?;
314314
self.sess.gated_spans.gate(sym::box_patterns, lo.to(self.prev_token.span));
315315
PatKind::Box(pat)
316-
} else if self.check_inline_const() {
316+
} else if self.check_inline_const(0) {
317317
// Parse `const pat`
318318
let const_expr = self.parse_const_block(lo.to(self.token.span))?;
319319

@@ -722,8 +722,8 @@ impl<'a> Parser<'a> {
722722
}
723723

724724
/// Is the token `dist` away from the current suitable as the start of a range patterns end?
725-
fn is_pat_range_end_start(&mut self, dist: usize) -> bool {
726-
self.check_inline_const()
725+
fn is_pat_range_end_start(&self, dist: usize) -> bool {
726+
self.check_inline_const(dist)
727727
|| self.look_ahead(dist, |t| {
728728
t.is_path_start() // e.g. `MY_CONST`;
729729
|| t.kind == token::Dot // e.g. `.5` for recovery;
@@ -733,7 +733,7 @@ impl<'a> Parser<'a> {
733733
}
734734

735735
fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
736-
if self.check_inline_const() {
736+
if self.check_inline_const(0) {
737737
self.parse_const_block(self.token.span)
738738
} else if self.check_path() {
739739
let lo = self.token.span;

src/test/ui/inline-const/const-match-pat-range.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// build-pass
22

33
#![allow(incomplete_features)]
4-
#![feature(inline_const)]
4+
#![feature(inline_const, half_open_range_patterns, exclusive_range_pattern)]
55
fn main() {
66
const N: u32 = 10;
77
let x: u32 = 3;
@@ -20,4 +20,14 @@ fn main() {
2020
1 ..= const { N + 1 } => {},
2121
_ => {},
2222
}
23+
24+
match x {
25+
.. const { N + 1 } => {},
26+
_ => {},
27+
}
28+
29+
match x {
30+
const { N - 1 } .. => {},
31+
_ => {},
32+
}
2333
}

0 commit comments

Comments
 (0)