Skip to content

[IRGen] Properly extract values in visitThrowInst for empty error #75379

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 1 commit into from
Jul 20, 2024
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
10 changes: 8 additions & 2 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4371,6 +4371,7 @@ static void emitReturnInst(IRGenSILFunction &IGF,
if (fnType->hasErrorResult()) {
error.add(getNullErrorValue());
}

emitAsyncReturn(IGF, asyncLayout, funcResultType, fnType, result, error);
} else {
auto funcLang = IGF.CurSILFn->getLoweredFunctionType()->getLanguage();
Expand Down Expand Up @@ -4420,12 +4421,13 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
}

llvm::Value *expandedResult = llvm::UndefValue::get(combined.combinedTy);
auto *structTy = dyn_cast<llvm::StructType>(combined.combinedTy);

if (!errorSchema.getExpandedType(IGM)->isVoidTy()) {
auto nativeError =
errorSchema.mapIntoNative(IGM, *this, errorResult, silErrorTy, false);

if (auto *structTy = dyn_cast<llvm::StructType>(combined.combinedTy)) {
if (structTy) {
for (unsigned i : combined.errorValueMapping) {
llvm::Value *elt = nativeError.claimNext();
auto *nativeTy = structTy->getElementType(i);
Expand All @@ -4444,7 +4446,11 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
combined.combinedTy, /*forExtraction*/ false);
}
} else {
out = expandedResult;
if (forAsync && structTy) {
emitAllExtractValues(expandedResult, structTy, out);
} else {
out = expandedResult;
}
}
};

Expand Down
11 changes: 11 additions & 0 deletions test/IRGen/typed_throws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,14 @@ func mayThrowAsyncTiny(x: Bool) async throws(TinyError) -> Bool {
func callsMayThrowAsyncTiny(x: Bool) async {
_ = try! await mayThrowAsyncTiny(x: x)
}

struct EmptyError: Error {}

@available(SwiftStdlib 6.0, *)
func mayThrowEmptyErrorAsync(x: Bool) async throws(EmptyError) -> String? {
guard x else {
throw EmptyError()
}

return ""
}