Skip to content

Commit a3ff7fc

Browse files
committed
SILGen: Bug fix handling consuming/borrowing parameters and optional chaining.
Make sure we remove the `@moveOnly` marker while projecting the payload as expected for the (no longer non-implicitly-copyable) projected result. Fixes rdar://116127887.
1 parent 1c3b994 commit a3ff7fc

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4411,6 +4411,13 @@ LValue SILGenLValue::visitBindOptionalExpr(BindOptionalExpr *e,
44114411

44124412
if (optBase.getType().isMoveOnly()) {
44134413
if (optBase.getType().isAddress()) {
4414+
// Strip the move-only wrapper if any.
4415+
if (optBase.getType().isMoveOnlyWrapped()) {
4416+
optBase = ManagedValue::forBorrowedAddressRValue(
4417+
SGF.B.createMoveOnlyWrapperToCopyableAddr(e,
4418+
optBase.getValue()));
4419+
}
4420+
44144421
optBase = enterAccessScope(SGF, e, ManagedValue(),
44154422
optBase, optTypeData,
44164423
baseAccessKind,
@@ -4423,13 +4430,26 @@ LValue SILGenLValue::visitBindOptionalExpr(BindOptionalExpr *e,
44234430
} else {
44244431
optBase = SGF.B.createFormalAccessBeginBorrow(e, optBase, IsNotLexical,
44254432
BeginBorrowInst::IsFixed);
4433+
// Strip the move-only wrapper if any.
4434+
if (optBase.getType().isMoveOnlyWrapped()) {
4435+
optBase
4436+
= SGF.B.createGuaranteedMoveOnlyWrapperToCopyableValue(e, optBase);
4437+
}
44264438
}
44274439
}
44284440
} else {
44294441
optBase = SGF.emitAddressOfLValue(e, std::move(optLV));
4442+
bool isMoveOnly = optBase.getType().isMoveOnly();
4443+
4444+
// Strip the move-only wrapper if any.
4445+
if (optBase.getType().isMoveOnlyWrapped()) {
4446+
optBase = ManagedValue::forLValue(
4447+
SGF.B.createMoveOnlyWrapperToCopyableAddr(e,
4448+
optBase.getValue()));
4449+
}
44304450

44314451
if (isConsumeAccess(baseAccessKind)) {
4432-
if (optBase.getType().isMoveOnly()) {
4452+
if (isMoveOnly) {
44334453
optBase = enterAccessScope(SGF, e, ManagedValue(),
44344454
optBase, optTypeData,
44354455
baseAccessKind,

test/SILGen/unmanaged-chain.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-swift-emit-silgen -verify %s
2+
3+
extension Unmanaged {
4+
func something1(with x: consuming Unmanaged<Instance>?) -> UnsafeMutableRawPointer? {
5+
x?.toOpaque()
6+
}
7+
8+
func something2(with x: consuming Unmanaged<Instance>?) -> UnsafeMutableRawPointer? {
9+
let y = x
10+
return y?.toOpaque()
11+
}
12+
func something3(with x: __owned Unmanaged<Instance>?) -> UnsafeMutableRawPointer? {
13+
x?.toOpaque()
14+
}
15+
}

0 commit comments

Comments
 (0)