IRGen: Fix key path generic environment marshalling for external property descriptors. #73259
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.
The layout of a computed key path component carries an argument buffer for captures, which has the following layout:
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:
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
andHashable
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.