Skip to content

[Sema/SILGen/IRGen] Implement method & initializer keypaths. #78823

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 29 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8f71f5c
[NFC] Rename property to member to generalize
amritpan Sep 5, 2024
811d549
[NFC] Rename unresolvedProperty to unresolvedMember
amritpan Sep 5, 2024
2583da9
[NFC] Generalize subscript index handling.
amritpan Sep 5, 2024
0af27a6
[ASTGen] Handle keypath methods.
amritpan Nov 19, 2024
0c614e0
[Expr/AST] Add unresolvedApply component to handle method arguments.
amritpan Sep 5, 2024
978a521
[Expr/AST] Add apply component to handle resolved method arguments.
amritpan Sep 5, 2024
6af6374
[CSGen] Generate constraints for keypaths to methods.
amritpan Sep 5, 2024
c059d15
[ConstraintSystem] Evaluate capabilities for keypath type.
amritpan Sep 5, 2024
724e3f9
[Constraint System] Evaluable hashability during constraint generation.
amritpan Mar 4, 2025
0e895c6
[CSApply] Solve keypath methods and initializers.
amritpan Jan 10, 2025
7ff563e
[CSDiagnostics] Remove checks preventing method/initializer keypaths.
amritpan Sep 6, 2024
970159c
[CSDiagnostics] Block async, throws and mutating methods.
amritpan Jan 10, 2025
a96b780
[Sema] Diagnose method arg captures that are not Hashable/Equatable.
amritpan Jan 17, 2025
86d456e
[Sema] Block inout method arguments.
amritpan Jan 18, 2025
555a486
[Mangler] Add new mangling schemes.
amritpan Dec 22, 2024
e7e354d
[NFC] Generalize subscript code for methods.
amritpan Nov 26, 2024
ae86b0e
[NFC] Refactor getter/setter logic for reuse.
amritpan Jan 22, 2025
91c94a6
[NFC] Refactor callee formation for reuse.
amritpan Jan 17, 2025
e5362c7
[NFC] Refactor subscript argument evaluation for reuse.
amritpan Jan 22, 2025
43df15e
[SILGen] Add KeyPathComponentKind.
amritpan Dec 23, 2024
60e03a8
[SILGen] Lower applied keypath methods.
amritpan Jan 21, 2025
4479996
[NFC] Refactor method thunk handling for reuse.
amritpan Jan 22, 2025
45a7b45
[SILGen] Lower unapplied methods.
amritpan Dec 20, 2024
807c323
[SILGenExpr] Lower initializers.
amritpan Dec 25, 2024
39b48bb
[SIL] Handle KeyPathComponentKind::Method in helpers.
amritpan Nov 26, 2024
834bd0e
[IRGen] Prevent descriptor emission.
amritpan Dec 20, 2024
78e7f6a
[Tests] Update tests.
amritpan Dec 9, 2024
15c219c
[Tests] Add tests.
amritpan Dec 17, 2024
98cd675
Guard feature behind experimental flag.
amritpan Jan 20, 2025
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
2 changes: 2 additions & 0 deletions docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ types where the metadata itself has unknown layout.)
global ::= from-type to-type generic-signature? 'Tr' // obsolete mangling for reabstraction thunk
global ::= entity generic-signature? type type* 'TK' // key path getter
global ::= entity generic-signature? type type* 'Tk' // key path setter
global ::= entity generic-signature? type type* 'Tkmu' // key path unapplied method
global ::= entity generic-signature? type type* 'TkMA' // key path applied method
global ::= type generic-signature 'TH' // key path equality
global ::= type generic-signature 'Th' // key path hasher
global ::= global generic-signature? 'TJ' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // autodiff function
Expand Down
14 changes: 9 additions & 5 deletions include/swift/AST/ASTMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,20 @@ class ASTMangler : public Mangler {
AutoDiffLinearMapKind linearMapKind,
const AutoDiffConfig &config);

