mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Improve diagnostics and error recovery for invalid identifiers. (#7249)
If a keyword or a sized type literal (eg, `f2`) is used in a context where we are confident that we are expecting an identifier -- either before a `:` in a binding pattern or after a `.` in a member access or designator -- then recover as if a raw identifier was used. This appears to be a particular stumbling block for coding agents, so seems worth paying special attention to. Add a mechanism to the tokenized buffer to track additional tokens synthesized for error recovery so that we can keep the lexed token sequence immutable and still satisfy the invariants throughout the rest of the toolchain for recovery tokens. Thanks to chandlerc for suggesting this approach!
This commit is contained in:
@@ -38,16 +38,23 @@ static auto HandlePeriodOrArrow(Context& context, NodeKind node_kind,
|
||||
context.PushState(StateKind::OnlyParenExpr);
|
||||
return;
|
||||
} else {
|
||||
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterPeriodOrArrow, Error,
|
||||
"expected identifier after `{0:->|.}`",
|
||||
Diagnostics::BoolAsSelect);
|
||||
bool recover_as_raw = context.PositionKind().is_word();
|
||||
CARBON_DIAGNOSTIC(
|
||||
ExpectedIdentifierAfterPeriodOrArrow, Error,
|
||||
"expected identifier after `{0:->|.}`"
|
||||
"{1:; prefix reserved word with `r#` to form a valid identifier|}",
|
||||
Diagnostics::BoolAsSelect, Diagnostics::BoolAsSelect);
|
||||
context.emitter().Emit(*context.position(),
|
||||
ExpectedIdentifierAfterPeriodOrArrow, is_arrow);
|
||||
// If we see a keyword, assume it was intended to be a name.
|
||||
// TODO: Should keywords be valid here?
|
||||
if (context.PositionKind().is_keyword()) {
|
||||
ExpectedIdentifierAfterPeriodOrArrow, is_arrow,
|
||||
recover_as_raw);
|
||||
// If we see a word, assume it was intended to be a name.
|
||||
// TODO: Should word tokens be valid here?
|
||||
if (recover_as_raw) {
|
||||
auto word_as_identifier =
|
||||
context.tokens().AddPostLexingRecoveryTokenAsIdentifier(
|
||||
context.Consume());
|
||||
context.AddLeafNode(NodeKind::IdentifierNameNotBeforeSignature,
|
||||
context.Consume(), /*has_error=*/true);
|
||||
word_as_identifier);
|
||||
} else {
|
||||
context.AddLeafNode(NodeKind::IdentifierNameNotBeforeSignature,
|
||||
*context.position(), /*has_error=*/true);
|
||||
|
||||
Reference in New Issue
Block a user