Reconcile function default values between decl and def (#7665)

Updates the pattern matching code to support unspecified default values.
Adds logic to decl and def merge code to diagnose mismatches in defaults
if specified in both places, or if let entirely unspecified.

Per https://github.com/carbon-language/carbon-lang/pull/7521.
This commit is contained in:
Lucile Rose Nihlen
2026-09-08 20:41:58 +00:00
committed by GitHub
parent 5a07a14fe9
commit ca9e985fa8
30 changed files with 4624 additions and 3875 deletions
+13 -7
View File
@@ -154,7 +154,12 @@ auto HandleParseNode(Context& context, Parse::PatternListCommaId /*node_id*/)
auto HandleParseNode(Context& context, Parse::DefaultValueUnspecifiedId node_id)
-> bool {
return context.TODO(node_id, "pattern default values");
context.node_stack().Push(
node_id, AddInst<SemIR::UnspecifiedValue>(
context, node_id,
{.type_id = GetSingletonType(
context, SemIR::UnspecifiedValueType::TypeInstId)}));
return true;
}
auto HandleParseNode(Context& context,
@@ -181,23 +186,24 @@ auto HandleParseNode(Context& context, Parse::DefaultValuePatternId node_id)
return false;
}
// Evaluate the default value constant.
auto expr_const_id = TryEvalInst(context, expr_inst_id);
if (expr_const_id == SemIR::ConstantId::NotConstant) {
CARBON_DIAGNOSTIC(PatternDefaultValueNotConstant, Error,
"default value for pattern must be constant");
context.emitter().Emit(LocIdForDiagnostics(expr_node_id),
PatternDefaultValueNotConstant);
context.emitter().Emit(
LocIdForDiagnostics(context.insts().GetCanonicalLocId(expr_inst_id)),
PatternDefaultValueNotConstant);
return false;
}
// Look up the instruction associated with the evaluated constant.
auto constant_inst_id = context.constant_values().GetInstId(expr_const_id);
CARBON_CHECK(constant_inst_id != SemIR::InstId::None);
auto value_inst_id = context.constant_values().GetInstId(expr_const_id);
CARBON_CHECK(value_inst_id.has_value());
// Add the value to the default values array in the full pattern stack, for
// recovery later in the NameComponent.
auto default_value_id =
context.full_pattern_stack().AddDefaultValue(constant_inst_id);
context.full_pattern_stack().AddDefaultValue(value_inst_id);
// Next on the node stack should be the pattern for which this default was
// specified. We pop that so we can issue the DefaultValuePattern in its