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