diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index aa72068387b30..e414e10e84169 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1278,10 +1278,14 @@ impl<'a> Parser<'a> { None }; let open_paren = self.token.span; + let call_depth = self.token_cursor.stack.len(); let seq = match self.parse_expr_paren_seq() { Ok(args) => Ok(self.mk_expr(lo.to(self.prev_token.span), self.mk_call(fun, args))), - Err(err) if self.is_expected_raw_ref_mut() => { + Err(err) + if self.is_expected_raw_ref_mut() + && self.token_cursor.stack.len() == call_depth => + { let guar = err.emit(); // Preserve the call expression so later passes can still diagnose the callee, // while treating the malformed `&raw ` argument as an error expression. @@ -1299,9 +1303,8 @@ impl<'a> Parser<'a> { fn recover_raw_ref_call_args(&mut self, guar: ErrorGuaranteed) -> ThinVec> { let err_span = self.prev_token.span.to(self.token.span); let mut args = thin_vec![self.mk_expr_err(err_span, guar)]; - while self.token != token::Eof && self.token != token::CloseParen { - if self.eat(exp!(Comma)) && self.token != token::Eof && self.token != token::CloseParen - { + while !self.token.kind.is_close_delim_or_eof() { + if self.eat(exp!(Comma)) && !self.token.kind.is_close_delim_or_eof() { args.push(self.mk_expr_err(self.prev_token.span.shrink_to_hi(), guar)); } else { self.parse_token_tree(); diff --git a/tests/ui/parser/recover/raw-no-const-mut-nested-call-arg.rs b/tests/ui/parser/recover/raw-no-const-mut-nested-call-arg.rs new file mode 100644 index 0000000000000..061a690ec30f6 --- /dev/null +++ b/tests/ui/parser/recover/raw-no-const-mut-nested-call-arg.rs @@ -0,0 +1,6 @@ +// Regression test for https://github.com/rust-lang/rust/issues/157853. + +fn main() { + Test([&raw 2]) + //~^ ERROR expected one of +} diff --git a/tests/ui/parser/recover/raw-no-const-mut-nested-call-arg.stderr b/tests/ui/parser/recover/raw-no-const-mut-nested-call-arg.stderr new file mode 100644 index 0000000000000..5beeae63356e3 --- /dev/null +++ b/tests/ui/parser/recover/raw-no-const-mut-nested-call-arg.stderr @@ -0,0 +1,15 @@ +error: expected one of `!`, `,`, `.`, `::`, `;`, `?`, `]`, `const`, `mut`, `{`, or an operator, found `2` + --> $DIR/raw-no-const-mut-nested-call-arg.rs:4:16 + | +LL | Test([&raw 2]) + | ^ expected one of 11 possible tokens + | +help: `&raw` must be followed by `const` or `mut` to be a raw reference expression + | +LL | Test([&raw const 2]) + | +++++ +LL | Test([&raw mut 2]) + | +++ + +error: aborting due to 1 previous error +