Skip to content

Commit cfda992

Browse files
committed
[5.0] IRGen: Fix building a closure that captures a struct containing a reference using an indirect convention
A <{ %AClass*}> value cannot be bitcasted to a swift.refcounted*. rdar://46538967
1 parent b33997f commit cfda992

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/IRGen/GenFunc.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,12 @@ void irgen::emitFunctionPartialApplication(
14341434
llvm::Value *ctx = args.claimNext();
14351435
if (isIndirectFormalParameter(*singleRefcountedConvention))
14361436
ctx = IGF.Builder.CreateLoad(ctx, IGF.IGM.getPointerAlignment());
1437-
ctx = IGF.Builder.CreateBitCast(ctx, IGF.IGM.RefCountedPtrTy);
1437+
// We might get a struct containing a pointer e.g type <{ %AClass* }>
1438+
if (!ctx->getType()->isPointerTy())
1439+
ctx = IGF.coerceValue(ctx, IGF.IGM.RefCountedPtrTy,
1440+
IGF.IGM.DataLayout);
1441+
else
1442+
ctx = IGF.Builder.CreateBitCast(ctx, IGF.IGM.RefCountedPtrTy);
14381443
out.add(ctx);
14391444
return;
14401445
}

test/IRGen/partial_apply.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,28 @@ bb2(%r : $()):
524524
bb3(%v : $()):
525525
return %v : $()
526526
}
527+
528+
struct A1 {
529+
let b: () -> ()
530+
}
531+
532+
struct A2<T> {
533+
let a: T
534+
}
535+
536+
class A3 {}
537+
538+
sil @amethod : $@convention(method) (@in_guaranteed A2<A3>) -> (@owned A1, @error Error)
539+
540+
sil @repo : $@convention(thin) (@in_guaranteed A2<A3>) -> @owned @callee_guaranteed () -> (@owned A1, @error Error) {
541+
bb0(%0 : $*A2<A3>):
542+
%1 = load %0 : $*A2<A3>
543+
%2 = alloc_stack $A2<A3>
544+
store %1 to %2 : $*A2<A3>
545+
%4 = function_ref @amethod : $@convention(method) (@in_guaranteed A2<A3>) -> (@owned A1, @error Error)
546+
%5 = partial_apply [callee_guaranteed] %4(%2) : $@convention(method) (@in_guaranteed A2<A3>) -> (@owned A1, @error Error)
547+
dealloc_stack %2 : $*A2<A3>
548+
return %5 : $@callee_guaranteed () -> (@owned A1, @error Error)
549+
}
550+
551+
sil_vtable A3 {}

0 commit comments

Comments
 (0)