Skip to content

Commit c9ddb73

Browse files
committed
refactor: refactor identifier parsing somewhat
1 parent 85123d2 commit c9ddb73

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ parse_expected_identifier_found_reserved_keyword = expected identifier, found re
336336
parse_expected_identifier_found_doc_comment = expected identifier, found doc comment
337337
parse_expected_identifier = expected identifier
338338
339-
parse_sugg_escape_to_use_as_identifier = escape `{$ident_name}` to use it as an identifier
339+
parse_sugg_escape_identifier = escape `{$ident_name}` to use it as an identifier
340340
341341
parse_sugg_remove_comma = remove this comma
342342

compiler/rustc_parse/src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,12 +888,12 @@ pub(crate) struct InvalidMetaItem {
888888

889889
#[derive(Subdiagnostic)]
890890
#[suggestion(
891-
parse_sugg_escape_to_use_as_identifier,
891+
parse_sugg_escape_identifier,
892892
style = "verbose",
893893
applicability = "maybe-incorrect",
894894
code = "r#"
895895
)]
896-
pub(crate) struct SuggEscapeToUseAsIdentifier {
896+
pub(crate) struct SuggEscapeIdentifier {
897897
#[primary_span]
898898
pub span: Span,
899899
pub ident_name: String,
@@ -937,7 +937,7 @@ impl ExpectedIdentifierFound {
937937
pub(crate) struct ExpectedIdentifier {
938938
pub span: Span,
939939
pub token: Token,
940-
pub suggest_raw: Option<SuggEscapeToUseAsIdentifier>,
940+
pub suggest_raw: Option<SuggEscapeIdentifier>,
941941
pub suggest_remove_comma: Option<SuggRemoveComma>,
942942
pub help_cannot_start_number: Option<HelpIdentifierStartsWithNumber>,
943943
}

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use super::{
66
use crate::errors::{
77
AmbiguousPlus, AttributeOnParamType, BadQPathStage2, BadTypePlus, BadTypePlusSub,
88
ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg,
9-
ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentOnParamType,
10-
DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
9+
ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything,
10+
DocCommentOnParamType, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
1111
GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
1212
HelpIdentifierStartsWithNumber, InInTypo, IncorrectAwait, IncorrectSemicolon,
1313
IncorrectUseOfAwait, ParenthesesInForHead, ParenthesesInForHeadSugg,
1414
PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst,
1515
StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg, StructLiteralNeedingParens,
16-
StructLiteralNeedingParensSugg, SuggEscapeToUseAsIdentifier, SuggRemoveComma,
16+
StructLiteralNeedingParensSugg, SuggEscapeIdentifier, SuggRemoveComma,
1717
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
1818
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead,
1919
};
@@ -268,7 +268,16 @@ impl<'a> Parser<'a> {
268268
self.sess.source_map().span_to_snippet(span)
269269
}
270270

271+
/// Emits an error with suggestions if an identifier was expected but not found.
271272
pub(super) fn expected_ident_found(&mut self) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
273+
if let TokenKind::DocComment(..) = self.prev_token.kind {
274+
return DocCommentDoesNotDocumentAnything {
275+
span: self.prev_token.span,
276+
missing_comma: None,
277+
}
278+
.into_diagnostic(&self.sess.span_diagnostic);
279+
}
280+
272281
let valid_follow = &[
273282
TokenKind::Eq,
274283
TokenKind::Colon,
@@ -286,7 +295,7 @@ impl<'a> Parser<'a> {
286295
if ident.is_raw_guess()
287296
&& self.look_ahead(1, |t| valid_follow.contains(&t.kind)) =>
288297
{
289-
Some(SuggEscapeToUseAsIdentifier {
298+
Some(SuggEscapeIdentifier {
290299
span: ident.span.shrink_to_lo(),
291300
// `Symbol::to_string()` is different from `Symbol::into_diagnostic_arg()`,
292301
// which uses `Symbol::to_ident_string()` and "helpfully" adds an implicit `r#`

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ impl<'a> Parser<'a> {
17441744
/// Parses a field identifier. Specialized version of `parse_ident_common`
17451745
/// for better diagnostics and suggestions.
17461746
fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
1747-
let (ident, is_raw) = self.ident_or_err()?;
1747+
let (ident, is_raw) = self.ident_or_err(true)?;
17481748
if !is_raw && ident.is_reserved() {
17491749
let snapshot = self.create_snapshot_for_diagnostic();
17501750
let err = if self.check_fn_front_matter(false, Case::Sensitive) {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ use thin_vec::ThinVec;
4242
use tracing::debug;
4343

4444
use crate::errors::{
45-
DocCommentDoesNotDocumentAnything, IncorrectVisibilityRestriction, MismatchedClosingDelimiter,
46-
NonStringAbiLiteral,
45+
IncorrectVisibilityRestriction, MismatchedClosingDelimiter, NonStringAbiLiteral,
4746
};
4847

4948
bitflags::bitflags! {
@@ -552,19 +551,8 @@ impl<'a> Parser<'a> {
552551
self.parse_ident_common(true)
553552
}
554553

555-
fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> {
556-
self.token.ident().ok_or_else(|| match self.prev_token.kind {
557-
TokenKind::DocComment(..) => DocCommentDoesNotDocumentAnything {
558-
span: self.prev_token.span,
559-
missing_comma: None,
560-
}
561-
.into_diagnostic(&self.sess.span_diagnostic),
562-
_ => self.expected_ident_found(),
563-
})
564-
}
565-
566554
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
567-
let (ident, is_raw) = self.ident_or_err()?;
555+
let (ident, is_raw) = self.ident_or_err(recover)?;
568556
if !is_raw && ident.is_reserved() {
569557
let mut err = self.expected_ident_found();
570558
if recover {
@@ -577,6 +565,17 @@ impl<'a> Parser<'a> {
577565
Ok(ident)
578566
}
579567

568+
fn ident_or_err(&mut self, _recover: bool) -> PResult<'a, (Ident, /* is_raw */ bool)> {
569+
let result = self.token.ident().ok_or_else(|| self.expected_ident_found());
570+
571+
let (ident, is_raw) = match result {
572+
Ok(ident) => ident,
573+
Err(err) => return Err(err),
574+
};
575+
576+
Ok((ident, is_raw))
577+
}
578+
580579
/// Checks if the next token is `tok`, and returns `true` if so.
581580
///
582581
/// This method will automatically add `tok` to `expected_tokens` if `tok` is not

0 commit comments

Comments
 (0)