Skip to content

Commit 89d8b23

Browse files
authored
Merge pull request #69806 from DougGregor/indirect-errors-allocating-inits
[SILGen] Handle indirect error results in allocating class initializers
2 parents 9e68479 + 5043295 commit 89d8b23

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

lib/SILGen/SILGenConstructor.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,22 @@ void SILGenFunction::emitClassConstructorAllocator(ConstructorDecl *ctor) {
936936
// Forward the constructor arguments.
937937
// FIXME: Handle 'self' along with the other body patterns.
938938
SmallVector<SILValue, 8> args;
939+
940+
// If the function we're calling has an indirect error result, create an
941+
// argument for it.
942+
if (F.getConventions().hasIndirectSILErrorResults()) {
943+
assert(F.getConventions().getNumIndirectSILErrorResults() == 1);
944+
auto paramTy = F.mapTypeIntoContext(
945+
F.getConventions().getSILErrorType(getTypeExpansionContext()));
946+
auto inContextParamTy = F.getLoweredType(paramTy.getASTType())
947+
.getCategoryType(paramTy.getCategory());
948+
SILArgument *arg = F.begin()->createFunctionArgument(inContextParamTy);
949+
950+
IndirectErrorResult = arg;
951+
952+
args.push_back(arg);
953+
}
954+
939955
bindParametersForForwarding(ctor->getParameters(), args);
940956

941957
if (ctor->requiresUnavailableDeclABICompatibilityStubs())

lib/SILGen/SILGenPoly.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ void SILGenFunction::collectThunkParams(
897897
static llvm::Optional<SILValue>
898898
emitThunkIndirectErrorArgument(SILGenFunction &SGF, SILLocation loc,
899899
CanSILFunctionType fnType) {
900-
// If the function we're calling has as indirect error result, create an
900+
// If the function we're calling has an indirect error result, create an
901901
// argument for it.
902902
auto innerError = fnType->getOptionalErrorResult();
903903
if (!innerError || innerError->getConvention() != ResultConvention::Indirect)
@@ -4876,7 +4876,7 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
48764876
outputSubstType.getResult(),
48774877
fnType, thunkType);
48784878

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

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

6610-
// If the function we're calling has as indirect error result, create an
6610+
// If the function we're calling has an indirect error result, create an
66116611
// argument for it.
66126612
if (auto innerIndirectErrorAddr =
66136613
emitThunkIndirectErrorArgument(*this, loc, witnessFTy)) {

test/SILGen/typed_throws.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,15 @@ func throwsConcreteWithDoCatchTypedRethrow() throws(MyError) {
115115
// CHECK: [[DEFAULT_BB]]:
116116
// CHECK-NEXT: throw [[ERROR]] : $MyError
117117
}
118+
119+
open class MyClass {
120+
// 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)
121+
// CHECK: bb0(%0 : $*E, %1 : @guaranteed $@noescape @callee_guaranteed @substituted <τ_0_0> () -> @error_indirect τ_0_0 for <E>, %2 : $@thick MyClass.Type):
122+
// CHECK: try_apply{{.*}}, normal [[NORMAL_BB:bb[0-9]+]], error [[ERROR_BB:bb[0-9]+]]
123+
// CHECK: [[NORMAL_BB]]([[SELF:%.*]] : @owned $MyClass):
124+
// CHECK-NEXT: return [[SELF]] : $MyClass
125+
// CHECK: [[ERROR_BB]]:
126+
// CHECK-NEXT: throw_addr
127+
public init<E>(body: () throws(E) -> Void) throws(E) {
128+
}
129+
}

0 commit comments

Comments
 (0)