mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-02 22:02:51 +01:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user