Skip to content

Correct getAccessStrategy for class extension storage decls. #10050

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
8 changes: 7 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,13 @@ static bool isPolymorphic(const AbstractStorageDecl *storage) {

case DeclKind::Class:
// Final properties can always be direct, even in classes.
return !storage->isFinal();
if (storage->isFinal())
return false;
// Extension properties are statically dispatched, unless they're @objc.
if (storage->getDeclContext()->isExtensionContext()
&& !storage->isObjC())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a coincidence rather than a design decision. I'd rather not put that into key path logic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fully expect us to get overridable methods in extensions some day, and that will include computed properties.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is there a distinction to be made between "imported from ObjC" and "declared @objc" and "@objc dynamic"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't keypath-specific, it's part of AbstractStorageDecl::getAccessStrategy. When we change the language, this seems like the right place to centralize that logic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather just be implicitly marking extension declarations final than put special cases for them all over the place. This might be the right place to put one, but it's not the only one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that making them final is a better fix, but might be a bit too invasive for 4.0. Do we have a bug for that?

return false;
return true;
}
llvm_unreachable("bad DeclKind");
}
Expand Down
21 changes: 21 additions & 0 deletions test/SILGen/keypaths_objc.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-experimental-keypath-components -emit-silgen -import-objc-header %S/Inputs/keypaths_objc.h %s | %FileCheck %s
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-experimental-keypath-components -emit-ir -import-objc-header %S/Inputs/keypaths_objc.h %s
// REQUIRES: objc_interop

import Foundation
Expand Down Expand Up @@ -55,3 +56,23 @@ func objcKeypathIdentifiers() {
// CHECK: keypath $KeyPath<Foo, Int>, (objc "int"; {{.*}} id #Foo.int!getter.1 :
_ = \Foo.int
}

struct X {}

extension NSObject {
var x: X { return X() }
@objc var objc: Int { return 0 }
@objc dynamic var dynamic: Int { return 0 }
}

// CHECK-LABEL: sil hidden @{{.*}}nonobjcExtensionOfObjCClass
func nonobjcExtensionOfObjCClass() {
// Should be treated as a statically-dispatch property
// CHECK: keypath $KeyPath<NSObject, X>, ({{.*}} id @
_ = \NSObject.x
// CHECK: keypath $KeyPath<NSObject, Int>, ({{.*}} id #NSObject.objc!getter.1.foreign
_ = \NSObject.objc
// CHECK: keypath $KeyPath<NSObject, Int>, ({{.*}} id #NSObject.dynamic!getter.1.foreign
_ = \NSObject.dynamic

}