mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +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:
@@ -7,12 +7,6 @@
|
||||
|
||||
namespace Carbon::Parse {
|
||||
|
||||
static auto IsBindingPatternOperator(Lex::TokenKind kind) -> bool {
|
||||
return kind == Lex::TokenKind::Colon ||
|
||||
kind == Lex::TokenKind::ColonExclaim ||
|
||||
kind == Lex::TokenKind::ColonQuestion;
|
||||
}
|
||||
|
||||
auto HandlePattern(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
switch (context.PositionKind()) {
|
||||
@@ -37,19 +31,15 @@ auto HandlePattern(Context& context) -> void {
|
||||
state.in_var_pattern, state.in_unused_pattern,
|
||||
state.ambient_precedence);
|
||||
break;
|
||||
case Lex::TokenKind::Identifier:
|
||||
case Lex::TokenKind::SelfValueIdentifier:
|
||||
case Lex::TokenKind::Underscore: {
|
||||
if (IsBindingPatternOperator(
|
||||
context.PositionKind(Lookahead::NextToken))) {
|
||||
default:
|
||||
if (context.PositionKind().is_word() &&
|
||||
context.PositionKind(Lookahead::NextToken)
|
||||
.is_binding_pattern_operator()) {
|
||||
context.PushStateForPattern(
|
||||
StateKind::BindingPattern, state.in_var_pattern,
|
||||
state.in_unused_pattern, state.ambient_precedence);
|
||||
break;
|
||||
}
|
||||
[[fallthrough]];
|
||||
}
|
||||
default:
|
||||
context.PushState(StateKind::ExprPattern);
|
||||
context.PushStateForExpr(state.ambient_precedence);
|
||||
break;
|
||||
@@ -63,7 +53,7 @@ auto HandleExprPattern(Context& context) -> void {
|
||||
// have a malformed attempt to introduce a binding pattern that we interpreted
|
||||
// as an expression pattern, so diagnose that here rather than diagnosing a
|
||||
// missing `;` at an outer level.
|
||||
if (IsBindingPatternOperator(context.PositionKind())) {
|
||||
if (context.PositionKind().is_binding_pattern_operator()) {
|
||||
if (!state.has_error) {
|
||||
CARBON_DIAGNOSTIC(ExpectedBindingName, Error,
|
||||
"unexpected expression before {0} in binding pattern",
|
||||
|
||||
Reference in New Issue
Block a user