From 83abed9df6ad0a8881270f166a02ffcaf437422f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 19 Oct 2020 18:44:37 -0300 Subject: [PATCH] Make inline const work for half open ranges --- compiler/rustc_parse/src/parser/expr.rs | 2 +- compiler/rustc_parse/src/parser/mod.rs | 6 +++--- compiler/rustc_parse/src/parser/pat.rs | 8 ++++---- src/test/ui/inline-const/const-match-pat-range.rs | 12 +++++++++++- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 51ecc53ca26..78c95428c72 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1062,7 +1062,7 @@ fn parse_bottom_expr(&mut self) -> PResult<'a, P> { }) } 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) diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 4999498e4f7..8ff97453c14 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -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, diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 8da0a2f57d8..27fe75a23b6 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -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) -> 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> { - 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; diff --git a/src/test/ui/inline-const/const-match-pat-range.rs b/src/test/ui/inline-const/const-match-pat-range.rs index 4fbccfaf200..c50c4f42848 100644 --- a/src/test/ui/inline-const/const-match-pat-range.rs +++ b/src/test/ui/inline-const/const-match-pat-range.rs @@ -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 } .. => {}, + _ => {}, + } }