Rollup merge of #78116 - spastorino:inline-const-in-range-pat, r=petrochenkov
Make inline const work in range patterns Fixes #78108 which is a follow up of https://github.com/rust-lang/rust/pull/77124 r? @petrochenkov
This commit is contained in:
commit
982c4b3081
6 changed files with 61 additions and 14 deletions
|
@ -1062,8 +1062,8 @@ impl<'a> Parser<'a> {
|
||||||
})
|
})
|
||||||
} else if self.eat_keyword(kw::Unsafe) {
|
} else if self.eat_keyword(kw::Unsafe) {
|
||||||
self.parse_block_expr(None, lo, BlockCheckMode::Unsafe(ast::UserProvided), attrs)
|
self.parse_block_expr(None, lo, BlockCheckMode::Unsafe(ast::UserProvided), attrs)
|
||||||
} else if self.check_inline_const() {
|
} else if self.check_inline_const(0) {
|
||||||
self.parse_const_expr(lo.to(self.token.span))
|
self.parse_const_block(lo.to(self.token.span))
|
||||||
} else if self.is_do_catch_block() {
|
} else if self.is_do_catch_block() {
|
||||||
self.recover_do_catch(attrs)
|
self.recover_do_catch(attrs)
|
||||||
} else if self.is_try_block() {
|
} else if self.is_try_block() {
|
||||||
|
|
|
@ -522,9 +522,9 @@ impl<'a> Parser<'a> {
|
||||||
self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const)
|
self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_inline_const(&mut self) -> bool {
|
fn check_inline_const(&self, dist: usize) -> bool {
|
||||||
self.check_keyword(kw::Const)
|
self.is_keyword_ahead(dist, &[kw::Const])
|
||||||
&& self.look_ahead(1, |t| match t.kind {
|
&& self.look_ahead(dist + 1, |t| match t.kind {
|
||||||
token::Interpolated(ref nt) => matches!(**nt, token::NtBlock(..)),
|
token::Interpolated(ref nt) => matches!(**nt, token::NtBlock(..)),
|
||||||
token::OpenDelim(DelimToken::Brace) => true,
|
token::OpenDelim(DelimToken::Brace) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -864,7 +864,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses inline const expressions.
|
/// Parses inline const expressions.
|
||||||
fn parse_const_expr(&mut self, span: Span) -> PResult<'a, P<Expr>> {
|
fn parse_const_block(&mut self, span: Span) -> PResult<'a, P<Expr>> {
|
||||||
self.sess.gated_spans.gate(sym::inline_const, span);
|
self.sess.gated_spans.gate(sym::inline_const, span);
|
||||||
self.eat_keyword(kw::Const);
|
self.eat_keyword(kw::Const);
|
||||||
let blk = self.parse_block()?;
|
let blk = self.parse_block()?;
|
||||||
|
|
|
@ -313,9 +313,15 @@ impl<'a> Parser<'a> {
|
||||||
let pat = self.parse_pat_with_range_pat(false, None)?;
|
let pat = self.parse_pat_with_range_pat(false, None)?;
|
||||||
self.sess.gated_spans.gate(sym::box_patterns, lo.to(self.prev_token.span));
|
self.sess.gated_spans.gate(sym::box_patterns, lo.to(self.prev_token.span));
|
||||||
PatKind::Box(pat)
|
PatKind::Box(pat)
|
||||||
} else if self.check_inline_const() {
|
} else if self.check_inline_const(0) {
|
||||||
// Parse `const pat`
|
// Parse `const pat`
|
||||||
PatKind::Lit(self.parse_const_expr(lo.to(self.token.span))?)
|
let const_expr = self.parse_const_block(lo.to(self.token.span))?;
|
||||||
|
|
||||||
|
if let Some(re) = self.parse_range_end() {
|
||||||
|
self.parse_pat_range_begin_with(const_expr, re)?
|
||||||
|
} else {
|
||||||
|
PatKind::Lit(const_expr)
|
||||||
|
}
|
||||||
} else if self.can_be_ident_pat() {
|
} else if self.can_be_ident_pat() {
|
||||||
// Parse `ident @ pat`
|
// Parse `ident @ pat`
|
||||||
// This can give false positives and parse nullary enums,
|
// This can give false positives and parse nullary enums,
|
||||||
|
@ -717,7 +723,8 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
/// Is the token `dist` away from the current suitable as the start of a range patterns end?
|
/// Is the token `dist` away from the current suitable as the start of a range patterns end?
|
||||||
fn is_pat_range_end_start(&self, dist: usize) -> bool {
|
fn is_pat_range_end_start(&self, dist: usize) -> bool {
|
||||||
self.look_ahead(dist, |t| {
|
self.check_inline_const(dist)
|
||||||
|
|| self.look_ahead(dist, |t| {
|
||||||
t.is_path_start() // e.g. `MY_CONST`;
|
t.is_path_start() // e.g. `MY_CONST`;
|
||||||
|| t.kind == token::Dot // e.g. `.5` for recovery;
|
|| t.kind == token::Dot // e.g. `.5` for recovery;
|
||||||
|| t.can_begin_literal_maybe_minus() // e.g. `42`.
|
|| t.can_begin_literal_maybe_minus() // e.g. `42`.
|
||||||
|
@ -726,7 +733,9 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
|
fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
|
||||||
if self.check_path() {
|
if self.check_inline_const(0) {
|
||||||
|
self.parse_const_block(self.token.span)
|
||||||
|
} else if self.check_path() {
|
||||||
let lo = self.token.span;
|
let lo = self.token.span;
|
||||||
let (qself, path) = if self.eat_lt() {
|
let (qself, path) = if self.eat_lt() {
|
||||||
// Parse a qualified path
|
// Parse a qualified path
|
||||||
|
|
38
src/test/ui/inline-const/const-match-pat-range.rs
Normal file
38
src/test/ui/inline-const/const-match-pat-range.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
// build-pass
|
||||||
|
|
||||||
|
#![allow(incomplete_features)]
|
||||||
|
#![feature(inline_const, half_open_range_patterns, exclusive_range_pattern)]
|
||||||
|
fn main() {
|
||||||
|
const N: u32 = 10;
|
||||||
|
let x: u32 = 3;
|
||||||
|
|
||||||
|
match x {
|
||||||
|
1 ..= const { N + 1 } => {},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
match x {
|
||||||
|
const { N - 1 } ..= 10 => {},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
match x {
|
||||||
|
const { N - 1 } ..= const { N + 1 } => {},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
match x {
|
||||||
|
.. const { N + 1 } => {},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
match x {
|
||||||
|
const { N - 1 } .. => {},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
match x {
|
||||||
|
..= const { N + 1 } => {},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,4 +13,4 @@
|
||||||
|
|
||||||
fn f() { |[](* }
|
fn f() { |[](* }
|
||||||
//~^ ERROR expected one of `,` or `:`, found `(`
|
//~^ ERROR expected one of `,` or `:`, found `(`
|
||||||
//~| ERROR expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `const`, `mut`, `ref`, `|`, identifier, or path, found `*`
|
//~| ERROR expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*`
|
||||||
|
|
|
@ -4,7 +4,7 @@ error: expected one of `,` or `:`, found `(`
|
||||||
LL | fn f() { |[](* }
|
LL | fn f() { |[](* }
|
||||||
| ^ expected one of `,` or `:`
|
| ^ expected one of `,` or `:`
|
||||||
|
|
||||||
error: expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `const`, `mut`, `ref`, `|`, identifier, or path, found `*`
|
error: expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*`
|
||||||
--> $DIR/issue-66357-unexpected-unreachable.rs:14:14
|
--> $DIR/issue-66357-unexpected-unreachable.rs:14:14
|
||||||
|
|
|
|
||||||
LL | fn f() { |[](* }
|
LL | fn f() { |[](* }
|
||||||
|
|
Loading…
Add table
Reference in a new issue