Skip to content

SILGen: Fix ordering of key path application writebacks with other lvalue components. #18357

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
Jul 30, 2018
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
32 changes: 32 additions & 0 deletions lib/SILGen/SILGenLValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,38 @@ namespace {
// Mark the projected address's dependence on the owner.
projectedAddr = SGF.B.createMarkDependence(loc, projectedAddr,
projectedOwner.getValue());

// Push a cleanup to destroy the owner object. We don't want to leave
// it to release whenever because the key path implementation hangs
// the writeback operations specific to the traversal onto the destruction
// of the owner.
struct DestroyOwnerPseudoComponent : WritebackPseudoComponent {
ManagedValue owner;

DestroyOwnerPseudoComponent(const LValueTypeData &typeData,
ManagedValue owner)
: WritebackPseudoComponent(typeData), owner(owner)
{}

void writeback(SILGenFunction &SGF, SILLocation loc,
ManagedValue base,
MaterializedLValue materialized,
bool isFinal) override {
SGF.Cleanups.popAndEmitCleanup(owner.getCleanup(),
CleanupLocation::get(loc),
NotForUnwind);
}

void dump(raw_ostream &OS, unsigned indent) const override {
OS << "DestroyOwnerPseudoComponent";
}
};

std::unique_ptr<LogicalPathComponent> component(
new DestroyOwnerPseudoComponent(getTypeData(), projectedOwner));

pushWriteback(SGF, loc, std::move(component), base, MaterializedLValue());

return ManagedValue::forLValue(projectedAddr);
}

Expand Down
41 changes: 41 additions & 0 deletions test/SILGen/keypath_application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,44 @@ func partial<A>(valueA: A,
_ = valueB[keyPath: pkpB]
}

extension Int {
var b: Int { get { return 0 } set { } }
var u: Int { get { return 0 } set { } }
var tt: Int { get { return 0 } set { } }
}

// CHECK-LABEL: sil hidden @{{.*}}writebackNesting
func writebackNesting(x: inout Int,
y: WritableKeyPath<Int, Int>,
z: WritableKeyPath<Int, Int>,
w: Int) -> Int {
// -- get 'b'
// CHECK: function_ref @$SSi19keypath_applicationE1bSivg
// -- apply keypath y
// CHECK: [[PROJECT_FN:%.*]] = function_ref @{{.*}}_projectKeyPathWritable
// CHECK: [[PROJECT_RET:%.*]] = apply [[PROJECT_FN]]
// CHECK: [[PROJECT_RET_BORROW:%.*]] = begin_borrow [[PROJECT_RET]]
// CHECK: [[PROJECT_RET_BORROW_OWNER:%.*]] = tuple_extract [[PROJECT_RET_BORROW]] {{.*}}, 1
// CHECK: [[OWNER_Y:%.*]] = copy_value [[PROJECT_RET_BORROW_OWNER]]
// -- get 'u'
// CHECK: function_ref @$SSi19keypath_applicationE1uSivg
// -- apply keypath z
// CHECK: [[PROJECT_FN:%.*]] = function_ref @{{.*}}_projectKeyPathWritable
// CHECK: [[PROJECT_RET:%.*]] = apply [[PROJECT_FN]]
// CHECK: [[PROJECT_RET_BORROW:%.*]] = begin_borrow [[PROJECT_RET]]
// CHECK: [[PROJECT_RET_BORROW_OWNER:%.*]] = tuple_extract [[PROJECT_RET_BORROW]] {{.*}}, 1
// CHECK: [[OWNER_Z:%.*]] = copy_value [[PROJECT_RET_BORROW_OWNER]]

// -- set 'tt'
// CHECK: function_ref @$SSi19keypath_applicationE2ttSivs
// -- destroy owner for keypath projection z
// CHECK: destroy_value [[OWNER_Z]]
// -- set 'u'
// CHECK: function_ref @$SSi19keypath_applicationE1uSivs
// -- destroy owner for keypath projection y
// CHECK: destroy_value [[OWNER_Y]]
// -- set 'b'
// CHECK: function_ref @$SSi19keypath_applicationE1bSivs

x.b[keyPath: y].u[keyPath: z].tt = w
}