Skip to content
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
19 changes: 14 additions & 5 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,13 @@ CombinedResultAndErrorType irgen::combineResultAndTypedErrorType(
assert(error->isIntOrPtrTy() &&
"Direct errors must only consist of int or ptr values");
result.errorValueMapping.push_back(combined.size());
if (res->getPrimitiveSizeInBits() >= error->getPrimitiveSizeInBits()) {

if (res == error) {
combined.push_back(res);
} else {
combined.push_back(error);
auto maxSize = std::max(IGM.DataLayout.getTypeSizeInBits(res),
IGM.DataLayout.getTypeSizeInBits(error));
combined.push_back(llvm::IntegerType::get(IGM.getLLVMContext(), maxSize));
}

++resIt;
Expand Down Expand Up @@ -2848,7 +2851,8 @@ class SyncCallEmission final : public CallEmission {
errorExplosion.add(elt);
}
} else {
errorExplosion.add(convertIfNecessary(combined.combinedTy, values[0]));
errorExplosion.add(convertIfNecessary(
combined.combinedTy, values[combined.errorValueMapping[0]]));
}

typedErrorExplosion =
Expand All @@ -2863,10 +2867,12 @@ class SyncCallEmission final : public CallEmission {
if (auto *structTy = dyn_cast<llvm::StructType>(
nativeSchema.getExpandedType(IGF.IGM))) {
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
resultExplosion.add(values[i]);
auto *nativeTy = structTy->getElementType(i);
resultExplosion.add(convertIfNecessary(nativeTy, values[i]));
}
} else {
resultExplosion.add(values[0]);
resultExplosion.add(
convertIfNecessary(combined.combinedTy, values[0]));
}
out = nativeSchema.mapFromNative(IGF.IGM, IGF, resultExplosion,
resultType);
Expand Down Expand Up @@ -5737,6 +5743,9 @@ void IRGenFunction::emitScalarReturn(SILType returnResultType,
eltTy->getPrimitiveSizeInBits()) {
assert(nativeTy->getPrimitiveSizeInBits() >
eltTy->getPrimitiveSizeInBits());
if (eltTy->isPointerTy()) {
return Builder.CreatePtrToInt(elt, nativeTy);
}
return Builder.CreateZExt(elt, nativeTy);
}
return elt;
Expand Down
5 changes: 5 additions & 0 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4460,6 +4460,11 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
eltTy->getPrimitiveSizeInBits()) {
assert(nativeTy->getPrimitiveSizeInBits() >
eltTy->getPrimitiveSizeInBits());

if (eltTy->isPointerTy()) {
return elt = Builder.CreatePtrToInt(elt, nativeTy);
}

return Builder.CreateZExt(elt, nativeTy);
}
return elt;
Expand Down
39 changes: 39 additions & 0 deletions test/IRGen/typed_throws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,42 @@ func testit() throws (S) {
public struct TypeH {
public var method: (Int) throws(MyBigError) -> String
}

struct SmallError: Error {
let x: Int
}

@inline(never)
func throwsSmallError() throws(SmallError) -> (Float, Int) {
throw SmallError(x: 1)
}

// CHECK: define hidden swiftcc i64 @"$s12typed_throws17catchesSmallErrorSiyF"()
// CHECK: [[RES:%.*]] = call swiftcc { float, i64 } @"$s12typed_throws0B10SmallErrorSf_SityAA0cD0VYKF"(ptr swiftself undef, ptr noalias nocapture swifterror dereferenceable(8) %swifterror)
// CHECK: [[R0:%.*]] = extractvalue { float, i64 } [[RES]], 0
// CHECK: [[R1:%.*]] = extractvalue { float, i64 } [[RES]], 1
// CHECK: phi i64 [ [[R1]], %typed.error.load ]
// CHECK: }
func catchesSmallError() -> Int {
do {
return try throwsSmallError().1
} catch {
return error.x
}
}

struct MyError: Error {
let x: AnyObject
}

// CHECK: define hidden swiftcc { float, i64, float } @"$s12typed_throws8mayThrow1x1ySf_s5Int32VSftSb_yXltAA7MyErrorVYKF"
// CHECK: [[CONVERTED:%.*]] = ptrtoint ptr {{%.*}} to i64
// CEHCK: insertvalue { float, i64, float } undef, i64 [[CONVERTED]], 1
// CHECK: }
@inline(never)
func mayThrow(x: Bool, y: AnyObject) throws(MyError) -> (Float, Int32, Float) {
guard x else {
throw MyError(x: y)
}
return (3.0, 4, 5.0)
}