Initial commit: Go 1.23 release state

This commit is contained in:
Vorapol Rinsatitnon
2024-09-21 23:49:08 +10:00
commit 17cd57a668
13231 changed files with 3114330 additions and 0 deletions

453
src/iter/iter.go Normal file
View File

@@ -0,0 +1,453 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package iter provides basic definitions and operations related to
iterators over sequences.
# Iterators
An iterator is a function that passes successive elements of a
sequence to a callback function, conventionally named yield.
The function stops either when the sequence is finished or
when yield returns false, indicating to stop the iteration early.
This package defines [Seq] and [Seq2]
(pronounced like seek—the first syllable of sequence)
as shorthands for iterators that pass 1 or 2 values per sequence element
to yield:
type (
Seq[V any] func(yield func(V) bool)
Seq2[K, V any] func(yield func(K, V) bool)
)
Seq2 represents a sequence of paired values, conventionally key-value
or index-value pairs.
Yield returns true if the iterator should continue with the next
element in the sequence, false if it should stop.
Iterator functions are most often called by a range loop, as in:
func PrintAll[V any](seq iter.Seq[V]) {
for v := range seq {
fmt.Println(v)
}
}
# Naming Conventions
Iterator functions and methods are named for the sequence being walked:
// All returns an iterator over all elements in s.
func (s *Set[V]) All() iter.Seq[V]
The iterator method on a collection type is conventionally named All,
because it iterates a sequence of all the values in the collection.
For a type containing multiple possible sequences, the iterator's name
can indicate which sequence is being provided:
// Cities returns an iterator over the major cities in the country.
func (c *Country) Cities() iter.Seq[*City]
// Languages returns an iterator over the official spoken languages of the country.
func (c *Country) Languages() iter.Seq[string]
If an iterator requires additional configuration, the constructor function
can take additional configuration arguments:
// Scan returns an iterator over key-value pairs with min ≤ key ≤ max.
func (m *Map[K, V]) Scan(min, max K) iter.Seq2[K, V]
// Split returns an iterator over the (possibly-empty) substrings of s
// separated by sep.
func Split(s, sep string) iter.Seq[string]
When there are multiple possible iteration orders, the method name may
indicate that order:
// All returns an iterator over the list from head to tail.
func (l *List[V]) All() iter.Seq[V]
// Backward returns an iterator over the list from tail to head.
func (l *List[V]) Backward() iter.Seq[V]
// Preorder returns an iterator over all nodes of the syntax tree
// beneath (and including) the specified root, in depth-first preorder,
// visiting a parent node before its children.
func Preorder(root Node) iter.Seq[Node]
# Single-Use Iterators
Most iterators provide the ability to walk an entire sequence:
when called, the iterator does any setup necessary to start the
sequence, then calls yield on successive elements of the sequence,
and then cleans up before returning. Calling the iterator again
walks the sequence again.
Some iterators break that convention, providing the ability to walk a
sequence only once. These “single-use iterators” typically report values
from a data stream that cannot be rewound to start over.
Calling the iterator again after stopping early may continue the
stream, but calling it again after the sequence is finished will yield
no values at all. Doc comments for functions or methods that return
single-use iterators should document this fact:
// Lines returns an iterator over lines read from r.
// It returns a single-use iterator.
func (r *Reader) Lines() iter.Seq[string]
# Pulling Values
Functions and methods that accept or return iterators
should use the standard [Seq] or [Seq2] types, to ensure
compatibility with range loops and other iterator adapters.
The standard iterators can be thought of as “push iterators”, which
push values to the yield function.
Sometimes a range loop is not the most natural way to consume values
of the sequence. In this case, [Pull] converts a standard push iterator
to a “pull iterator”, which can be called to pull one value at a time
from the sequence. [Pull] starts an iterator and returns a pair
of functions—next and stop—which return the next value from the iterator
and stop it, respectively.
For example:
// Pairs returns an iterator over successive pairs of values from seq.
func Pairs[V any](seq iter.Seq[V]) iter.Seq2[V, V] {
return func(yield func(V, V) bool) {
next, stop := iter.Pull(seq)
defer stop()
for {
v1, ok1 := next()
if !ok1 {
return
}
v2, ok2 := next()
// If ok2 is false, v2 should be the
// zero value; yield one last pair.
if !yield(v1, v2) {
return
}
if !ok2 {
return
}
}
}
}
If clients do not consume the sequence to completion, they must call stop,
which allows the iterator function to finish and return. As shown in
the example, the conventional way to ensure this is to use defer.
# Standard Library Usage
A few packages in the standard library provide iterator-based APIs,
most notably the [maps] and [slices] packages.
For example, [maps.Keys] returns an iterator over the keys of a map,
while [slices.Sorted] collects the values of an iterator into a slice,
sorts them, and returns the slice, so to iterate over the sorted keys of a map:
for _, key := range slices.Sorted(maps.Keys(m)) {
...
}
# Mutation
Iterators provide only the values of the sequence, not any direct way
to modify it. If an iterator wishes to provide a mechanism for modifying
a sequence during iteration, the usual approach is to define a position type
with the extra operations and then provide an iterator over positions.
For example, a tree implementation might provide:
// Positions returns an iterator over positions in the sequence.
func (t *Tree[V]) Positions() iter.Seq[*Pos]
// A Pos represents a position in the sequence.
// It is only valid during the yield call it is passed to.
type Pos[V any] struct { ... }
// Pos returns the value at the cursor.
func (p *Pos[V]) Value() V
// Delete deletes the value at this point in the iteration.
func (p *Pos[V]) Delete()
// Set changes the value v at the cursor.
func (p *Pos[V]) Set(v V)
And then a client could delete boring values from the tree using:
for p := range t.Positions() {
if boring(p.Value()) {
p.Delete()
}
}
*/
package iter
import (
"internal/race"
"runtime"
"unsafe"
)
// Seq is an iterator over sequences of individual values.
// When called as seq(yield), seq calls yield(v) for each value v in the sequence,
// stopping early if yield returns false.
// See the [iter] package documentation for more details.
type Seq[V any] func(yield func(V) bool)
// Seq2 is an iterator over sequences of pairs of values, most commonly key-value pairs.
// When called as seq(yield), seq calls yield(k, v) for each pair (k, v) in the sequence,
// stopping early if yield returns false.
// See the [iter] package documentation for more details.
type Seq2[K, V any] func(yield func(K, V) bool)
type coro struct{}
//go:linkname newcoro runtime.newcoro
func newcoro(func(*coro)) *coro
//go:linkname coroswitch runtime.coroswitch
func coroswitch(*coro)
// Pull converts the “push-style” iterator sequence seq
// into a “pull-style” iterator accessed by the two functions
// next and stop.
//
// Next returns the next value in the sequence
// and a boolean indicating whether the value is valid.
// When the sequence is over, next returns the zero V and false.
// It is valid to call next after reaching the end of the sequence
// or after calling stop. These calls will continue
// to return the zero V and false.
//
// Stop ends the iteration. It must be called when the caller is
// no longer interested in next values and next has not yet
// signaled that the sequence is over (with a false boolean return).
// It is valid to call stop multiple times and when next has
// already returned false. Typically, callers should “defer stop()”.
//
// It is an error to call next or stop from multiple goroutines
// simultaneously.
//
// If the iterator panics during a call to next (or stop),
// then next (or stop) itself panics with the same value.
func Pull[V any](seq Seq[V]) (next func() (V, bool), stop func()) {
var (
v V
ok bool
done bool
yieldNext bool
racer int
panicValue any
seqDone bool // to detect Goexit
)
c := newcoro(func(c *coro) {
race.Acquire(unsafe.Pointer(&racer))
if done {
race.Release(unsafe.Pointer(&racer))
return
}
yield := func(v1 V) bool {
if done {
return false
}
if !yieldNext {
panic("iter.Pull: yield called again before next")
}
yieldNext = false
v, ok = v1, true
race.Release(unsafe.Pointer(&racer))
coroswitch(c)
race.Acquire(unsafe.Pointer(&racer))
return !done
}
// Recover and propagate panics from seq.
defer func() {
if p := recover(); p != nil {
panicValue = p
} else if !seqDone {
panicValue = goexitPanicValue
}
done = true // Invalidate iterator
race.Release(unsafe.Pointer(&racer))
}()
seq(yield)
var v0 V
v, ok = v0, false
seqDone = true
})
next = func() (v1 V, ok1 bool) {
race.Write(unsafe.Pointer(&racer)) // detect races
if done {
return
}
if yieldNext {
panic("iter.Pull: next called again before yield")
}
yieldNext = true
race.Release(unsafe.Pointer(&racer))
coroswitch(c)
race.Acquire(unsafe.Pointer(&racer))
// Propagate panics and goexits from seq.
if panicValue != nil {
if panicValue == goexitPanicValue {
// Propagate runtime.Goexit from seq.
runtime.Goexit()
} else {
panic(panicValue)
}
}
return v, ok
}
stop = func() {
race.Write(unsafe.Pointer(&racer)) // detect races
if !done {
done = true
race.Release(unsafe.Pointer(&racer))
coroswitch(c)
race.Acquire(unsafe.Pointer(&racer))
// Propagate panics and goexits from seq.
if panicValue != nil {
if panicValue == goexitPanicValue {
// Propagate runtime.Goexit from seq.
runtime.Goexit()
} else {
panic(panicValue)
}
}
}
}
return next, stop
}
// Pull2 converts the “push-style” iterator sequence seq
// into a “pull-style” iterator accessed by the two functions
// next and stop.
//
// Next returns the next pair in the sequence
// and a boolean indicating whether the pair is valid.
// When the sequence is over, next returns a pair of zero values and false.
// It is valid to call next after reaching the end of the sequence
// or after calling stop. These calls will continue
// to return a pair of zero values and false.
//
// Stop ends the iteration. It must be called when the caller is
// no longer interested in next values and next has not yet
// signaled that the sequence is over (with a false boolean return).
// It is valid to call stop multiple times and when next has
// already returned false. Typically, callers should “defer stop()”.
//
// It is an error to call next or stop from multiple goroutines
// simultaneously.
//
// If the iterator panics during a call to next (or stop),
// then next (or stop) itself panics with the same value.
func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) {
var (
k K
v V
ok bool
done bool
yieldNext bool
racer int
panicValue any
seqDone bool
)
c := newcoro(func(c *coro) {
race.Acquire(unsafe.Pointer(&racer))
if done {
race.Release(unsafe.Pointer(&racer))
return
}
yield := func(k1 K, v1 V) bool {
if done {
return false
}
if !yieldNext {
panic("iter.Pull2: yield called again before next")
}
yieldNext = false
k, v, ok = k1, v1, true
race.Release(unsafe.Pointer(&racer))
coroswitch(c)
race.Acquire(unsafe.Pointer(&racer))
return !done
}
// Recover and propagate panics from seq.
defer func() {
if p := recover(); p != nil {
panicValue = p
} else if !seqDone {
panicValue = goexitPanicValue
}
done = true // Invalidate iterator.
race.Release(unsafe.Pointer(&racer))
}()
seq(yield)
var k0 K
var v0 V
k, v, ok = k0, v0, false
seqDone = true
})
next = func() (k1 K, v1 V, ok1 bool) {
race.Write(unsafe.Pointer(&racer)) // detect races
if done {
return
}
if yieldNext {
panic("iter.Pull2: next called again before yield")
}
yieldNext = true
race.Release(unsafe.Pointer(&racer))
coroswitch(c)
race.Acquire(unsafe.Pointer(&racer))
// Propagate panics and goexits from seq.
if panicValue != nil {
if panicValue == goexitPanicValue {
// Propagate runtime.Goexit from seq.
runtime.Goexit()
} else {
panic(panicValue)
}
}
return k, v, ok
}
stop = func() {
race.Write(unsafe.Pointer(&racer)) // detect races
if !done {
done = true
race.Release(unsafe.Pointer(&racer))
coroswitch(c)
race.Acquire(unsafe.Pointer(&racer))
// Propagate panics and goexits from seq.
if panicValue != nil {
if panicValue == goexitPanicValue {
// Propagate runtime.Goexit from seq.
runtime.Goexit()
} else {
panic(panicValue)
}
}
}
}
return next, stop
}
// goexitPanicValue is a sentinel value indicating that an iterator
// exited via runtime.Goexit.
var goexitPanicValue any = new(int)

