From c4d0c911615a7b4efecca47abab294c7c1b788ce Mon Sep 17 00:00:00 2001 From: Deep Majumder Date: Wed, 14 Dec 2022 19:10:03 +0530 Subject: [PATCH 1/2] Allow .. to be parsed as let initializer .. and ..= are valid expressions, however when used in a let statement it is not parsed. --- compiler/rustc_ast/src/token.rs | 4 ++++ compiler/rustc_parse/src/parser/expr.rs | 10 +++++++--- src/test/ui/parser/issue-105634.rs | 8 ++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/parser/issue-105634.rs diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index 7b8c0d79a17e8..5247007fdce98 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -379,6 +379,10 @@ impl Token { } } + pub fn is_range_seperator(&self) -> bool { + [DotDot, DotDotDot, DotDotEq].contains(&self.kind) + } + pub fn is_op(&self) -> bool { match self.kind { Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | BinOp(_) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 8a3545eb3be3a..7235b15a46be6 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -180,7 +180,7 @@ impl<'a> Parser<'a> { LhsExpr::AttributesParsed(attrs) => Some(attrs), _ => None, }; - if [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind) { + if self.token.is_range_seperator() { return self.parse_prefix_range_expr(attrs); } else { self.parse_prefix_expr(attrs)? @@ -512,7 +512,7 @@ impl<'a> Parser<'a> { } debug_assert!( - [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind), + self.token.is_range_seperator(), "parse_prefix_range_expr: token {:?} is not DotDot/DotDotEq", self.token ); @@ -896,7 +896,11 @@ impl<'a> Parser<'a> { let has_lifetime = self.token.is_lifetime() && self.look_ahead(1, |t| t != &token::Colon); let lifetime = has_lifetime.then(|| self.expect_lifetime()); // For recovery, see below. let (borrow_kind, mutbl) = self.parse_borrow_modifiers(lo); - let expr = self.parse_prefix_expr(None); + let expr = if self.token.is_range_seperator() { + self.parse_prefix_range_expr(None) + } else { + self.parse_prefix_expr(None) + }; let (hi, expr) = self.interpolated_or_expr_span(expr)?; let span = lo.to(hi); if let Some(lt) = lifetime { diff --git a/src/test/ui/parser/issue-105634.rs b/src/test/ui/parser/issue-105634.rs new file mode 100644 index 0000000000000..7a094d8e4b4b4 --- /dev/null +++ b/src/test/ui/parser/issue-105634.rs @@ -0,0 +1,8 @@ +// run-pass + +fn main() { + let _a = ..; + let _b = ..=10; + let _c = &..; + let _d = &..=10; +} From 4af9e6a1c203b6459aea23aed05924fcf2e12c58 Mon Sep 17 00:00:00 2001 From: Deep Majumder Date: Fri, 16 Dec 2022 18:41:08 +0530 Subject: [PATCH 2/2] Little fixes --- compiler/rustc_ast/src/token.rs | 2 +- compiler/rustc_parse/src/parser/expr.rs | 6 +++--- src/test/ui/parser/issue-105634.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index 5247007fdce98..5b6cf30fa96e3 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -379,7 +379,7 @@ impl Token { } } - pub fn is_range_seperator(&self) -> bool { + pub fn is_range_separator(&self) -> bool { [DotDot, DotDotDot, DotDotEq].contains(&self.kind) } diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 7235b15a46be6..40100363c5037 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -180,7 +180,7 @@ impl<'a> Parser<'a> { LhsExpr::AttributesParsed(attrs) => Some(attrs), _ => None, }; - if self.token.is_range_seperator() { + if self.token.is_range_separator() { return self.parse_prefix_range_expr(attrs); } else { self.parse_prefix_expr(attrs)? @@ -512,7 +512,7 @@ impl<'a> Parser<'a> { } debug_assert!( - self.token.is_range_seperator(), + self.token.is_range_separator(), "parse_prefix_range_expr: token {:?} is not DotDot/DotDotEq", self.token ); @@ -896,7 +896,7 @@ impl<'a> Parser<'a> { let has_lifetime = self.token.is_lifetime() && self.look_ahead(1, |t| t != &token::Colon); let lifetime = has_lifetime.then(|| self.expect_lifetime()); // For recovery, see below. let (borrow_kind, mutbl) = self.parse_borrow_modifiers(lo); - let expr = if self.token.is_range_seperator() { + let expr = if self.token.is_range_separator() { self.parse_prefix_range_expr(None) } else { self.parse_prefix_expr(None) diff --git a/src/test/ui/parser/issue-105634.rs b/src/test/ui/parser/issue-105634.rs index 7a094d8e4b4b4..579aa6e5bfb1b 100644 --- a/src/test/ui/parser/issue-105634.rs +++ b/src/test/ui/parser/issue-105634.rs @@ -1,4 +1,4 @@ -// run-pass +// check-pass fn main() { let _a = ..;