allow trailing commas in a struct pattern after an _ field (#8074)

Following a leads decision
(https://github.com/carbon-language/carbon-lang/issues/7404), this PR
adds support for trailing commas in a struct pattern where it was
previously only allowed after a full field.
This commit is contained in:
oli-ej authored and GitHub committed 2026-11-03 22:02:33 +00:00
1 parent 80a4a4d005
commit 5ae3dcaa92
8 files changed
+78 -25

No files matched your search

+29 -10
View File
@@ -2,6 +2,7 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/diagnostics/format_providers.h"
#include "toolchain/parse/context.h"
#include "toolchain/parse/handle.h"
@@ -143,19 +144,37 @@ auto HandleStructPatternUnderscore(Context& context) -> void {
auto underscore = context.ConsumeChecked(Lex::TokenKind::Underscore);
bool is_last = context.PositionIs(Lex::TokenKind::CloseCurlyBrace);
if (!context.PositionIs(Lex::TokenKind::CloseCurlyBrace)) {
bool has_error = true;
bool has_comma = context.PositionIs(Lex::TokenKind::Comma);
if (!is_last) {
CARBON_DIAGNOSTIC(
ExpectedCloseAfterUnderscore, Error,
"unexpected token `{0}` after `_` in struct pattern, expected `}`",
Lex::TokenKind);
context.emitter().Emit(*context.position(), ExpectedCloseAfterUnderscore,
context.PositionKind());
state.has_error = true;
auto token = context.position();
auto token_kind = context.PositionKind();
if (has_comma) {
if (context.PositionIs(Lex::TokenKind::CloseCurlyBrace,
Lookahead::NextToken)) {
// We have a trailing `,` followed by `}`, so there is no error
has_error = false;
} else {
token++;
token_kind = context.PositionKind(Lookahead::NextToken);
}
}
if (has_error) {
CARBON_DIAGNOSTIC(ExpectedCloseAfterUnderscore, Error,
"unexpected token `{0}` after {1:`,`|`_`} in struct "
"pattern{1: with `_` field|}, expected `}`",
Lex::TokenKind, Diagnostics::BoolAsSelect);
context.emitter().Emit(*token, ExpectedCloseAfterUnderscore, token_kind,
has_comma);
state.has_error = true;
}
}
context.AddNode(NodeKind::UnderscoreName, underscore, state.has_error);
context.AddNode(NodeKind::PatternListUnderscoreField, underscore,
state.has_error);
if (state.has_error) {
context.ReturnErrorOnState();