[region-isolation] Fix an off by one error when mapping AST capture indices to SIL level parameter indices. #77354
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.
This problem comes up with the following example:
The specific issue is that the closure we generate actually includes an implicit(any) parameter at the SIL level which occurs after the callee operand but before the captures. This caused the captured variable index from the AST and the one we compute from the partial_apply to differ by 1. So we need to subtract 1 in such a case. That is why we used to print 'asdf' instead of 'a' above.
DISCUSSION: This shows an interesting difference between SIL applied arg indices and AST indices. SIL applied arg indices would include the implicit(any) parameter since it is a parameter in the SIL function type. In contrast, this doesn't show up in the formal AST parameters or captures. To make it easier to reason about this, I added a new API to ApplySite called ApplySite::getASTAppliedArgIndex and added large comments to getASTAppliedArgIndex and getAppliedArgIndex that explains the issue.
rdar://136593706
#76648