Skip to content

Commit 5c7293b

Browse files
authored
Merge pull request #83381 from jckarter/addressable-capture-store-borrow-placement
SILGen: Fix positioning of store_borrow for addressable references to closure captures.
2 parents 48af998 + 42d1ca3 commit 5c7293b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/SILGen/SILGenDecl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2337,7 +2337,12 @@ SILGenFunction::getLocalVariableAddressableBuffer(VarDecl *decl,
23372337
} else if (auto inst = value->getDefiningInstruction()) {
23382338
B.setInsertionPoint(inst->getParent(), std::next(inst->getIterator()));
23392339
} else if (auto arg = dyn_cast<SILArgument>(value)) {
2340-
B.setInsertionPoint(arg->getParent()->begin());
2340+
if (auto farg = dyn_cast<SILFunctionArgument>(value);
2341+
farg && farg->isClosureCapture()) {
2342+
B.setInsertionPoint(allocStack->getNextInstruction());
2343+
} else {
2344+
B.setInsertionPoint(arg->getParent()->begin());
2345+
}
23412346
} else {
23422347
llvm_unreachable("unexpected value source!");
23432348
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %target-swift-emit-silgen -enable-experimental-feature AddressableParameters %s | %FileCheck %s
2+
// REQUIRES: swift_feature_AddressableParameters
3+
struct Foo {
4+
var x, y, z: Int
5+
6+
init() { fatalError() }
7+
8+
@_addressableSelf
9+
func foo() {}
10+
}
11+
12+
func block(_: (Int) -> ()) {}
13+
14+
func test() {
15+
let x = Foo()
16+
block { (_) in x.foo() }
17+
}
18+
// ensure the closure properly nests the store_borrow after the alloc_stack:
19+
// CHECK-LABEL: sil{{.*}} @${{.*}}4test{{.*}}U_ : $
20+
// CHECK: [[STACK:%.*]] = alloc_stack
21+
// CHECK-NEXT: store_borrow {{.*}} to [[STACK]]

0 commit comments

Comments
 (0)