Skip to content

[5.1] Judge the need for an external key path reference by the overridden base. #25645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3419,23 +3419,32 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
ArrayRef<ProtocolConformanceRef> indexHashables,
CanType baseTy,
bool forPropertyDescriptor) {
auto baseDecl = storage;

// ABI-compatible overrides do not have property descriptors, so we need
// to reference the overridden declaration instead.
if (isa<ClassDecl>(baseDecl->getDeclContext())) {
while (!baseDecl->isValidKeyPathComponent())
baseDecl = baseDecl->getOverriddenDecl();
}

/// Returns true if a key path component for the given property or
/// subscript should be externally referenced.
auto shouldUseExternalKeyPathComponent = [&]() -> bool {
return (!forPropertyDescriptor &&
(storage->getModuleContext() != SwiftModule ||
storage->isResilient(SwiftModule, expansion)) &&
(baseDecl->getModuleContext() != SwiftModule ||
baseDecl->isResilient(SwiftModule, expansion)) &&
// Protocol requirements don't have nor need property descriptors.
!isa<ProtocolDecl>(storage->getDeclContext()) &&
!isa<ProtocolDecl>(baseDecl->getDeclContext()) &&
// Properties that only dispatch via ObjC lookup do not have nor
// need property descriptors, since the selector identifies the
// storage.
// Properties that are not public don't need property descriptors
// either.
(!storage->hasAnyAccessors() ||
(!getAccessorDeclRef(getRepresentativeAccessorForKeyPath(storage))
(!baseDecl->hasAnyAccessors() ||
(!getAccessorDeclRef(getRepresentativeAccessorForKeyPath(baseDecl))
.isForeign &&
getAccessorDeclRef(getRepresentativeAccessorForKeyPath(storage))
getAccessorDeclRef(getRepresentativeAccessorForKeyPath(baseDecl))
.getLinkage(ForDefinition) <= SILLinkage::PublicNonABI)));
};

Expand Down Expand Up @@ -3465,10 +3474,7 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,

// ABI-compatible overrides do not have property descriptors, so we need
// to reference the overridden declaration instead.
auto *baseDecl = externalDecl;
if (isa<ClassDecl>(baseDecl->getDeclContext())) {
while (!baseDecl->isValidKeyPathComponent())
baseDecl = baseDecl->getOverriddenDecl();
if (baseDecl != externalDecl) {
externalSubs = SubstitutionMap::getOverrideSubstitutions(baseDecl,
externalDecl,
externalSubs);
Expand Down
11 changes: 11 additions & 0 deletions test/SILGen/keypaths_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,14 @@ func sharedCProperty() {
// CHECK-NOT: external #c_union.some_field
let dataKeyPath: WritableKeyPath<c_union, some_struct>? = \c_union.some_field
}

class OverrideFrameworkObjCProperty: A {
override var counter: Int32 {
get { return 0 }
set { }
}
}

func overrideFrameworkObjCProperty() {
let _ = \OverrideFrameworkObjCProperty.counter
}