Make inline const work for half open ranges

This commit is contained in:
Santiago Pastorino 2020-10-19 18:44:37 -03:00
parent f8842b9bac
commit 83abed9df6
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
4 changed files with 19 additions and 9 deletions

View File

@ -1062,7 +1062,7 @@ fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
})
} else if self.eat_keyword(kw::Unsafe) {
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_block(lo.to(self.token.span))
} else if self.is_do_catch_block() {
self.recover_do_catch(attrs)

View File

@ -522,9 +522,9 @@ fn check_const_arg(&mut self) -> bool {
self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const)
}
fn check_inline_const(&mut self) -> bool {
self.check_keyword(kw::Const)
&& self.look_ahead(1, |t| match t.kind {
fn check_inline_const(&self, dist: usize) -> bool {
self.is_keyword_ahead(dist, &[kw::Const])
&& self.look_ahead(dist + 1, |t| match t.kind {
token::Interpolated(ref nt) => matches!(**nt, token::NtBlock(..)),
token::OpenDelim(DelimToken::Brace) => true,
_ => false,

View File

@ -313,7 +313,7 @@ fn parse_pat_with_range_pat(
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() {
} else if self.check_inline_const(0) {
// Parse `const pat`
let const_expr = self.parse_const_block(lo.to(self.token.span))?;
@ -722,8 +722,8 @@ fn parse_pat_range_to(&mut self, mut re: Spanned<RangeEnd>) -> PResult<'a, PatKi
}
/// Is the token `dist` away from the current suitable as the start of a range patterns end?
fn is_pat_range_end_start(&mut self, dist: usize) -> bool {
self.check_inline_const()
fn is_pat_range_end_start(&self, dist: usize) -> bool {
self.check_inline_const(dist)
|| self.look_ahead(dist, |t| {
t.is_path_start() // e.g. `MY_CONST`;
|| t.kind == token::Dot // e.g. `.5` for recovery;
@ -733,7 +733,7 @@ fn is_pat_range_end_start(&mut self, dist: usize) -> bool {
}
fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
if self.check_inline_const() {
if self.check_inline_const(0) {
self.parse_const_block(self.token.span)
} else if self.check_path() {
let lo = self.token.span;

View File

@ -1,7 +1,7 @@
// build-pass
#![allow(incomplete_features)]
#![feature(inline_const)]
#![feature(inline_const, half_open_range_patterns, exclusive_range_pattern)]
fn main() {
const N: u32 = 10;
let x: u32 = 3;
@ -20,4 +20,14 @@ fn main() {
1 ..= const { N + 1 } => {},
_ => {},
}
match x {
.. const { N + 1 } => {},
_ => {},
}
match x {
const { N - 1 } .. => {},
_ => {},
}
}