Factor out common pattern of trying to complete a type then falling back to an error type if that fails. (#3454)

As requested in [review of
#3450](https://github.com/carbon-language/carbon-lang/pull/3450#discussion_r1414220143).
This commit is contained in:
Richard Smith
2023-12-07 01:53:30 +00:00
committed by GitHub
parent fe6f7b4330
commit 18d7ba9542
4 changed files with 48 additions and 45 deletions
+10 -11
View File
@@ -62,17 +62,16 @@ static auto BuildFunctionDecl(Context& context, bool is_definition)
auto return_node_copy = return_node;
return_type_id = context.insts().Get(return_storage_id).type_id();
if (!context.TryToCompleteType(return_type_id, [&] {
CARBON_DIAGNOSTIC(IncompleteTypeInFunctionReturnType, Error,
"Function returns incomplete type `{0}`.",
std::string);
return context.emitter().Build(
return_node_copy, IncompleteTypeInFunctionReturnType,
context.sem_ir().StringifyType(return_type_id));
})) {
return_type_id = SemIR::TypeId::Error;
} else if (!SemIR::GetInitRepr(context.sem_ir(), return_type_id)
.has_return_slot()) {
return_type_id = context.AsCompleteType(return_type_id, [&] {
CARBON_DIAGNOSTIC(IncompleteTypeInFunctionReturnType, Error,
"Function returns incomplete type `{0}`.", std::string);
return context.emitter().Build(
return_node_copy, IncompleteTypeInFunctionReturnType,
context.sem_ir().StringifyType(return_type_id));
});
if (!SemIR::GetInitRepr(context.sem_ir(), return_type_id)
.has_return_slot()) {
// The function only has a return slot if it uses in-place initialization.
} else {
return_slot_id = return_storage_id;