cl: support range over function types (Go 1.23)#586
Merged
Conversation
Add support for Go 1.23 iterator functions that can be used with the range statement. This enables custom iterators with signatures: - func(yield func() bool) - 0 values - func(yield func(V) bool) - 1 value - func(yield func(K, V) bool) - 2 values The checkIteratorFunc function detects these signatures and returns appropriate key/value types for the range statement. Fixes goplus/xgo#2607 Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: xushiwei <396972+xushiwei@users.noreply.github.com>
Add three test cases for Go 1.23 iterator function support:
- TestForRangeFunc0: 0-value iterator (for range foo {})
- TestForRangeFunc1: 1-value iterator (for v := range bar {})
- TestForRangeFunc2: 2-value iterator (for k, v := range weekdays {})
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xushiwei <396972+xushiwei@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #586 +/- ##
==========================================
- Coverage 96.06% 95.91% -0.15%
==========================================
Files 25 25
Lines 6855 6881 +26
==========================================
+ Hits 6585 6600 +15
- Misses 202 208 +6
- Partials 68 73 +5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Requested by @xushiwei
This PR adds support for Go 1.23 iterator functions that can be used with the range statement.
Summary
checkIteratorFunc()function instmt.goto detect iterator function signaturesgetKeyValTypes()to handle*types.Signaturefor iterator functionsIterator Function Signatures
Go 1.23 introduced range-over-function with these signatures:
func(yield func() bool)- 0 valuesfunc(yield func(V) bool)- 1 valuefunc(yield func(K, V) bool)- 2 values (key-value pairs)Examples
Test Cases
TestForRangeFunc0- 0-value iterator (for range foo {})TestForRangeFunc1- 1-value iterator (for v := range bar {})TestForRangeFunc2- 2-value iterator (for k, v := range weekdays {})All tests pass ✅
Fixes goplus/xgo#2607
Generated with codeagent
Co-authored-by: xushiwei 396972+xushiwei@users.noreply.github.com