485
src/iter/pull_test.go Normal file
View File

@@ -0,0 +1,485 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package iter_test
import (
"fmt"
. "iter"
"runtime"
"testing"
)
func count(n int) Seq[int] {
return func(yield func(int) bool) {
for i := range n {
if !yield(i) {
break
}
}
}
}
func squares(n int) Seq2[int, int64] {
return func(yield func(int, int64) bool) {
for i := range n {
if !yield(i, int64(i)*int64(i)) {
break
}
}
}
}
func TestPull(t *testing.T) {
for end := 0; end <= 3; end++ {
t.Run(fmt.Sprint(end), func(t *testing.T) {
ng := stableNumGoroutine()
wantNG := func(want int) {
if xg := runtime.NumGoroutine() - ng; xg != want {
t.Helper()
t.Errorf("have %d extra goroutines, want %d", xg, want)
}
}
wantNG(0)
next, stop := Pull(count(3))
wantNG(1)
for i := range end {
v, ok := next()
if v != i || ok != true {
t.Fatalf("next() = %d, %v, want %d, %v", v, ok, i, true)
}
wantNG(1)
}
wantNG(1)
if end < 3 {
stop()
wantNG(0)
}
for range 2 {
v, ok := next()
if v != 0 || ok != false {
t.Fatalf("next() = %d, %v, want %d, %v", v, ok, 0, false)
}
wantNG(0)
}
wantNG(0)
stop()
stop()
stop()
wantNG(0)
})
}
}
func TestPull2(t *testing.T) {
for end := 0; end <= 3; end++ {
t.Run(fmt.Sprint(end), func(t *testing.T) {
ng := stableNumGoroutine()
wantNG := func(want int) {
if xg := runtime.NumGoroutine() - ng; xg != want {
t.Helper()
t.Errorf("have %d extra goroutines, want %d", xg, want)
}
}
wantNG(0)
next, stop := Pull2(squares(3))
wantNG(1)
for i := range end {
k, v, ok := next()
if k != i || v != int64(i*i) || ok != true {
t.Fatalf("next() = %d, %d, %v, want %d, %d, %v", k, v, ok, i, i*i, true)
}
wantNG(1)
}
wantNG(1)
if end < 3 {
stop()
wantNG(0)
}
for range 2 {
k, v, ok := next()
if v != 0 || ok != false {
t.Fatalf("next() = %d, %d, %v, want %d, %d, %v", k, v, ok, 0, 0, false)
}
wantNG(0)
}
wantNG(0)
stop()
stop()
stop()
wantNG(0)
})
}
}
// stableNumGoroutine is like NumGoroutine but tries to ensure stability of
// the value by letting any exiting goroutines finish exiting.
func stableNumGoroutine() int {
// The idea behind stablizing the value of NumGoroutine is to
// see the same value enough times in a row in between calls to
// runtime.Gosched. With GOMAXPROCS=1, we're trying to make sure
// that other goroutines run, so that they reach a stable point.
// It's not guaranteed, because it is still possible for a goroutine
// to Gosched back into itself, so we require NumGoroutine to be
// the same 100 times in a row. This should be more than enough to
// ensure all goroutines get a chance to run to completion (or to
// some block point) for a small group of test goroutines.
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
c := 0
ng := runtime.NumGoroutine()
for i := 0; i < 1000; i++ {
nng := runtime.NumGoroutine()
if nng == ng {
c++
} else {
c = 0
ng = nng
}
if c >= 100 {
// The same value 100 times in a row is good enough.
return ng
}
runtime.Gosched()
}
panic("failed to stabilize NumGoroutine after 1000 iterations")
}
func TestPullDoubleNext(t *testing.T) {
next, _ := Pull(doDoubleNext())
nextSlot = next
next()
if nextSlot != nil {
t.Fatal("double next did not fail")
}
}
var nextSlot func() (int, bool)
func doDoubleNext() Seq[int] {
return func(_ func(int) bool) {
defer func() {
if recover() != nil {
nextSlot = nil
}
}()
nextSlot()
}
}
func TestPullDoubleNext2(t *testing.T) {
next, _ := Pull2(doDoubleNext2())
nextSlot2 = next
next()
if nextSlot2 != nil {
t.Fatal("double next did not fail")
}
}
var nextSlot2 func() (int, int, bool)
func doDoubleNext2() Seq2[int, int] {
return func(_ func(int, int) bool) {
defer func() {
if recover() != nil {
nextSlot2 = nil
}
}()
nextSlot2()
}
}
func TestPullDoubleYield(t *testing.T) {
_, stop := Pull(storeYield())
defer func() {
if recover() != nil {
yieldSlot = nil
}
stop()
}()
yieldSlot(5)
if yieldSlot != nil {
t.Fatal("double yield did not fail")
}
}
func storeYield() Seq[int] {
return func(yield func(int) bool) {
yieldSlot = yield
if !yield(5) {
return
}
}
}
var yieldSlot func(int) bool
func TestPullDoubleYield2(t *testing.T) {
_, stop := Pull2(storeYield2())
defer func() {
if recover() != nil {
yieldSlot2 = nil
}
stop()
}()
yieldSlot2(23, 77)
if yieldSlot2 != nil {
t.Fatal("double yield did not fail")
}
}
func storeYield2() Seq2[int, int] {
return func(yield func(int, int) bool) {
yieldSlot2 = yield
if !yield(23, 77) {
return
}
}
}
var yieldSlot2 func(int, int) bool
func TestPullPanic(t *testing.T) {
t.Run("next", func(t *testing.T) {
next, stop := Pull(panicSeq())
if !panicsWith("boom", func() { next() }) {
t.Fatal("failed to propagate panic on first next")
}
// Make sure we don't panic again if we try to call next or stop.
if _, ok := next(); ok {
t.Fatal("next returned true after iterator panicked")
}
// Calling stop again should be a no-op.
stop()
})
t.Run("stop", func(t *testing.T) {
next, stop := Pull(panicCleanupSeq())
x, ok := next()
if !ok || x != 55 {
t.Fatalf("expected (55, true) from next, got (%d, %t)", x, ok)
}
if !panicsWith("boom", func() { stop() }) {
t.Fatal("failed to propagate panic on stop")
}
// Make sure we don't panic again if we try to call next or stop.
if _, ok := next(); ok {
t.Fatal("next returned true after iterator panicked")
}
// Calling stop again should be a no-op.
stop()
})
}
func panicSeq() Seq[int] {
return func(yield func(int) bool) {
panic("boom")
}
}
func panicCleanupSeq() Seq[int] {
return func(yield func(int) bool) {
for {
if !yield(55) {
panic("boom")
}
}
}
}
func TestPull2Panic(t *testing.T) {
t.Run("next", func(t *testing.T) {
next, stop := Pull2(panicSeq2())
if !panicsWith("boom", func() { next() }) {
t.Fatal("failed to propagate panic on first next")
}
// Make sure we don't panic again if we try to call next or stop.
if _, _, ok := next(); ok {
t.Fatal("next returned true after iterator panicked")
}
// Calling stop again should be a no-op.
stop()
})
t.Run("stop", func(t *testing.T) {
next, stop := Pull2(panicCleanupSeq2())
x, y, ok := next()
if !ok || x != 55 || y != 100 {
t.Fatalf("expected (55, 100, true) from next, got (%d, %d, %t)", x, y, ok)
}
if !panicsWith("boom", func() { stop() }) {
t.Fatal("failed to propagate panic on stop")
}
// Make sure we don't panic again if we try to call next or stop.
if _, _, ok := next(); ok {
t.Fatal("next returned true after iterator panicked")
}
// Calling stop again should be a no-op.
stop()
})
}
func panicSeq2() Seq2[int, int] {
return func(yield func(int, int) bool) {
panic("boom")
}
}
func panicCleanupSeq2() Seq2[int, int] {
return func(yield func(int, int) bool) {
for {
if !yield(55, 100) {
panic("boom")
}
}
}
}
func panicsWith(v any, f func()) (panicked bool) {
defer func() {
if r := recover(); r != nil {
if r != v {
panic(r)
}
panicked = true
}
}()
f()
return
}
func TestPullGoexit(t *testing.T) {
t.Run("next", func(t *testing.T) {
var next func() (int, bool)
var stop func()
if !goexits(t, func() {
next, stop = Pull(goexitSeq())
next()
}) {
t.Fatal("failed to Goexit from next")
}
if x, ok := next(); x != 0 || ok {
t.Fatal("iterator returned valid value after iterator Goexited")
}
stop()
})
t.Run("stop", func(t *testing.T) {
next, stop := Pull(goexitCleanupSeq())
x, ok := next()
if !ok || x != 55 {
t.Fatalf("expected (55, true) from next, got (%d, %t)", x, ok)
}
if !goexits(t, func() {
stop()
}) {
t.Fatal("failed to Goexit from stop")
}
// Make sure we don't panic again if we try to call next or stop.
if x, ok := next(); x != 0 || ok {
t.Fatal("next returned true or non-zero value after iterator Goexited")
}
// Calling stop again should be a no-op.
stop()
})
}
func goexitSeq() Seq[int] {
return func(yield func(int) bool) {
runtime.Goexit()
}
}
func goexitCleanupSeq() Seq[int] {
return func(yield func(int) bool) {
for {
if !yield(55) {
runtime.Goexit()
}
}
}
}
func TestPull2Goexit(t *testing.T) {
t.Run("next", func(t *testing.T) {
var next func() (int, int, bool)
var stop func()
if !goexits(t, func() {
next, stop = Pull2(goexitSeq2())
next()
}) {
t.Fatal("failed to Goexit from next")
}
if x, y, ok := next(); x != 0 || y != 0 || ok {
t.Fatal("iterator returned valid value after iterator Goexited")
}
stop()
})
t.Run("stop", func(t *testing.T) {
next, stop := Pull2(goexitCleanupSeq2())
x, y, ok := next()
if !ok || x != 55 || y != 100 {
t.Fatalf("expected (55, 100, true) from next, got (%d, %d, %t)", x, y, ok)
}
if !goexits(t, func() {
stop()
}) {
t.Fatal("failed to Goexit from stop")
}
// Make sure we don't panic again if we try to call next or stop.
if x, y, ok := next(); x != 0 || y != 0 || ok {
t.Fatal("next returned true or non-zero after iterator Goexited")
}
// Calling stop again should be a no-op.
stop()
})
}
func goexitSeq2() Seq2[int, int] {
return func(yield func(int, int) bool) {
runtime.Goexit()
}
}
func goexitCleanupSeq2() Seq2[int, int] {
return func(yield func(int, int) bool) {
for {
if !yield(55, 100) {
runtime.Goexit()
}
}
}
}
func goexits(t *testing.T, f func()) bool {
t.Helper()
exit := make(chan bool)
go func() {
cleanExit := false
defer func() {
exit <- recover() == nil && !cleanExit
}()
f()
cleanExit = true
}()
return <-exit
}
func TestPullImmediateStop(t *testing.T) {
next, stop := Pull(panicSeq())
stop()
// Make sure we don't panic if we try to call next or stop.
if _, ok := next(); ok {
t.Fatal("next returned true after iterator was stopped")
}
}
func TestPull2ImmediateStop(t *testing.T) {
next, stop := Pull2(panicSeq2())
stop()
// Make sure we don't panic if we try to call next or stop.
if _, _, ok := next(); ok {
t.Fatal("next returned true after iterator was stopped")
}
}