Skip to content

Commit 7feb592

Browse files
authored
Merge pull request swiftlang#74840 from drexin/wip-130781414
[IRGen] Properly convert between ptr and int in typed error mapping
2 parents 4edda08 + 22fec31 commit 7feb592

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,13 @@ CombinedResultAndErrorType irgen::combineResultAndTypedErrorType(
414414
assert(error->isIntOrPtrTy() &&
415415
"Direct errors must only consist of int or ptr values");
416416
result.errorValueMapping.push_back(combined.size());
417-
if (res->getPrimitiveSizeInBits() >= error->getPrimitiveSizeInBits()) {
417+
418+
if (res == error) {
418419
combined.push_back(res);
419420
} else {
420-
combined.push_back(error);
421+
auto maxSize = std::max(IGM.DataLayout.getTypeSizeInBits(res),
422+
IGM.DataLayout.getTypeSizeInBits(error));
423+
combined.push_back(llvm::IntegerType::get(IGM.getLLVMContext(), maxSize));
421424
}
422425

423426
++resIt;
@@ -2864,10 +2867,12 @@ class SyncCallEmission final : public CallEmission {
28642867
if (auto *structTy = dyn_cast<llvm::StructType>(
28652868
nativeSchema.getExpandedType(IGF.IGM))) {
28662869
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
2867-
resultExplosion.add(values[i]);
2870+
auto *nativeTy = structTy->getElementType(i);
2871+
resultExplosion.add(convertIfNecessary(nativeTy, values[i]));
28682872
}
28692873
} else {
2870-
resultExplosion.add(values[0]);
2874+
resultExplosion.add(
2875+
convertIfNecessary(combined.combinedTy, values[0]));
28712876
}
28722877
out = nativeSchema.mapFromNative(IGF.IGM, IGF, resultExplosion,
28732878
resultType);
@@ -5738,6 +5743,9 @@ void IRGenFunction::emitScalarReturn(SILType returnResultType,
57385743
eltTy->getPrimitiveSizeInBits()) {
57395744
assert(nativeTy->getPrimitiveSizeInBits() >
57405745
eltTy->getPrimitiveSizeInBits());
5746+
if (eltTy->isPointerTy()) {
5747+
return Builder.CreatePtrToInt(elt, nativeTy);
5748+
}
57415749
return Builder.CreateZExt(elt, nativeTy);
57425750
}
57435751
return elt;

lib/IRGen/IRGenSIL.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4460,6 +4460,11 @@ void IRGenSILFunction::visitThrowInst(swift::ThrowInst *i) {
44604460
eltTy->getPrimitiveSizeInBits()) {
44614461
assert(nativeTy->getPrimitiveSizeInBits() >
44624462
eltTy->getPrimitiveSizeInBits());
4463+
4464+
if (eltTy->isPointerTy()) {
4465+
return elt = Builder.CreatePtrToInt(elt, nativeTy);
4466+
}
4467+
44634468
return Builder.CreateZExt(elt, nativeTy);
44644469
}
44654470
return elt;

test/IRGen/typed_throws.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,19 @@ func catchesSmallError() -> Int {
111111
return error.x
112112
}
113113
}
114+
115+
struct MyError: Error {
116+
let x: AnyObject
117+
}
118+
119+
// CHECK: define hidden swiftcc { float, i64, float } @"$s12typed_throws8mayThrow1x1ySf_s5Int32VSftSb_yXltAA7MyErrorVYKF"
120+
// CHECK: [[CONVERTED:%.*]] = ptrtoint ptr {{%.*}} to i64
121+
// CEHCK: insertvalue { float, i64, float } undef, i64 [[CONVERTED]], 1
122+
// CHECK: }
123+
@inline(never)
124+
func mayThrow(x: Bool, y: AnyObject) throws(MyError) -> (Float, Int32, Float) {
125+
guard x else {
126+
throw MyError(x: y)
127+
}
128+
return (3.0, 4, 5.0)
129+
}

0 commit comments

Comments
 (0)