Diagnose non-regular patterns in struct pattern shorthand (#8173)

Previously these would produce an invalid parse tree, causing a crash.
This commit is contained in:
Geoff Romer authored and GitHub committed 2026-11-24 18:35:54 +00:00
1 parent 7f576a0415
commit ace4dbfaa4
3 files changed
+65

No files matched your search

+1
View File
@@ -471,6 +471,7 @@ CARBON_DIAGNOSTIC_KIND(IntWidthTooLarge)
CARBON_DIAGNOSTIC_KIND(InvalidArrayExpr)
CARBON_DIAGNOSTIC_KIND(NegativeIntInUnsignedType)
CARBON_DIAGNOSTIC_KIND(NonConstantCallToCompTimeOnlyFunction)
CARBON_DIAGNOSTIC_KIND(NonRegularBindingInStructPatternShorthand)
CARBON_DIAGNOSTIC_KIND(NonRegularBindingInVarDecl)
CARBON_DIAGNOSTIC_KIND(SelfOutsideParamList)
CARBON_DIAGNOSTIC_KIND(SelfInImplicitParamList)
@@ -299,6 +299,17 @@ static auto HandleBindingPatternFinish(Context& context, StateKind finish_kind)
state.has_error = true;
}
} else {
if (state.in_field_shorthand_pattern &&
finish_kind != StateKind::BindingPatternFinishAsRegular) {
CARBON_DIAGNOSTIC(
NonRegularBindingInStructPatternShorthand, Error,
"found {0:generic|`:?`} binding as shorthand struct pattern element",
Diagnostics::BoolAsSelect);
context.emitter().Emit(
*context.position(), NonRegularBindingInStructPatternShorthand,
finish_kind == StateKind::BindingPatternFinishAsGeneric);
state.has_error = true;
}
switch (finish_kind) {
case StateKind::BindingPatternFinishAsGeneric:
node_kind = NodeKind::CompileTimeBindingPattern;
+53
View File
@@ -392,6 +392,29 @@ let {ref _: i32} = {};
// CHECK:STDERR:
let {var _: i32} = {};
// --- fail_struct_pattern_shorthand_generic_binding.carbon
// CHECK:STDERR: ╭─┤ fail_struct_pattern_shorthand_generic_binding.carbon:[[@LINE+8]]:17
// CHECK:STDERR: -> │ let {generic x:y} = {};
// CHECK:STDERR: · ┳
// CHECK:STDERR: · ┗━━┫ error: found generic binding as shorthand struct pattern
// CHECK:STDERR: · ┃ element
// CHECK:STDERR: · ┃ [NonRegularBindingInStructPatternShorthand]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
let {generic x:y} = {};
// --- fail_struct_pattern_shorthand_form_binding.carbon
// CHECK:STDERR: ╭─┤ fail_struct_pattern_shorthand_form_binding.carbon:[[@LINE+7]]:10
// CHECK:STDERR: -> │ let {x:?y} = {};
// CHECK:STDERR: · ┳
// CHECK:STDERR: · ┗━━┫ error: found `:?` binding as shorthand struct pattern element
// CHECK:STDERR: · ┃ [NonRegularBindingInStructPatternShorthand]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
let {x:?y} = {};
// CHECK:STDOUT: - filename: struct_pattern_empty.carbon
// CHECK:STDOUT: ╭─FileStart ''
// CHECK:STDOUT: │ ╭─LetIntroducer 'let'
@@ -1235,3 +1258,33 @@ let {var _: i32} = {};
// CHECK:STDOUT: ├─LetDecl ';'
// CHECK:STDOUT: ├─FileEnd ''
// CHECK:STDOUT: (root)
// CHECK:STDOUT: - filename: fail_struct_pattern_shorthand_generic_binding.carbon
// CHECK:STDOUT: ╭─FileStart ''
// CHECK:STDOUT: │ ╭─LetIntroducer 'let'
// CHECK:STDOUT: │ │ ╭─StructPatternStart '{'
// CHECK:STDOUT: │ │ │ ╭─IdentifierNameNotBeforeSignature 'x'
// CHECK:STDOUT: │ │ │ ├─CompileTimeBindingPatternTypeStart ':'
// CHECK:STDOUT: │ │ │ ├─IdentifierNameExpr 'y'
// CHECK:STDOUT: │ │ ├─CompileTimeBindingPattern ':' has_error
// CHECK:STDOUT: │ ├─StructPattern '}' has_error
// CHECK:STDOUT: │ ├─LetInitializer '='
// CHECK:STDOUT: │ │ ╭─StructLiteralStart '{'
// CHECK:STDOUT: │ ├─StructLiteral '}'
// CHECK:STDOUT: ├─LetDecl ';'
// CHECK:STDOUT: ├─FileEnd ''
// CHECK:STDOUT: (root)
// CHECK:STDOUT: - filename: fail_struct_pattern_shorthand_form_binding.carbon
// CHECK:STDOUT: ╭─FileStart ''
// CHECK:STDOUT: │ ╭─LetIntroducer 'let'
// CHECK:STDOUT: │ │ ╭─StructPatternStart '{'
// CHECK:STDOUT: │ │ │ ╭─IdentifierNameNotBeforeSignature 'x'
// CHECK:STDOUT: │ │ │ ├─BindingPatternTypeStart ':?'
// CHECK:STDOUT: │ │ │ ├─IdentifierNameExpr 'y'
// CHECK:STDOUT: │ │ ├─FormBindingPattern ':?' has_error
// CHECK:STDOUT: │ ├─StructPattern '}' has_error
// CHECK:STDOUT: │ ├─LetInitializer '='
// CHECK:STDOUT: │ │ ╭─StructLiteralStart '{'
// CHECK:STDOUT: │ ├─StructLiteral '}'
// CHECK:STDOUT: ├─LetDecl ';'
// CHECK:STDOUT: ├─FileEnd ''
// CHECK:STDOUT: (root)