Rollup merge of #106919 - compiler-errors:underscore-typo-in-field-pat, r=jackh726

Recover `_` as `..` in field pattern
This commit is contained in:
Matthias Krüger 2023-02-02 17:14:05 +01:00 committed by GitHub
commit 8f47954742
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 34 deletions

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use rustc_ast::token::Token;
use rustc_ast::{Path, Visibility};
use rustc_errors::{fluent, AddToDiagnostic, Applicability, EmissionGuarantee, IntoDiagnostic};
@ -1802,8 +1804,9 @@ pub(crate) struct EnumPatternInsteadOfIdentifier {
#[diag(parse_dot_dot_dot_for_remaining_fields)]
pub(crate) struct DotDotDotForRemainingFields {
#[primary_span]
#[suggestion(code = "..", applicability = "machine-applicable")]
#[suggestion(code = "..", style = "verbose", applicability = "machine-applicable")]
pub span: Span,
pub token_str: Cow<'static, str>,
}
#[derive(Diagnostic)]

View file

@ -962,12 +962,15 @@ impl<'a> Parser<'a> {
}
ate_comma = false;
if self.check(&token::DotDot) || self.token == token::DotDotDot {
if self.check(&token::DotDot)
|| self.check_noexpect(&token::DotDotDot)
|| self.check_keyword(kw::Underscore)
{
etc = true;
let mut etc_sp = self.token.span;
self.recover_one_fewer_dotdot();
self.bump(); // `..` || `...`
self.recover_bad_dot_dot();
self.bump(); // `..` || `...` || `_`
if self.token == token::CloseDelim(Delimiter::Brace) {
etc_span = Some(etc_sp);
@ -1060,14 +1063,15 @@ impl<'a> Parser<'a> {
Ok((fields, etc))
}
/// Recover on `...` as if it were `..` to avoid further errors.
/// Recover on `...` or `_` as if it were `..` to avoid further errors.
/// See issue #46718.
fn recover_one_fewer_dotdot(&self) {
if self.token != token::DotDotDot {
fn recover_bad_dot_dot(&self) {
if self.token == token::DotDot {
return;
}
self.sess.emit_err(DotDotDotForRemainingFields { span: self.token.span });
let token_str = pprust::token_to_string(&self.token);
self.sess.emit_err(DotDotDotForRemainingFields { span: self.token.span, token_str });
}
fn parse_pat_field(&mut self, lo: Span, attrs: AttrVec) -> PResult<'a, PatField> {