Skip to content

Commit 8fca31e

Browse files
authored
Merge pull request #75150 from drexin/wip-130971168
[IRGen] Don't apply direct error return to functions with indirect result
2 parents 32af2f6 + e3c0bc8 commit 8fca31e

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ void SignatureExpansion::expandResult(
650650
const TypeInfo *directResultTypeInfo;
651651
std::tie(ResultIRType, directResultTypeInfo) = expandDirectResult();
652652

653-
if (!fnConv.hasIndirectSILErrorResults()) {
653+
if (!fnConv.hasIndirectSILResults() && !fnConv.hasIndirectSILErrorResults()) {
654654
llvm::Type *directErrorType;
655655
const TypeInfo *directErrorTypeInfo;
656656
std::tie(directErrorType, directErrorTypeInfo) = expandDirectErrorType();
@@ -2032,7 +2032,8 @@ void SignatureExpansion::expandParameters(
20322032
auto &errorTI = IGM.getTypeInfo(errorType);
20332033
auto &nativeError = errorTI.nativeReturnValueSchema(IGM);
20342034

2035-
if (getSILFuncConventions().hasIndirectSILErrorResults() ||
2035+
if (getSILFuncConventions().hasIndirectSILResults() ||
2036+
getSILFuncConventions().hasIndirectSILErrorResults() ||
20362037
native.requiresIndirect() ||
20372038
nativeError.shouldReturnTypedErrorIndirectly()) {
20382039
ParamIRTypes.push_back(IGM.getStorageType(errorType)->getPointerTo());
@@ -2595,7 +2596,8 @@ class SyncCallEmission final : public CallEmission {
25952596
auto &errorSchema =
25962597
IGF.IGM.getTypeInfo(silErrorTy).nativeReturnValueSchema(IGF.IGM);
25972598

2598-
if (nativeSchema.requiresIndirect() ||
2599+
if (fnConv.hasIndirectSILResults() ||
2600+
nativeSchema.requiresIndirect() ||
25992601
errorSchema.shouldReturnTypedErrorIndirectly()) {
26002602
// Return the error indirectly.
26012603
auto buf = IGF.getCalleeTypedErrorResultSlot(silErrorTy);
@@ -4364,8 +4366,9 @@ bool CallEmission::mayReturnTypedErrorDirectly() const {
43644366
SILFunctionConventions fnConv(getCallee().getOrigFunctionType(),
43654367
IGF.getSILModule());
43664368
bool mayReturnErrorDirectly = false;
4367-
if (!convertDirectToIndirectReturn && !fnConv.hasIndirectSILErrorResults() &&
4368-
fnConv.funcTy->hasErrorResult() && fnConv.isTypedError()) {
4369+
if (!convertDirectToIndirectReturn && !fnConv.hasIndirectSILResults() &&
4370+
!fnConv.hasIndirectSILErrorResults() && fnConv.funcTy->hasErrorResult() &&
4371+
fnConv.isTypedError()) {
43694372
auto errorType =
43704373
fnConv.getSILErrorType(IGF.IGM.getMaximalTypeExpansionContext());
43714374
auto &errorSchema =

lib/IRGen/IRGenSIL.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,8 @@ static void emitEntryPointArgumentsNativeCC(IRGenSILFunction &IGF,
21752175
auto &errorTI = cast<FixedTypeInfo>(IGF.getTypeInfo(inContextErrorType));
21762176
auto &native = resultTI.nativeReturnValueSchema(IGF.IGM);
21772177
auto &nativeError = errorTI.nativeReturnValueSchema(IGF.IGM);
2178-
if (funcTy->isAsync() || native.requiresIndirect() ||
2178+
if (funcTy->isAsync() || fnConv.hasIndirectSILResults() ||
2179+
native.requiresIndirect() ||
21792180
nativeError.shouldReturnTypedErrorIndirectly()) {
21802181
IGF.setCallerTypedErrorResultSlot(
21812182
Address(emission->getCallerTypedErrorResultArgument(),
@@ -3911,7 +3912,8 @@ void IRGenSILFunction::visitFullApplySite(FullApplySite site) {
39113912
auto &resultSchema = resultTI.nativeReturnValueSchema(IGM);
39123913
auto &errorSchema = errorTI.nativeReturnValueSchema(IGM);
39133914

3914-
if (isAsync() || substConv.hasIndirectSILErrorResults() ||
3915+
if (isAsync() || substConv.hasIndirectSILResults() ||
3916+
substConv.hasIndirectSILErrorResults() ||
39153917
resultSchema.requiresIndirect() ||
39163918
errorSchema.shouldReturnTypedErrorIndirectly()) {
39173919
Explosion errorValue;
@@ -4378,7 +4380,7 @@ static void emitReturnInst(IRGenSILFunction &IGF,
43784380
funcLang == SILFunctionLanguage::C && "Need to handle all cases");
43794381
SILType errorType;
43804382
if (fnType->hasErrorResult() && conv.isTypedError() &&
4381-
!conv.hasIndirectSILErrorResults()) {
4383+
!conv.hasIndirectSILResults() && !conv.hasIndirectSILErrorResults()) {
43824384
errorType =
43834385
conv.getSILErrorType(IGF.IGM.getMaximalTypeExpansionContext());
43844386
}
@@ -4433,7 +4435,8 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
44334435
auto &errorSchema = errorTI.nativeReturnValueSchema(IGM);
44344436

44354437
Builder.CreateStore(flag, getCallerErrorResultSlot());
4436-
if (resultSchema.requiresIndirect() ||
4438+
if (conv.hasIndirectSILResults() || conv.hasIndirectSILErrorResults() ||
4439+
resultSchema.requiresIndirect() ||
44374440
errorSchema.shouldReturnTypedErrorIndirectly()) {
44384441
errorTI.initialize(*this, errorResult, getCallerTypedErrorResultSlot(),
44394442
false);

test/IRGen/typed_throws.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,13 @@ func directErrorMergePtrAndInt(x: Bool, y: AnyObject) throws(SmallError) -> (Any
144144

145145
return try directErrorMergePtrAndInt(x: !x, y: y)
146146
}
147+
148+
// This used to crash at compile time, because it was trying to use a direct
149+
// error return in combination with an indirect result, which is illegal.
150+
func genericThrows<T>(x: Bool, y: T) throws(SmallError) -> T {
151+
guard x else {
152+
throw SmallError(x: 1)
153+
}
154+
155+
return y
156+
}

0 commit comments

Comments
 (0)