Skip to content

[5.7] SILGen: Don't reference external property descriptors for @_alwaysEmitIntoClient properties. #59214

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 1 commit into from
Jun 2, 2022
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
56 changes: 41 additions & 15 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3580,21 +3580,47 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
/// Returns true if a key path component for the given property or
/// subscript should be externally referenced.
auto shouldUseExternalKeyPathComponent = [&]() -> bool {
return (!forPropertyDescriptor &&
(baseDecl->getModuleContext() != SwiftModule ||
baseDecl->isResilient(SwiftModule, expansion)) &&
// Protocol requirements don't have nor need property descriptors.
!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.
(!baseDecl->requiresOpaqueAccessors() ||
(!getAccessorDeclRef(getRepresentativeAccessorForKeyPath(baseDecl))
.isForeign &&
getAccessorDeclRef(getRepresentativeAccessorForKeyPath(baseDecl))
.getLinkage(ForDefinition) <= SILLinkage::PublicNonABI)));
// The property descriptor has the canonical key path component information
// so doesn't have to refer to another external descriptor.
if (forPropertyDescriptor) {
return false;
}

// Don't need to use the external component if we're inside the resilience
// domain of its defining module.
if (baseDecl->getModuleContext() == SwiftModule
&& !baseDecl->isResilient(SwiftModule, expansion)) {
return false;
}

// Protocol requirements don't have nor need property descriptors.
if (isa<ProtocolDecl>(baseDecl->getDeclContext())) {
return false;
}

// Always-emit-into-client properties can't reliably refer to a property
// descriptor that may not exist in older versions of their home dylib.
// Their definition is also always entirely visible to clients so it isn't
// needed.
if (baseDecl->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
return false;
}

// 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.
if (baseDecl->requiresOpaqueAccessors()) {
auto representative = getAccessorDeclRef(
getRepresentativeAccessorForKeyPath(baseDecl));
if (representative.isForeign)
return false;
if (representative.getLinkage(ForDefinition) > SILLinkage::PublicNonABI)
return false;
}

return true;
};

auto strategy = storage->getAccessStrategy(AccessSemantics::Ordinary,
Expand Down
6 changes: 6 additions & 0 deletions validation-test/Evolution/Inputs/keypaths.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@ public final class AFinalClass {

}

public struct AEIC {
#if AFTER
@_alwaysEmitIntoClient
public var aeic: Int { return 0 }
#endif
}
12 changes: 12 additions & 0 deletions validation-test/Evolution/test_keypaths.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,16 @@ tests.test("AFinalClass.${name}") {

% end

#if AFTER
tests.test("AlwaysEmitIntoClient") {
// AEIC declarations are vendored into their importing module, so
// equality doesn't currently work reliably with them. We can however
// test that the reference to the property works and is backward-compatible
// when using the `after` test suite with the `before` dylib.
let kp = \AEIC.aeic

expectEqual(kp, kp)
}
#endif

runAllTests()