std::string mangleKeyPathGetterThunkHelper(const AbstractStorageDecl *property,
GenericSignature signature,
CanType baseType,
SubstitutionMap subs,
ResilienceExpansion expansion);
std::string mangleKeyPathGetterThunkHelper(
const AbstractStorageDecl *property, GenericSignature signature,
CanType baseType, SubstitutionMap subs, ResilienceExpansion expansion);
std::string mangleKeyPathSetterThunkHelper(const AbstractStorageDecl *property,
GenericSignature signature,
CanType baseType,
SubstitutionMap subs,
ResilienceExpansion expansion);
std::string mangleKeyPathUnappliedMethodThunkHelper(
const AbstractFunctionDecl *method, GenericSignature signature,
CanType baseType, SubstitutionMap subs, ResilienceExpansion expansion);
std::string mangleKeyPathAppliedMethodThunkHelper(
const AbstractFunctionDecl *method, GenericSignature signature,
CanType baseType, SubstitutionMap subs, ResilienceExpansion expansion);
std::string mangleKeyPathEqualsHelper(ArrayRef<CanType> indices,
GenericSignature signature,
ResilienceExpansion expansion);
Expand Down
12 changes: 12 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8171,6 +8171,18 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
/// The async function marked as the alternative to this function, if any.
AbstractFunctionDecl *getAsyncAlternative() const;

/// True if the storage can be referenced by a keypath directly.
/// Otherwise, its override must be referenced.
bool isValidKeyPathComponent() const;

/// Do we need to use resilient access patterns outside of this
/// method's resilience domain?
bool isResilient() const;

/// Do we need to use resilient access patterns when accessing this
/// method from the given module?
bool isResilient(ModuleDecl *M, ResilienceExpansion expansion) const;

/// If \p asyncAlternative is set, then compare its parameters to this
/// (presumed synchronous) function's parameters to find the index of the
/// completion handler parameter. This should be the only missing
Expand Down
13 changes: 9 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,9 @@ NOTE(keypath_static_member_access_from_unsupported_module_note,none,
ERROR(expr_keypath_enum_case,none,
"%select{key path|dynamic key path member lookup}1 cannot refer to enum case %0",
(const ValueDecl *, bool))
ERROR(expr_keypath_mutating_method,none,
"%select{key path|dynamic key path member lookup}1 cannot refer to mutating method %0",
(const ValueDecl *, bool))
ERROR(expr_keypath_empty,none,
"empty key path does not refer to a property", ())
ERROR(expr_unsupported_objc_key_path_component,none,
Expand Down Expand Up @@ -716,8 +719,8 @@ ERROR(expr_swift_keypath_empty, none,
"key path must have at least one component", ())
ERROR(expr_string_interpolation_outside_string,none,
"string interpolation can only appear inside a string literal", ())
ERROR(expr_keypath_subscript_index_not_hashable, none,
"subscript index of type %0 in a key path must be Hashable", (Type))
ERROR(expr_keypath_arg_or_index_not_hashable, none,
"%select{method argument|subscript index}0 of type %1 in a key path must be Hashable", (bool, Type))
ERROR(expr_smart_keypath_application_type_mismatch,none,
"key path of type %0 cannot be applied to a base of type %1",
(Type, Type))
Expand Down Expand Up @@ -1558,6 +1561,8 @@ ERROR(cannot_pass_inout_arg_to_subscript,none,
"cannot pass an inout argument to a subscript; use "
"'withUnsafeMutablePointer' to explicitly convert argument "
"to a pointer", ())
ERROR(cannot_pass_inout_arg_to_keypath_method,none,
"cannot pass an inout argument to a keypath method", ())

ERROR(incorrect_property_wrapper_reference,none,
"cannot convert value %0 of type %1 to expected type %2, "
Expand Down Expand Up @@ -5594,8 +5599,8 @@ ERROR(actor_isolated_keypath_component,none,
"cannot form key path to %0 %kind1",
(ActorIsolation, const ValueDecl *))
ERROR(effectful_keypath_component,none,
"cannot form key path to %0 with 'throws' or 'async'",
(DescriptiveDeclKind))
"cannot form %select{key path|dynamic key path member lookup}1 to %0 with 'throws' or 'async'",
(DescriptiveDeclKind, bool))
ERROR(local_function_executed_concurrently,none,
"concurrently-executed %kind0 must be marked as '@Sendable'",
(const ValueDecl *))
Expand Down
Loading