Skip to content

IRGen: Fix key path generic environment marshalling for external property descriptors. #73259

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

Conversation

jckarter
Copy link
Contributor

The layout of a computed key path component carries an argument buffer for captures, which has the following layout:

---
captured values (subscript indices)
---
generic arguments
---

When we reference an externally-defined public property or subscript from a key path, and the external declaration has a property descriptor, then the generic arguments for the external declaration get appended to the end of this buffer, giving:

---
captured values (subscript indices)
---
generic arguments
---
external property's generic arguments
---

The convention for key path accessors to bind their generic environment is thus to unpack them from the end of the argument buffer, so that the external keypath's accessors can find the arguments to bind the external generic environment while still allowing the enclosing key path to save the original captured generic environment (which may be necessary to capture the appropriate conditional and/or retroactive Equatable and Hashable conformances for subscript indices).

However, our code generation for binding the generic arguments out of the argument buffer contained a flawed optimization: for a property, we know there are never any captured values, so I had assumed that the generic parameters could always be bound from the beginning of the argument buffer, assuming that the generic parameters make up the totality of the buffer. This falls over for external property descriptor references when the key path itself captures a generic environment, since the external property's expected generic environment appears after the key path's original generic environment. We can fix this by removing the conditional entirely, and always adjusting the offset we load the generic environment from to look at the end of the buffer. Fixes rdar://125886333.

…erty descriptors.

The layout of a computed key path component carries an argument buffer for captures, which has
the following layout:

```
---
captured values (subscript indices)
---
generic arguments
---
```

When we reference an externally-defined public property or subscript from a key path, and the
external declaration has a property descriptor, then the generic arguments for the external
declaration get appended to the end of this buffer, giving:

```
---
captured values (subscript indices)
---
generic arguments
---
external property's generic arguments
---
```

The convention for key path accessors to bind their generic environment is thus to unpack them
from the end of the argument buffer, so that the external keypath's accessors can find the
arguments to bind the external generic environment while still allowing the enclosing key path
to save the original captured generic environment (which may be necessary to capture the
appropriate conditional and/or retroactive `Equatable` and `Hashable` conformances for
subscript indices).

However, our code generation for binding the generic arguments out of the argument buffer
contained a flawed optimization: for a property, we know there are never any captured values,
so I had assumed that the generic parameters could always be bound from the beginning of the
argument buffer, assuming that the generic parameters make up the totality of the buffer. This
falls over for external property descriptor references when the key path itself captures a
generic environment, since the external property's expected generic environment appears after
the key path's original generic environment. We can fix this by removing the conditional
entirely, and always adjusting the offset we load the generic environment from to look at the
end of the buffer. Fixes rdar://125886333.
@jckarter jckarter requested a review from a team as a code owner April 25, 2024 17:53
@jckarter jckarter requested review from aschwaighofer and removed request for a team April 25, 2024 17:53
@jckarter
Copy link
Contributor Author

@swift-ci Please test

Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@jckarter jckarter merged commit 2f954ef into swiftlang:main Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants