From 992b914b6bf41aae4212ea4e8ee09f8b16a31f28 Mon Sep 17 00:00:00 2001 From: Ömer Sinan Ağacan Date: Sat, 27 Feb 2021 13:37:50 +0300 Subject: Recover from X when parsing const expr This adds recovery when in array type syntax user writes [X; Y] instead of [X; Y::] Fixes #82566 Note that whenever we parse an expression and know that the next token cannot be `,`, we should be calling check_mistyped_turbofish_with_multiple_type_params for this recovery. Previously we only did this for statement parsing (e.g. `let x = f;`). We now also do it when parsing the length field in array type syntax. --- compiler/rustc_parse/src/parser/ty.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_parse/src') diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 9553f5d09e8..8f03bfd4c3a 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -360,12 +360,20 @@ impl<'a> Parser<'a> { } Err(err) => return Err(err), }; + let ty = if self.eat(&token::Semi) { - TyKind::Array(elt_ty, self.parse_anon_const_expr()?) + let mut length = self.parse_anon_const_expr()?; + if let Err(e) = self.expect(&token::CloseDelim(token::Bracket)) { + // Try to recover from `X` when `X::` works + self.check_mistyped_turbofish_with_multiple_type_params(e, &mut length.value)?; + self.expect(&token::CloseDelim(token::Bracket))?; + } + TyKind::Array(elt_ty, length) } else { + self.expect(&token::CloseDelim(token::Bracket))?; TyKind::Slice(elt_ty) }; - self.expect(&token::CloseDelim(token::Bracket))?; + Ok(ty) } -- cgit 1.4.1-3-g733a5