Skip to content

SILGen: Establish an addressability scope for closure captures. #81878

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
Jun 2, 2025
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
2 changes: 2 additions & 0 deletions lib/SILGen/SILGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,8 @@ SILGenFunction::getLocalVariableAddressableBuffer(VarDecl *decl,
SILValue reabstraction, allocStack, storeBorrow;
{
SavedInsertionPointRAII save(B);
ASSERT(AddressableBuffers.find(decl) != AddressableBuffers.end()
&& "local variable did not have an addressability scope set");
auto insertPoint = AddressableBuffers[decl].insertPoint;
B.setInsertionPoint(insertPoint);
auto allocStackTy = fullyAbstractedTy;
Expand Down
4 changes: 3 additions & 1 deletion lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction

PreparedAddressableBuffer(SILInstruction *insertPoint)
: insertPoint(insertPoint)
{}
{
ASSERT(insertPoint && "null insertion point provided");
}

PreparedAddressableBuffer(PreparedAddressableBuffer &&other)
: insertPoint(other.insertPoint)
Expand Down
1 change: 1 addition & 0 deletions lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,7 @@ static void emitCaptureArguments(SILGenFunction &SGF,
}

SGF.VarLocs[VD] = SILGenFunction::VarLoc(arg, enforcement, box);
SGF.enterLocalVariableAddressableBufferScope(VD);
SILDebugVariable DbgVar(VD->isLet(), ArgNo);
if (auto *AllocStack = dyn_cast<AllocStackInst>(arg)) {
AllocStack->setArgNo(ArgNo);
Expand Down
23 changes: 23 additions & 0 deletions test/SILGen/dependency_through_closure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-swift-emit-silgen -verify -enable-experimental-feature LifetimeDependence -enable-experimental-feature AddressableTypes %s

// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_AddressableTypes

@_addressableForDependencies
struct Owner {
@lifetime(borrow self)
func reference() -> Reference { fatalError() }
}

struct Reference: ~Escapable {
@lifetime(immortal)
init() { fatalError() }

func use() {}
}

func closure(_: () -> Void) {}

func dependencyThroughClosure(from owner: borrowing Owner) {
closure { owner.reference().use() }
}
3 changes: 0 additions & 3 deletions test/Sema/Inputs/lifetime_depend_infer.swiftinterface
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,12 @@ public struct NoncopyableSelfAccessors : ~Copyable & ~Escapable {
_modify
}

// FIXME: rdar://150073405 ([SILGen] support synthesized _modify on top of borrowed getters with library evolution)
/*
public var neComputedBorrow: lifetime_depend_infer.NE {
@lifetime(borrow self)
get
@lifetime(&self)
set
}
*/

public var neYieldedBorrow: lifetime_depend_infer.NE {
@lifetime(borrow self)
Expand Down