Skip to content

Commit 84f1f54

Browse files
authored
Rollup merge of rust-lang#78116 - spastorino:inline-const-in-range-pat, r=petrochenkov
Make inline const work in range patterns Fixes rust-lang#78108 which is a follow up of rust-lang#77124 r? @petrochenkov
2 parents f4780a9 + 5656a41 commit 84f1f54

File tree

6 files changed

+61
-14
lines changed

6 files changed

+61
-14
lines changed

compiler/rustc_parse/src/parser/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,8 @@ 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() {
1066-
self.parse_const_expr(lo.to(self.token.span))
1065+
} else if self.check_inline_const(0) {
1066+
self.parse_const_block(lo.to(self.token.span))
10671067
} else if self.is_do_catch_block() {
10681068
self.recover_do_catch(attrs)
10691069
} else if self.is_try_block() {

compiler/rustc_parse/src/parser/mod.rs

+4-4
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,
@@ -864,7 +864,7 @@ impl<'a> Parser<'a> {
864864
}
865865

866866
/// Parses inline const expressions.
867-
fn parse_const_expr(&mut self, span: Span) -> PResult<'a, P<Expr>> {
867+
fn parse_const_block(&mut self, span: Span) -> PResult<'a, P<Expr>> {
868868
self.sess.gated_spans.gate(sym::inline_const, span);
869869
self.eat_keyword(kw::Const);
870870
let blk = self.parse_block()?;

compiler/rustc_parse/src/parser/pat.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,15 @@ 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`
318-
PatKind::Lit(self.parse_const_expr(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,
@@ -717,16 +723,19 @@ impl<'a> Parser<'a> {
717723

718724
/// Is the token `dist` away from the current suitable as the start of a range patterns end?
719725
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`;
726+
self.check_inline_const(dist)
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(0) {
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,38 @@
1+
// build-pass
2+
3+
#![allow(incomplete_features)]
4+
#![feature(inline_const, half_open_range_patterns, exclusive_range_pattern)]
5+
fn main() {
6+
const N: u32 = 10;
7+
let x: u32 = 3;
8+
9+
match x {
10+
1 ..= const { N + 1 } => {},
11+
_ => {},
12+
}
13+
14+
match x {
15+
const { N - 1 } ..= 10 => {},
16+
_ => {},
17+
}
18+
19+
match x {
20+
const { N - 1 } ..= const { N + 1 } => {},
21+
_ => {},
22+
}
23+
24+
match x {
25+
.. const { N + 1 } => {},
26+
_ => {},
27+
}
28+
29+
match x {
30+
const { N - 1 } .. => {},
31+
_ => {},
32+
}
33+
34+
match x {
35+
..= const { N + 1 } => {},
36+
_ => {}
37+
}
38+
}

src/test/ui/parser/issue-66357-unexpected-unreachable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313

1414
fn f() { |[](* }
1515
//~^ ERROR expected one of `,` or `:`, found `(`
16-
//~| ERROR expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `const`, `mut`, `ref`, `|`, identifier, or path, found `*`
16+
//~| ERROR expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*`

src/test/ui/parser/issue-66357-unexpected-unreachable.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: expected one of `,` or `:`, found `(`
44
LL | fn f() { |[](* }
55
| ^ expected one of `,` or `:`
66

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

0 commit comments

Comments
 (0)