Skip to content

Commit a081e04

Browse files
committed
SILCombine: fix a crash when optimizing keypath-offset-of
Need to check if the keypath's root type is a legal SIL type. rdar://103834325
1 parent b95fe48 commit a081e04

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ bool SILCombiner::tryOptimizeKeypathOffsetOf(ApplyInst *AI,
344344
KeyPathPattern *pattern = kp->getPattern();
345345
SubstitutionMap patternSubs = kp->getSubstitutions();
346346
CanType rootTy = pattern->getRootType().subst(patternSubs)->getCanonicalType();
347+
348+
// TODO: support lowering of the rootTy if it's not a legal SIL type in the
349+
// first place, e.g. if it contains an AnyFunctionType.
350+
if (!rootTy->isLegalSILType())
351+
return false;
352+
347353
CanType parentTy = rootTy;
348354

349355
// First check if _storedInlineOffset would return an offset or nil. Basically

test/SILOptimizer/keypath_offset.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ struct TupleProperties {
6161

6262
typealias Tuple<T, U> = (S<T>, C<U>)
6363

64+
typealias TupleOfFunctions = (a: @convention(c) () -> (), b: @convention(c) () -> ())
65+
6466
func getIdentityKeyPathOfType<T>(_: T.Type) -> KeyPath<T, T> {
6567
return \.self
6668
}
@@ -157,6 +159,12 @@ func testTupleOffsets() {
157159
printOffset(TLayout.offset(of: \Tuple<Int, Int>.1))
158160
}
159161

162+
// Just check that we don't crash.
163+
@inline(never)
164+
func testTupleOfFunctions() {
165+
printOffset(MemoryLayout<TupleOfFunctions>.offset(of: \.b))
166+
}
167+
160168
// CHECK-LABEL: sil {{.*}} @$s4test0A19GenericTupleOffsetsyyxmlF
161169
// CHECK-NOT: _storedInlineOffset
162170
// CHECK-NOT: class_method
@@ -194,4 +202,6 @@ print("### testGenericTupleOffsets")
194202
testGenericTupleOffsets(Int.self)
195203
print("### testResilientOffsets")
196204
testResilientOffsets()
205+
print("### testTupleOfFunctions")
206+
testTupleOfFunctions()
197207

0 commit comments

Comments
 (0)