Skip to content

Commit f7aaa35

Browse files
committed
Swift SIL: fix KeyPathInst.visitReferencedFunctions
It crashed for keypath instructions with zero components.
1 parent f6e6b2e commit f7aaa35

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,8 @@ final public class RefTailAddrInst : SingleValueInstruction, UnaryInstruction {}
480480
final public class KeyPathInst : SingleValueInstruction {
481481
public override func visitReferencedFunctions(_ cl: (Function) -> ()) {
482482
var results = KeyPathFunctionResults()
483-
var componentIdx = 0
484-
repeat {
485-
componentIdx = KeyPathInst_getReferencedFunctions(bridged, componentIdx, &results)
483+
for componentIdx in 0..<KeyPathInst_getNumComponents(bridged) {
484+
KeyPathInst_getReferencedFunctions(bridged, componentIdx, &results)
486485
let numFuncs = results.numFunctions
487486
withUnsafePointer(to: &results) {
488487
$0.withMemoryRebound(to: BridgedFunction.self, capacity: numFuncs) {
@@ -492,7 +491,7 @@ final public class KeyPathInst : SingleValueInstruction {
492491
}
493492
}
494493
}
495-
} while componentIdx >= 0
494+
}
496495
}
497496
}
498497

include/swift/SIL/SILBridging.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ struct KeyPathFunctionResults {
399399
BridgedFunction functions[maxFunctions];
400400
SwiftInt numFunctions;
401401
};
402-
SwiftInt KeyPathInst_getReferencedFunctions(BridgedInstruction kpi, SwiftInt componentIdx,
402+
SwiftInt KeyPathInst_getNumComponents(BridgedInstruction kpi);
403+
void KeyPathInst_getReferencedFunctions(BridgedInstruction kpi, SwiftInt componentIdx,
403404
KeyPathFunctionResults * _Nonnull results);
404405

405406
BridgedSubstitutionMap ApplySite_getSubstitutionMap(BridgedInstruction inst);

lib/SIL/Utils/SILBridging.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,14 @@ SwiftInt CondBranchInst_getNumTrueArgs(BridgedInstruction cbr) {
886886
return castToInst<CondBranchInst>(cbr)->getNumTrueArgs();
887887
}
888888

889-
SwiftInt KeyPathInst_getReferencedFunctions(BridgedInstruction kpi, SwiftInt componentIdx,
889+
SwiftInt KeyPathInst_getNumComponents(BridgedInstruction kpi) {
890+
if (KeyPathPattern *pattern = castToInst<KeyPathInst>(kpi)->getPattern()) {
891+
return (SwiftInt)pattern->getComponents().size();
892+
}
893+
return 0;
894+
}
895+
896+
void KeyPathInst_getReferencedFunctions(BridgedInstruction kpi, SwiftInt componentIdx,
890897
KeyPathFunctionResults * _Nonnull results) {
891898
KeyPathPattern *pattern = castToInst<KeyPathInst>(kpi)->getPattern();
892899
const KeyPathPatternComponent &comp = pattern->getComponents()[componentIdx];
@@ -896,9 +903,6 @@ SwiftInt KeyPathInst_getReferencedFunctions(BridgedInstruction kpi, SwiftInt com
896903
assert(results->numFunctions < KeyPathFunctionResults::maxFunctions);
897904
results->functions[results->numFunctions++] = {func};
898905
}, [](SILDeclRef) {});
899-
900-
++componentIdx;
901-
return componentIdx < (int)pattern->getComponents().size() ? componentIdx : -1;
902906
}
903907

904908
BridgedSubstitutionMap ApplySite_getSubstitutionMap(BridgedInstruction inst) {

0 commit comments

Comments
 (0)