Skip to content

Commit 355b7dc

Browse files
committed
fix bug when devirtualizing a begin_apply during inlining
When devirtualizing a `begin_apply`, it was passing the token's use list to the conversion function when trying to convert the yielded result. It's suppose to be the yielded result's list. This became apparent when it encountered an access of a `@_borrowed` property and we hit an assertion about an empty use-list of a guaranteed value, when it was in fact the wrong list!
1 parent 60772f1 commit 355b7dc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ replaceBeginApplyInst(SILBuilder &builder, SILLocation loc,
559559
auto newYield = newYields[i];
560560
// Insert any end_borrow if the yielded value before the token's uses.
561561
SmallVector<SILInstruction *, 4> users(
562-
makeUserIteratorRange(oldBAI->getTokenResult()->getUses()));
562+
makeUserIteratorRange(oldYield->getUses()));
563563
auto yieldCastRes = castValueToABICompatibleType(
564564
&builder, loc, newYield, newYield->getType(), oldYield->getType(),
565565
users);

test/SILOptimizer/mandatory_inlining_devirt.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -sil-verify-all %s -module-name test -emit-sil -o - -verify | %FileCheck %s
1+
// RUN: %target-swift-frontend -sil-verify-all %s -module-name test -emit-sil -o - -verify | %FileCheck %s --enable-var-scope
22

33

44
// Constructor calls are dispatched dynamically for open classes, even if
@@ -49,3 +49,19 @@ public func caller(_ c: Concrete) throws {
4949
// CHECK: try_apply [[FN]]([[ARG]]) : $@convention(method) (Concrete) -> @error any Error
5050
try callee(c)
5151
}
52+
53+
54+
public struct File {
55+
var alias: FileHandle = FileHandle()
56+
}
57+
58+
public class FileHandle {
59+
@_borrowed var file: File = File()
60+
}
61+
62+
// CHECK-LABEL: sil @$s4test20access_borrowed_readyAA10FileHandleCADF : $@convention(thin) (@guaranteed FileHandle) -> @owned FileHandle {
63+
public func access_borrowed_read(_ l: FileHandle) -> FileHandle {
64+
// CHECK-NOT: begin_apply
65+
// CHECK: return
66+
return l.file.alias
67+
}

0 commit comments

Comments
 (0)