Skip to content

Commit 3821f6e

Browse files
authored
Merge pull request #12677 from graydon/rdar-34913689-dead-function-elim-vs-foreign-declref-keypaths-swift-4.1-branch
2 parents 56bc5a6 + cbcd313 commit 3821f6e

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

lib/SILOptimizer/IPO/DeadFunctionElimination.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,22 @@ class FunctionLivenessComputation {
223223
auto id = component.getComputedPropertyId();
224224
switch (id.getKind()) {
225225
case KeyPathPatternComponent::ComputedPropertyId::DeclRef: {
226-
auto decl = cast<AbstractFunctionDecl>(id.getDeclRef().getDecl());
227-
if (auto clas = dyn_cast<ClassDecl>(decl->getDeclContext())) {
228-
ensureAliveClassMethod(getMethodInfo(decl, /*witness*/ false),
229-
dyn_cast<FuncDecl>(decl),
230-
clas);
231-
} else if (isa<ProtocolDecl>(decl->getDeclContext())) {
232-
ensureAliveProtocolMethod(getMethodInfo(decl, /*witness*/ true));
226+
auto declRef = id.getDeclRef();
227+
if (declRef.isForeign) {
228+
// Nothing to do here: foreign functions aren't ours to be deleting.
229+
// (And even if they were, they're ObjC-dispatched and thus anchored
230+
// already: see isAnchorFunction)
233231
} else {
234-
llvm_unreachable("key path keyed by a non-class, non-protocol method");
232+
auto decl = cast<AbstractFunctionDecl>(declRef.getDecl());
233+
if (auto clas = dyn_cast<ClassDecl>(decl->getDeclContext())) {
234+
ensureAliveClassMethod(getMethodInfo(decl, /*witness*/ false),
235+
dyn_cast<FuncDecl>(decl),
236+
clas);
237+
} else if (isa<ProtocolDecl>(decl->getDeclContext())) {
238+
ensureAliveProtocolMethod(getMethodInfo(decl, /*witness*/ true));
239+
} else {
240+
llvm_unreachable("key path keyed by a non-class, non-protocol method");
241+
}
235242
}
236243
break;
237244
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@import Foundation;
2+
3+
@interface ObjCFoo
4+
5+
@property(readonly) NSString *_Nonnull objcProp;
6+
7+
@end
8+
9+
10+
@interface ObjCFoo (Extras)
11+
12+
@property(readonly) NSString *_Nonnull objcExtraProp;
13+
14+
@end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-swift-frontend %s -O -emit-sil -import-objc-header %S/Inputs/keypaths_objc.h
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
public func test_nocrash_rdar34913689() {
6+
_ = \ObjCFoo.objcExtraProp
7+
}

0 commit comments

Comments
 (0)