-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] Support additional args in @dynamicMemberLookup subscripts #81148
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
Open
itaiferber
wants to merge
5
commits into
swiftlang:main
Choose a base branch
from
itaiferber:additional-dynamicmemberlookup-args
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,002
−321
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a51abfa
Cache SubscriptDecl @dynamicMemberLookup eligibility
itaiferber 3dbe477
Use SubscriptDecl interface for @dynamicMemberLookup checks
itaiferber d2a6a7c
Support additional args in @dynamicMemberLookup subscripts
itaiferber 7294e4d
Add isolation info to ExtInfo for SubscriptDecls
itaiferber 5e77bd2
Add unit tests for multi-arg @dynamicMemberLookup subscripts
itaiferber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,11 @@ namespace clang { | |
class PointerAuthQualifier; | ||
} // end namespace clang | ||
|
||
namespace { | ||
class AttributeChecker; | ||
class DeclChecker; | ||
} // end anonymous namespace | ||
|
||
namespace swift { | ||
enum class AccessSemantics : unsigned char; | ||
class AccessorDecl; | ||
|
@@ -509,9 +514,24 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi | |
defaultArgumentKind : NumDefaultArgumentKindBits | ||
); | ||
|
||
SWIFT_INLINE_BITFIELD(SubscriptDecl, VarDecl, 2, | ||
StaticSpelling : 2 | ||
SWIFT_INLINE_BITFIELD(SubscriptDecl, VarDecl, 2+2, | ||
StaticSpelling : 2, | ||
|
||
/// Whether this decl has been evaluated as eligible to satisfy an | ||
/// `@dynamicMemberLookup` requirement, and if eligible, the type of dynamic | ||
/// member parameter it would use to satisfy the requirement. | ||
/// | ||
/// * 0b00 - not yet evaluated | ||
/// * 0b01 - evaluated; not eligible | ||
/// * 0b10 - evaluated; eligible, taking a `{{Reference}Writable}KeyPath` | ||
/// value | ||
/// * 0b11 - evaluated; eligible, taking an `ExpressibleByStringLiteral` | ||
/// value | ||
/// | ||
/// i.e., 0 or `DynamicMemberLookupSubscriptEligibility` values + 1 | ||
DynamicMemberLookupEligibility : 2 | ||
); | ||
|
||
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+2+2+2+8+1+1+1+1+1+1, | ||
/// \see AbstractFunctionDecl::BodyKind | ||
BodyKind : 3, | ||
|
@@ -7381,6 +7401,36 @@ enum class ObjCSubscriptKind { | |
Keyed | ||
}; | ||
|
||
/// Describes how a `SubscriptDecl` could be eligible to fulfill a | ||
/// `@dynamicMemberLookup` requirement. | ||
/// | ||
/// `@dynamicMemberLookup` requires that a subscript: | ||
/// | ||
/// 1. Take an initial argument with an explicit `dynamicMember` argument | ||
/// label, | ||
/// 2. Whose parameter type is non-variadic and is either | ||
/// * A `{{Reference}Writable}KeyPath`, or | ||
/// * A concrete type conforming to `ExpressibleByStringLiteral`, | ||
/// 3. And whose following arguments (if any) are all either variadic or have | ||
/// a default value | ||
/// | ||
/// Subscripts which don't meet these requirements strictly are not eligible. | ||
enum class DynamicMemberLookupSubscriptEligibility : uint8_t { | ||
/// The `SubscriptDecl` cannot fulfill a `@dynamicMemberLookup` requirement. | ||
/// | ||
/// This can be due to a name mismatch, type mismatch, missing default | ||
/// arguments, or otherwise. | ||
None, | ||
|
||
/// The `SubscriptDecl` can fulfill a `@dynamicMemberLookup` requirement with | ||
/// a `{{Reference}Writable}KeyPath` dynamic member parameter. | ||
KeyPath, | ||
|
||
/// The `SubscriptDecl` can fulfill a `@dynamicMemberLookup` requirement with | ||
/// an `ExpressibleByStringLiteral`-conforming dynamic member parameter. | ||
String, | ||
}; | ||
|
||
/// Declares a subscripting operator for a type. | ||
/// | ||
/// A subscript declaration is defined as a get/set pair that produces a | ||
|
@@ -7410,13 +7460,35 @@ enum class ObjCSubscriptKind { | |
/// signatures (indices and element type) are distinct. | ||
/// | ||
class SubscriptDecl : public GenericContext, public AbstractStorageDecl { | ||
friend AttributeChecker; | ||
friend DeclChecker; | ||
friend class ResultTypeRequest; | ||
|
||
SourceLoc StaticLoc; | ||
SourceLoc ArrowLoc; | ||
ParameterList *Indices; | ||
TypeLoc ElementTy; | ||
|
||
// Evaluates and stores the decl's eligibility to fulfill | ||
// `@dynamicMemberLookup` requirements. Given `Diags`, emits diagnostics for | ||
// requirement mismatches; should only be passed in by `AttributeChecker` and | ||
// `DeclChecker` within a type-checking context. | ||
// | ||
// Implemented in the type checker because checking the validity of a | ||
// string-based dynamic member parameter requires checking conformance to | ||
// `ExpressibleByStringLiteral`. | ||
DynamicMemberLookupSubscriptEligibility | ||
evaluateDynamicMemberLookupEligibility(DiagnosticEngine *Diags = nullptr); | ||
|
||
// Maps `Bits.SubscriptDecl.DynamicMemberLookupEligibility` as stored to a | ||
// `DynamicMemberLookupSubscriptEligibility`; `None` if `@dynamicMemberLookup` | ||
// requirements have not been checked yet. | ||
DynamicMemberLookupSubscriptEligibility | ||
getStoredDynamicMemberLookupEligibility() const; | ||
|
||
void setDynamicMemberLookupEligibility( | ||
DynamicMemberLookupSubscriptEligibility eligibility); | ||
|
||
void setElementInterfaceType(Type type); | ||
|
||
SubscriptDecl(DeclName Name, | ||
|
@@ -7435,6 +7507,11 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl { | |
setIndices(Indices); | ||
} | ||
|
||
/// Returns the given as a `BoundGenericType` if it is a | ||
/// `{{Reference}Writable}KeyPath` type which could be used to fulfill | ||
/// `@dynamicMemberLookup` requirements; `nullptr` otherwise. | ||
static BoundGenericType *getDynamicMemberParamTypeAsKeyPathType(Type paramTy); | ||
|
||
public: | ||
/// Factory function only for use by deserialization. | ||
static SubscriptDecl *createDeserialized(ASTContext &Context, DeclName Name, | ||
|
@@ -7496,6 +7573,16 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl { | |
/// implies. | ||
ObjCSubscriptKind getObjCSubscriptKind() const; | ||
|
||
/// Determines, caches, and returns whether the decl can be used to satisfy an | ||
/// `@dynamicMemberLookup` requirement (and if so, how). | ||
DynamicMemberLookupSubscriptEligibility | ||
getDynamicMemberLookupSubscriptEligibility(); | ||
Comment on lines
+7578
to
+7579
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of exposing this enum, I suppose it is also possible to restore |
||
|
||
/// If the decl can be used to satisfy an `@dynamicMemberLookup` requirement | ||
/// using a `{{Reference}Writable}KeyPath` dynamic member parameter, returns | ||
/// the type of that parameter; `nullptr` otherwise. | ||
BoundGenericType *getDynamicMemberLookupKeyPathType(); | ||
|
||
SubscriptDecl *getOverriddenDecl() const { | ||
return cast_or_null<SubscriptDecl>( | ||
AbstractStorageDecl::getOverriddenDecl()); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DynamicMemberLookupSubscriptEligibility
is... a mouthful — but it's the least-inaccurate name I could come up with. Happy to improve this with suggestions!