Skip to content

Commit 3c30d68

Browse files
authored
Merge pull request #78823 from amritpan/method-keypaths
[Sema/SILGen/IRGen] Implement method & initializer keypaths.
2 parents 75ba7a8 + 98cd675 commit 3c30d68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2531
-1110
lines changed

docs/ABI/Mangling.rst

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ types where the metadata itself has unknown layout.)
259259
global ::= from-type to-type generic-signature? 'Tr' // obsolete mangling for reabstraction thunk
260260
global ::= entity generic-signature? type type* 'TK' // key path getter
261261
global ::= entity generic-signature? type type* 'Tk' // key path setter
262+
global ::= entity generic-signature? type type* 'Tkmu' // key path unapplied method
263+
global ::= entity generic-signature? type type* 'TkMA' // key path applied method
262264
global ::= type generic-signature 'TH' // key path equality
263265
global ::= type generic-signature 'Th' // key path hasher
264266
global ::= global generic-signature? 'TJ' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // autodiff function

include/swift/AST/ASTMangler.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,20 @@ class ASTMangler : public Mangler {
346346
AutoDiffLinearMapKind linearMapKind,
347347
const AutoDiffConfig &config);
348348

349-
std::string mangleKeyPathGetterThunkHelper(const AbstractStorageDecl *property,
350-
GenericSignature signature,
351-
CanType baseType,
352-
SubstitutionMap subs,
353-
ResilienceExpansion expansion);
349+
std::string mangleKeyPathGetterThunkHelper(
350+
const AbstractStorageDecl *property, GenericSignature signature,
351+
CanType baseType, SubstitutionMap subs, ResilienceExpansion expansion);
354352
std::string mangleKeyPathSetterThunkHelper(const AbstractStorageDecl *property,
355353
GenericSignature signature,
356354
CanType baseType,
357355
SubstitutionMap subs,
358356
ResilienceExpansion expansion);
357+
std::string mangleKeyPathUnappliedMethodThunkHelper(
358+
const AbstractFunctionDecl *method, GenericSignature signature,
359+
CanType baseType, SubstitutionMap subs, ResilienceExpansion expansion);
360+
std::string mangleKeyPathAppliedMethodThunkHelper(
361+
const AbstractFunctionDecl *method, GenericSignature signature,
362+
CanType baseType, SubstitutionMap subs, ResilienceExpansion expansion);
359363
std::string mangleKeyPathEqualsHelper(ArrayRef<CanType> indices,
360364
GenericSignature signature,
361365
ResilienceExpansion expansion);

include/swift/AST/Decl.h

+12
Original file line numberDiff line numberDiff line change
@@ -8171,6 +8171,18 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
81718171
/// The async function marked as the alternative to this function, if any.
81728172
AbstractFunctionDecl *getAsyncAlternative() const;
81738173

8174+
/// True if the storage can be referenced by a keypath directly.
8175+
/// Otherwise, its override must be referenced.
8176+
bool isValidKeyPathComponent() const;
8177+
8178+
/// Do we need to use resilient access patterns outside of this
8179+
/// method's resilience domain?
8180+
bool isResilient() const;
8181+
8182+
/// Do we need to use resilient access patterns when accessing this
8183+
/// method from the given module?
8184+
bool isResilient(ModuleDecl *M, ResilienceExpansion expansion) const;
8185+
81748186
/// If \p asyncAlternative is set, then compare its parameters to this
81758187
/// (presumed synchronous) function's parameters to find the index of the
81768188
/// completion handler parameter. This should be the only missing

include/swift/AST/DiagnosticsSema.def

+9-4
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,9 @@ NOTE(keypath_static_member_access_from_unsupported_module_note,none,
689689
ERROR(expr_keypath_enum_case,none,
690690
"%select{key path|dynamic key path member lookup}1 cannot refer to enum case %0",
691691
(const ValueDecl *, bool))
692+
ERROR(expr_keypath_mutating_method,none,
693+
"%select{key path|dynamic key path member lookup}1 cannot refer to mutating method %0",
694+
(const ValueDecl *, bool))
692695
ERROR(expr_keypath_empty,none,
693696
"empty key path does not refer to a property", ())
694697
ERROR(expr_unsupported_objc_key_path_component,none,
@@ -718,8 +721,8 @@ ERROR(expr_swift_keypath_empty, none,
718721
"key path must have at least one component", ())
719722
ERROR(expr_string_interpolation_outside_string,none,
720723
"string interpolation can only appear inside a string literal", ())
721-
ERROR(expr_keypath_subscript_index_not_hashable, none,
722-
"subscript index of type %0 in a key path must be Hashable", (Type))
724+
ERROR(expr_keypath_arg_or_index_not_hashable, none,
725+
"%select{method argument|subscript index}0 of type %1 in a key path must be Hashable", (bool, Type))
723726
ERROR(expr_smart_keypath_application_type_mismatch,none,
724727
"key path of type %0 cannot be applied to a base of type %1",
725728
(Type, Type))
@@ -1560,6 +1563,8 @@ ERROR(cannot_pass_inout_arg_to_subscript,none,
15601563
"cannot pass an inout argument to a subscript; use "
15611564
"'withUnsafeMutablePointer' to explicitly convert argument "
15621565
"to a pointer", ())
1566+
ERROR(cannot_pass_inout_arg_to_keypath_method,none,
1567+
"cannot pass an inout argument to a keypath method", ())
15631568

15641569
ERROR(incorrect_property_wrapper_reference,none,
15651570
"cannot convert value %0 of type %1 to expected type %2, "
@@ -5596,8 +5601,8 @@ ERROR(actor_isolated_keypath_component,none,
55965601
"cannot form key path to %0 %kind1",
55975602
(ActorIsolation, const ValueDecl *))
55985603
ERROR(effectful_keypath_component,none,
5599-
"cannot form key path to %0 with 'throws' or 'async'",
5600-
(DescriptiveDeclKind))
5604+
"cannot form %select{key path|dynamic key path member lookup}1 to %0 with 'throws' or 'async'",
5605+
(DescriptiveDeclKind, bool))
56015606
ERROR(local_function_executed_concurrently,none,
56025607
"concurrently-executed %kind0 must be marked as '@Sendable'",
56035608
(const ValueDecl *))

0 commit comments

Comments
 (0)