Skip to content

[SILGen] Handle indirect error results in allocating class initializers #69806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,22 @@ void SILGenFunction::emitClassConstructorAllocator(ConstructorDecl *ctor) {
// Forward the constructor arguments.
// FIXME: Handle 'self' along with the other body patterns.
SmallVector<SILValue, 8> args;

// If the function we're calling has an indirect error result, create an
// argument for it.
if (F.getConventions().hasIndirectSILErrorResults()) {
assert(F.getConventions().getNumIndirectSILErrorResults() == 1);
auto paramTy = F.mapTypeIntoContext(
F.getConventions().getSILErrorType(getTypeExpansionContext()));
auto inContextParamTy = F.getLoweredType(paramTy.getASTType())
.getCategoryType(paramTy.getCategory());
SILArgument *arg = F.begin()->createFunctionArgument(inContextParamTy);

IndirectErrorResult = arg;

args.push_back(arg);
}

bindParametersForForwarding(ctor->getParameters(), args);

if (ctor->requiresUnavailableDeclABICompatibilityStubs())
Expand Down
8 changes: 4 additions & 4 deletions lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ void SILGenFunction::collectThunkParams(
static llvm::Optional<SILValue>
emitThunkIndirectErrorArgument(SILGenFunction &SGF, SILLocation loc,
CanSILFunctionType fnType) {
// If the function we're calling has as indirect error result, create an
// If the function we're calling has an indirect error result, create an
// argument for it.
auto innerError = fnType->getOptionalErrorResult();
if (!innerError || innerError->getConvention() != ResultConvention::Indirect)
Expand Down Expand Up @@ -4876,7 +4876,7 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
outputSubstType.getResult(),
fnType, thunkType);

// If the function we're calling has as indirect error result, create an
// If the function we're calling has an indirect error result, create an
// argument for it.
if (auto innerIndirectErrorAddr =
emitThunkIndirectErrorArgument(SGF, loc, fnType)) {
Expand Down Expand Up @@ -6216,7 +6216,7 @@ SILGenFunction::emitVTableThunk(SILDeclRef base,
inputSubstType.getResult(),
derivedFTy, thunkTy);

// If the function we're calling has as indirect error result, create an
// If the function we're calling has an indirect error result, create an
// argument for it.
if (auto innerIndirectErrorAddr =
emitThunkIndirectErrorArgument(*this, loc, derivedFTy)) {
Expand Down Expand Up @@ -6607,7 +6607,7 @@ void SILGenFunction::emitProtocolWitness(
reqtSubstTy.getResult(),
witnessFTy, thunkTy);

// If the function we're calling has as indirect error result, create an
// If the function we're calling has an indirect error result, create an
// argument for it.
if (auto innerIndirectErrorAddr =
emitThunkIndirectErrorArgument(*this, loc, witnessFTy)) {
Expand Down
12 changes: 12 additions & 0 deletions test/SILGen/typed_throws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,15 @@ func throwsConcreteWithDoCatchTypedRethrow() throws(MyError) {
// CHECK: [[DEFAULT_BB]]:
// CHECK-NEXT: throw [[ERROR]] : $MyError
}

open class MyClass {
// CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s12typed_throws7MyClassC4bodyACyyxYKXE_txYKcs5ErrorRzlufC : $@convention(method) <E where E : Error> (@guaranteed @noescape @callee_guaranteed @substituted <τ_0_0> () -> @error_indirect τ_0_0 for <E>, @thick MyClass.Type) -> (@owned MyClass, @error_indirect E)
// CHECK: bb0(%0 : $*E, %1 : @guaranteed $@noescape @callee_guaranteed @substituted <τ_0_0> () -> @error_indirect τ_0_0 for <E>, %2 : $@thick MyClass.Type):
// CHECK: try_apply{{.*}}, normal [[NORMAL_BB:bb[0-9]+]], error [[ERROR_BB:bb[0-9]+]]
// CHECK: [[NORMAL_BB]]([[SELF:%.*]] : @owned $MyClass):
// CHECK-NEXT: return [[SELF]] : $MyClass
// CHECK: [[ERROR_BB]]:
// CHECK-NEXT: throw_addr
public init<E>(body: () throws(E) -> Void) throws(E) {
}
}