Skip to content
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

[Sema/SILGen/IRGen/StdLib] Implement metatype keypaths #73242

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

amritpan
Copy link
Contributor

@amritpan amritpan commented Apr 25, 2024

This is the implementation for SE-NNNN which extends keypath usage to include references to static properties,
eg. metatype keypaths:

struct Bee {
  static let name = "honeybee"
}

let kp = \Bee.Type.name

@amritpan amritpan requested review from hborla, slavapestov, xedin and a team as code owners April 25, 2024 02:59
@amritpan amritpan changed the title Implement metatype keypaths [Sema/SILGen/IRGen/StdLib] Implement metatype keypaths Apr 25, 2024
@Azoy Azoy requested a review from jckarter April 25, 2024 16:40
// CHECK: true
print(keyPath3FromLibB == keyPath3FromLibC)
// CHECK: true
print(keyPath3FromLibB != keyPath4FromLibC)
Copy link
Member

Choose a reason for hiding this comment

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

@amritpan I think we need a lot more tests. Could you please add tests that access static properties and subscripts to test/SILGen/keypaths.swift, we also need chained access tests in Sema and in SILGen and interpreter tests for all of the above that make sure that changes behave as expected at runtime.

_ = \X.a // expected-error{{static member 'a' cannot be used on instance of type 'X'}}
_ = \X.Type.a
_ = \X.b // expected-error{{static member 'b' cannot be used on instance of type 'X'}}
_ = \X.Type.b
Copy link
Member

Choose a reason for hiding this comment

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

We need a few tests here that flip the situation around and try to access instance member (property or subscript) on a metatype base.

lib/Sema/CSGen.cpp Outdated Show resolved Hide resolved
lib/Sema/CSFix.cpp Show resolved Hide resolved
include/swift/Sema/CSFix.h Outdated Show resolved Hide resolved
include/swift/Sema/CSFix.h Outdated Show resolved Hide resolved
@@ -7192,6 +7192,8 @@ void SILProperty::verify(const SILModule &M) const {
auto sig = dc->getGenericSignatureOfContext();
auto baseTy = dc->getInnermostTypeContext()->getSelfInterfaceType()
->getReducedType(sig);
if (decl->isStatic())
baseTy = CanMetatypeType::get(baseTy);
Copy link
Member

Choose a reason for hiding this comment

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

Since retrieval of the base type is not as straightforward anymore I think it should either become a property of SILProperty or we should add a method on SILProperty i.e. CanType SILProperty::getBaseType() const to compute it internally.

Copy link
Contributor Author

@amritpan amritpan May 3, 2024

Choose a reason for hiding this comment

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

I added CanType SILProperty::getBaseType() const to SILVerifier.cpp above ::verify and don't think that is the place for it but am not sure where else it should go. Perhaps at the end of SILType.cpp?

@amritpan amritpan force-pushed the metatype-kp-implementation branch 2 times, most recently from a3ba657 to bc8d04b Compare May 3, 2024 18:13
@amritpan amritpan force-pushed the metatype-kp-implementation branch from bc8d04b to 71bc3a6 Compare May 5, 2024 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants