Skip to content

cl: support range over function types (Go 1.23)#586

Merged
xushiwei merged 2 commits intomainfrom
xgopilot/range-over-func
Feb 9, 2026
Merged

cl: support range over function types (Go 1.23)#586
xushiwei merged 2 commits intomainfrom
xgopilot/range-over-func

Conversation

@xgopilot
Copy link
Contributor

@xgopilot xgopilot bot commented Feb 9, 2026

Requested by @xushiwei

This PR adds support for Go 1.23 iterator functions that can be used with the range statement.

Summary

  • Added checkIteratorFunc() function in stmt.go to detect iterator function signatures
  • Modified getKeyValTypes() to handle *types.Signature for iterator functions
  • Added 3 test cases covering all iterator variants

Iterator Function Signatures

Go 1.23 introduced range-over-function with these signatures:

  • func(yield func() bool) - 0 values
  • func(yield func(V) bool) - 1 value
  • func(yield func(K, V) bool) - 2 values (key-value pairs)

Examples

func bar(yield func(v string) bool) {
    yield("Hello")
    yield("World")
}

for v := range bar {
    println(v)  // Output: Hello, World
}

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

xgopilot bot and others added 2 commits February 9, 2026 01:31
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
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 57.69231% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.91%. Comparing base (e9785e5) to head (f42e14c).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
stmt.go 57.69% 6 Missing and 5 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xushiwei xushiwei merged commit f42e14c into main Feb 9, 2026
42 of 44 checks passed
@xgopilot xgopilot bot deleted the xgopilot/range-over-func branch February 9, 2026 14:04
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.

Range Over Function Types

1 participant