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
+49
View File
@@ -2300,6 +2300,24 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
.query_specific_interface_id = query_specific_interface_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
SemIR::DefaultValuePattern inst)
-> ResolveResult {
auto subpattern = GetLocalImportRefInfo(resolver, inst.subpattern_id);
if (resolver.HasNewWork()) {
return ResolveResult::Retry();
}
return ResolveResult::Deduplicated<SemIR::DefaultValuePattern>(
resolver,
{
.type_id = resolver.local_types().GetTypeIdForTypeConstantId(
subpattern.local_type_const_id),
.subpattern_id = AddLoadedImportRef(resolver, subpattern),
.default_value_id = inst.default_value_id,
});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
SemIR::ExportDecl inst) -> ResolveResult {
auto value_id = GetLocalConstantId(resolver, inst.value_id);
@@ -2467,6 +2485,17 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
auto call_param_patterns = GetLocalBlockImportRefInfo(
resolver, import_function.call_param_patterns_id);
auto call_param_default_values = GetLocalBlockImportRefInfo(
resolver, import_function.call_param_default_values_id);
llvm::SmallVector<SemIR::InstId> imported_default_values;
if (call_param_default_values.has_value()) {
auto import_fn = [&resolver](const auto& import_info) {
return GetLocalConstantInstId(resolver, import_info.import_inst_id);
};
llvm::append_range(imported_default_values,
llvm::map_range(*call_param_default_values, import_fn));
}
auto return_type_const_id = SemIR::ConstantId::None;
if (import_function.return_type_inst_id.has_value()) {
return_type_const_id =
@@ -2516,6 +2545,10 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
// Add the function declaration.
new_function.call_param_patterns_id =
AddLoadedImportRefBlock(resolver, call_param_patterns);
if (call_param_default_values.has_value()) {
new_function.call_param_default_values_id =
resolver.local_inst_blocks().Add(imported_default_values);
}
new_function.parent_scope_id = parent_scope_id;
new_function.implicit_param_patterns_id =
AddLoadedImportRefBlock(resolver, implicit_param_patterns);
@@ -4340,6 +4373,16 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
.element_type_inst_id = elem_const_inst_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
SemIR::UnspecifiedValue inst) -> ResolveResult {
CARBON_CHECK(resolver.import_ir().types().Is<SemIR::UnspecifiedValueType>(
inst.type_id));
auto type_id = GetSingletonType(resolver.local_context(),
SemIR::UnspecifiedValueType::TypeInstId);
return ResolveResult::Deduplicated<SemIR::UnspecifiedValue>(
resolver, {.type_id = type_id});
}
template <typename VarPatternT>
requires SemIR::Internal::HasInstCategory<SemIR::AnyVarPattern, VarPatternT>
static auto TryResolveTypedInst(ImportRefResolver& resolver, VarPatternT inst)
@@ -4498,6 +4541,9 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
case CARBON_KIND(SemIR::CustomWitness inst): {
return TryResolveTypedInst(resolver, inst);
}
case CARBON_KIND(SemIR::DefaultValuePattern inst): {
return TryResolveTypedInst(resolver, inst);
}
case CARBON_KIND(SemIR::ExportDecl inst): {
return TryResolveTypedInst(resolver, inst);
}
@@ -4651,6 +4697,9 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
case CARBON_KIND(SemIR::UnboundElementType inst): {
return TryResolveTypedInst(resolver, inst);
}
case CARBON_KIND(SemIR::UnspecifiedValue inst): {
return TryResolveTypedInst(resolver, inst);
}
case CARBON_KIND(SemIR::ValueBindingPattern inst): {
return TryResolveTypedInst(resolver, inst);
}