Skip to content

Commit bde7daa

Browse files
committed
[SILOptimizer] Don't apply CMO to key paths that reference inaccessible properties
rdar://145095088
1 parent 63b7f05 commit bde7daa

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,21 @@ bool CrossModuleOptimization::canSerializeFieldsByInstructionKind(
651651
canUse = methodScope.isPublicOrPackage();
652652
}
653653
});
654+
auto pattern = KPI->getPattern();
655+
for (auto &component : pattern->getComponents()) {
656+
if (!canUse) {
657+
break;
658+
}
659+
switch (component.getKind()) {
660+
case KeyPathPatternComponent::Kind::StoredProperty: {
661+
auto property = component.getStoredPropertyDecl();
662+
canUse = isPackageOrPublic(property->getEffectiveAccess());
663+
break;
664+
}
665+
default:
666+
break;
667+
}
668+
}
654669
return canUse;
655670
}
656671
if (auto *MI = dyn_cast<MethodInst>(inst)) {

test/SILOptimizer/Inputs/cross-module/cross-module.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,11 @@ public struct S<each T : Visitable> {
311311
_ = (repeat (each storage).visit())
312312
}
313313
}
314+
315+
public struct StructWithInternal {
316+
var internalVar: Int
317+
}
318+
319+
public func getKP() -> KeyPath<StructWithInternal, Int> {
320+
return \StructWithInternal.internalVar
321+
}

test/SILOptimizer/cross-module-optimization.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ func testPrivateVar() {
171171
print(getRandom())
172172
}
173173

174+
func testKeyPathAccess() -> KeyPath<StructWithInternal, Int> {
175+
return getKP()
176+
}
177+
174178
testNestedTypes()
175179
testClass()
176180
testError()
@@ -182,4 +186,4 @@ testMisc()
182186
testGlobal()
183187
testImplementationOnly()
184188
testPrivateVar()
185-
189+
testKeyPathAccess()

0 commit comments

Comments
 (0)