Skip to content

Conversation

itaiferber
Copy link
Contributor

@itaiferber itaiferber commented Apr 28, 2025

Adds support for satisfying @dynamicMemberLookup requirements using subscripts which have arguments following dynamicMember: so long as they are either variadic or have default arguments. This allows transforming member references into x[dynamicMember:...] calls which can pass in arguments such as #function, #fileID, #line, etc.

Main changes:

  1. Allows SubscriptDecls to satisfy @dynamicMemberLookup requirements as long as dynamicMember: remains the first explicitly-labeled argument, and all following arguments are either isDefaultArgument() or isVariadic()
  2. Caches the result of checking for eligibility in SubscriptDecl.Bits; this was being checked multiple times per decl and it seems prudent to just store the info since we have the bits
  3. Updates ExprRewriter to produce ArgumentLists for these subscripts by filling in DefaultArgumentExprs as needed
  4. Adds unit tests

The PR has been split into individual commits for (ideally) ease of review.

Copy link
Contributor Author

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!

Comment on lines +7578 to +7579
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 isValidDynamicMemberLookupSubscript() et. al., but I do think it's nicer to have a type-safe interface that's harder to typo.

@itaiferber
Copy link
Contributor Author

@swift-ci please smoke test

itaiferber added 5 commits May 6, 2025 14:58
`SubscriptDecl`s may get checked multiple times for eligibility in
fulfilling `@dynamicMemberLookup` requirements; since the checks are
non-trivial and the result doesn't change, this eligibility can be
cached in the decl's `Bits`.
`SubscriptDecl` exposes eligibility for `@dynamicMemberLookup`
requirements directly, so the `TypeChecker` interface for these members
can be replaced.
Adds support for `SubscriptDecl`s to fulfill `@dynamicMemberLookup`
requirements if they have additional arguments after `dynamicMember:` so
long as those arguments have default values, or are variadic.

This allows exposing values like `#function`, `#fileID`, `#line`, etc.
to dynamic member lookup.
Subscripts with `isolated` arguments currently do not pass a compiler
assertion about the consistency of the function type w.r.t. isolation.
This is because the `ExtInfo` for `SubscriptDecl`s doesn't currently
have isolation information applied (though this works in practice);
annotating the `ExtInfo` with isolation allows this assertion to pass.
@itaiferber itaiferber force-pushed the additional-dynamicmemberlookup-args branch from 725aeab to 5e77bd2 Compare May 6, 2025 19:17
@itaiferber
Copy link
Contributor Author

@swift-ci Please smoke test

@xwu
Copy link
Collaborator

xwu commented May 12, 2025

@swift-ci Please build toolchain

@xwu xwu added the swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process label May 27, 2025
@itaiferber
Copy link
Contributor Author

@slavapestov / @hborla / @xedin Now that SE-0484 has been accepted, please don't hesitate to let me know if there's anything I can do to make a review here easier — I know this is a busy time for Apple folks. Hoping the changes here are uncontroversial enough, but happy to adjust with feedback!

@itaiferber
Copy link
Contributor Author

I assume that it's probably a bit late to try to get this into Swift 6.2 at this point and that we'll aim for 6.3, but if there's anything I can do to help with this PR, please let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants