-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,10 @@ namespace clang { | |
class PointerAuthQualifier; | ||
} // end namespace clang | ||
|
||
namespace { | ||
class AttributeChecker; | ||
} // end anonymous namespace | ||
|
||
namespace swift { | ||
enum class AccessSemantics : unsigned char; | ||
class AccessorDecl; | ||
|
@@ -509,9 +513,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 +7400,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 +7459,23 @@ enum class ObjCSubscriptKind { | |
/// signatures (indices and element type) are distinct. | ||
/// | ||
class SubscriptDecl : public GenericContext, public AbstractStorageDecl { | ||
friend AttributeChecker; | ||
friend class ResultTypeRequest; | ||
|
||
SourceLoc StaticLoc; | ||
SourceLoc ArrowLoc; | ||
ParameterList *Indices; | ||
TypeLoc ElementTy; | ||
|
||
// 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 +7494,24 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl { | |
setIndices(Indices); | ||
} | ||
|
||
// Evaluates whether `SD` could satisfy `@dynamicMemberLookup` requirements | ||
// (without storing the result in `SD`). `ignoreArgLabel` allows checking of | ||
// requirements even if the subscript doesn't have a `dynamicMember:` argument | ||
// label; this is used by `AttributeChecker::visitDynamicMemberLookupAttr` to | ||
// produce fix-its and should otherwise be `false`. | ||
// | ||
// Implemented in the typechecker because it requires access to type checking | ||
// to validate the conformance of the `dynamicMember:` argument to | ||
// `ExpressibleByStringLiteral`. | ||
static DynamicMemberLookupSubscriptEligibility | ||
evaluateDynamicMemberLookupEligibility(const SubscriptDecl *SD, | ||
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. This is the core of what was previously |
||
bool ignoreArgLabel = false); | ||
|
||
/// 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()); | ||
|
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!