all: modernize interface{} -> any
Produced with the command:
$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -any -test -fix ./...
Change-Id: I09993badc57974a708d348a492ba3ea39f27052e
Reviewed-on: https://go-review.googlesource.com/c/sync/+/744340
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
diff --git a/singleflight/singleflight.go b/singleflight/singleflight.go
index 4051830..90ca138 100644
--- a/singleflight/singleflight.go
+++ b/singleflight/singleflight.go
@@ -22,7 +22,7 @@
// A panicError is an arbitrary value recovered from a panic
// with the stack trace during the execution of given function.
type panicError struct {
- value interface{}
+ value any
stack []byte
}
@@ -40,7 +40,7 @@
return err
}
-func newPanicError(v interface{}) error {
+func newPanicError(v any) error {
stack := debug.Stack()
// The first line of the stack trace is of the form "goroutine N [status]:"
@@ -58,7 +58,7 @@
// These fields are written once before the WaitGroup is done
// and are only read after the WaitGroup is done.
- val interface{}
+ val any
err error
// These fields are read and written with the singleflight
@@ -78,7 +78,7 @@
// Result holds the results of Do, so they can be passed
// on a channel.
type Result struct {
- Val interface{}
+ Val any
Err error
Shared bool
}
@@ -88,7 +88,7 @@
// time. If a duplicate comes in, the duplicate caller waits for the
// original to complete and receives the same results.
// The return value shared indicates whether v was given to multiple callers.
-func (g *Group) Do(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool) {
+func (g *Group) Do(key string, fn func() (any, error)) (v any, err error, shared bool) {
g.mu.Lock()
if g.m == nil {
g.m = make(map[string]*call)
@@ -118,7 +118,7 @@
// results when they are ready.
//
// The returned channel will not be closed.
-func (g *Group) DoChan(key string, fn func() (interface{}, error)) <-chan Result {
+func (g *Group) DoChan(key string, fn func() (any, error)) <-chan Result {
ch := make(chan Result, 1)
g.mu.Lock()
if g.m == nil {
@@ -141,7 +141,7 @@
}
// doCall handles the single call for a key.
-func (g *Group) doCall(c *call, key string, fn func() (interface{}, error)) {
+func (g *Group) doCall(c *call, key string, fn func() (any, error)) {
normalReturn := false
recovered := false
diff --git a/singleflight/singleflight_test.go b/singleflight/singleflight_test.go
index 853ec42..9d8edc8 100644
--- a/singleflight/singleflight_test.go
+++ b/singleflight/singleflight_test.go
@@ -30,7 +30,7 @@
testCases := []struct {
name string
- panicValue interface{}
+ panicValue any
wrappedErrorType bool
}{
{
@@ -51,7 +51,7 @@
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
- var recovered interface{}
+ var recovered any
group := &Group{}
@@ -61,7 +61,7 @@
t.Logf("after panic(%#v) in group.Do, recovered %#v", tc.panicValue, recovered)
}()
- _, _, _ = group.Do(tc.name, func() (interface{}, error) {
+ _, _, _ = group.Do(tc.name, func() (any, error) {
panic(tc.panicValue)
})
}()
@@ -84,7 +84,7 @@
func TestDo(t *testing.T) {
var g Group
- v, err, _ := g.Do("key", func() (interface{}, error) {
+ v, err, _ := g.Do("key", func() (any, error) {
return "bar", nil
})
if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want {
@@ -98,7 +98,7 @@
func TestDoErr(t *testing.T) {
var g Group
someErr := errors.New("Some error")
- v, err, _ := g.Do("key", func() (interface{}, error) {
+ v, err, _ := g.Do("key", func() (any, error) {
return nil, someErr
})
if err != someErr {
@@ -114,7 +114,7 @@
var wg1, wg2 sync.WaitGroup
c := make(chan string, 1)
var calls int32
- fn := func() (interface{}, error) {
+ fn := func() (any, error) {
if atomic.AddInt32(&calls, 1) == 1 {
// First invocation.
wg1.Done()
@@ -167,7 +167,7 @@
)
go func() {
- g.Do("key", func() (i interface{}, e error) {
+ g.Do("key", func() (i any, e error) {
close(firstStarted)
<-unblockFirst
close(firstFinished)
@@ -178,7 +178,7 @@
g.Forget("key")
unblockSecond := make(chan struct{})
- secondResult := g.DoChan("key", func() (i interface{}, e error) {
+ secondResult := g.DoChan("key", func() (i any, e error) {
<-unblockSecond
return 2, nil
})
@@ -186,7 +186,7 @@
close(unblockFirst)
<-firstFinished
- thirdResult := g.DoChan("key", func() (i interface{}, e error) {
+ thirdResult := g.DoChan("key", func() (i any, e error) {
return 3, nil
})
@@ -200,7 +200,7 @@
func TestDoChan(t *testing.T) {
var g Group
- ch := g.DoChan("key", func() (interface{}, error) {
+ ch := g.DoChan("key", func() (any, error) {
return "bar", nil
})
@@ -219,7 +219,7 @@
// See https://github.com/golang/go/issues/41133
func TestPanicDo(t *testing.T) {
var g Group
- fn := func() (interface{}, error) {
+ fn := func() (any, error) {
panic("invalid memory address or nil pointer dereference")
}
@@ -256,7 +256,7 @@
func TestGoexitDo(t *testing.T) {
var g Group
- fn := func() (interface{}, error) {
+ fn := func() (any, error) {
runtime.Goexit()
return nil, nil
}
@@ -310,7 +310,7 @@
}()
g := new(Group)
- ch := g.DoChan("", func() (interface{}, error) {
+ ch := g.DoChan("", func() (any, error) {
panic("Panicking in DoChan")
})
<-ch
@@ -351,7 +351,7 @@
defer func() {
recover()
}()
- g.Do("", func() (interface{}, error) {
+ g.Do("", func() (any, error) {
close(blocked)
<-unblock
panic("Panicking in Do")
@@ -359,7 +359,7 @@
}()
<-blocked
- ch := g.DoChan("", func() (interface{}, error) {
+ ch := g.DoChan("", func() (any, error) {
panic("DoChan unexpectedly executed callback")
})
close(unblock)
@@ -395,11 +395,11 @@
g := new(Group)
block := make(chan struct{})
- res1c := g.DoChan("key", func() (interface{}, error) {
+ res1c := g.DoChan("key", func() (any, error) {
<-block
return "func 1", nil
})
- res2c := g.DoChan("key", func() (interface{}, error) {
+ res2c := g.DoChan("key", func() (any, error) {
<-block
return "func 2", nil
})
diff --git a/syncmap/map_bench_test.go b/syncmap/map_bench_test.go
index b279b4f..9fb7181 100644
--- a/syncmap/map_bench_test.go
+++ b/syncmap/map_bench_test.go
@@ -157,7 +157,7 @@
perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) {
for ; pb.Next(); i++ {
- m.Range(func(_, _ interface{}) bool { return true })
+ m.Range(func(_, _ any) bool { return true })
}
},
})
@@ -204,7 +204,7 @@
m.Load(i)
if i%mapSize == 0 {
- m.Range(func(k, _ interface{}) bool {
+ m.Range(func(k, _ any) bool {
m.Delete(k)
return false
})
diff --git a/syncmap/map_reference_test.go b/syncmap/map_reference_test.go
index 923c51b..8453174 100644
--- a/syncmap/map_reference_test.go
+++ b/syncmap/map_reference_test.go
@@ -13,42 +13,42 @@
// mapInterface is the interface Map implements.
type mapInterface interface {
- Load(interface{}) (interface{}, bool)
- Store(key, value interface{})
- LoadOrStore(key, value interface{}) (actual interface{}, loaded bool)
- Delete(interface{})
- Range(func(key, value interface{}) (shouldContinue bool))
+ Load(any) (any, bool)
+ Store(key, value any)
+ LoadOrStore(key, value any) (actual any, loaded bool)
+ Delete(any)
+ Range(func(key, value any) (shouldContinue bool))
}
// RWMutexMap is an implementation of mapInterface using a sync.RWMutex.
type RWMutexMap struct {
mu sync.RWMutex
- dirty map[interface{}]interface{}
+ dirty map[any]any
}
-func (m *RWMutexMap) Load(key interface{}) (value interface{}, ok bool) {
+func (m *RWMutexMap) Load(key any) (value any, ok bool) {
m.mu.RLock()
value, ok = m.dirty[key]
m.mu.RUnlock()
return
}
-func (m *RWMutexMap) Store(key, value interface{}) {
+func (m *RWMutexMap) Store(key, value any) {
m.mu.Lock()
if m.dirty == nil {
- m.dirty = make(map[interface{}]interface{})
+ m.dirty = make(map[any]any)
}
m.dirty[key] = value
m.mu.Unlock()
}
-func (m *RWMutexMap) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) {
+func (m *RWMutexMap) LoadOrStore(key, value any) (actual any, loaded bool) {
m.mu.Lock()
actual, loaded = m.dirty[key]
if !loaded {
actual = value
if m.dirty == nil {
- m.dirty = make(map[interface{}]interface{})
+ m.dirty = make(map[any]any)
}
m.dirty[key] = value
}
@@ -56,15 +56,15 @@
return actual, loaded
}
-func (m *RWMutexMap) Delete(key interface{}) {
+func (m *RWMutexMap) Delete(key any) {
m.mu.Lock()
delete(m.dirty, key)
m.mu.Unlock()
}
-func (m *RWMutexMap) Range(f func(key, value interface{}) (shouldContinue bool)) {
+func (m *RWMutexMap) Range(f func(key, value any) (shouldContinue bool)) {
m.mu.RLock()
- keys := make([]interface{}, 0, len(m.dirty))
+ keys := make([]any, 0, len(m.dirty))
for k := range m.dirty {
keys = append(keys, k)
}
@@ -89,13 +89,13 @@
clean atomic.Value
}
-func (m *DeepCopyMap) Load(key interface{}) (value interface{}, ok bool) {
- clean, _ := m.clean.Load().(map[interface{}]interface{})
+func (m *DeepCopyMap) Load(key any) (value any, ok bool) {
+ clean, _ := m.clean.Load().(map[any]any)
value, ok = clean[key]
return value, ok
}
-func (m *DeepCopyMap) Store(key, value interface{}) {
+func (m *DeepCopyMap) Store(key, value any) {
m.mu.Lock()
dirty := m.dirty()
dirty[key] = value
@@ -103,8 +103,8 @@
m.mu.Unlock()
}
-func (m *DeepCopyMap) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) {
- clean, _ := m.clean.Load().(map[interface{}]interface{})
+func (m *DeepCopyMap) LoadOrStore(key, value any) (actual any, loaded bool) {
+ clean, _ := m.clean.Load().(map[any]any)
actual, loaded = clean[key]
if loaded {
return actual, loaded
@@ -112,7 +112,7 @@
m.mu.Lock()
// Reload clean in case it changed while we were waiting on m.mu.
- clean, _ = m.clean.Load().(map[interface{}]interface{})
+ clean, _ = m.clean.Load().(map[any]any)
actual, loaded = clean[key]
if !loaded {
dirty := m.dirty()
@@ -124,7 +124,7 @@
return actual, loaded
}
-func (m *DeepCopyMap) Delete(key interface{}) {
+func (m *DeepCopyMap) Delete(key any) {
m.mu.Lock()
dirty := m.dirty()
delete(dirty, key)
@@ -132,8 +132,8 @@
m.mu.Unlock()
}
-func (m *DeepCopyMap) Range(f func(key, value interface{}) (shouldContinue bool)) {
- clean, _ := m.clean.Load().(map[interface{}]interface{})
+func (m *DeepCopyMap) Range(f func(key, value any) (shouldContinue bool)) {
+ clean, _ := m.clean.Load().(map[any]any)
for k, v := range clean {
if !f(k, v) {
break
@@ -141,9 +141,9 @@
}
}
-func (m *DeepCopyMap) dirty() map[interface{}]interface{} {
- clean, _ := m.clean.Load().(map[interface{}]interface{})
- dirty := make(map[interface{}]interface{}, len(clean)+1)
+func (m *DeepCopyMap) dirty() map[any]any {
+ clean, _ := m.clean.Load().(map[any]any)
+ dirty := make(map[any]any, len(clean)+1)
for k, v := range clean {
dirty[k] = v
}
diff --git a/syncmap/map_test.go b/syncmap/map_test.go
index bf69f50..912d09c 100644
--- a/syncmap/map_test.go
+++ b/syncmap/map_test.go
@@ -29,10 +29,10 @@
// mapCall is a quick.Generator for calls on mapInterface.
type mapCall struct {
op mapOp
- k, v interface{}
+ k, v any
}
-func (c mapCall) apply(m mapInterface) (interface{}, bool) {
+func (c mapCall) apply(m mapInterface) (any, bool) {
switch c.op {
case opLoad:
return m.Load(c.k)
@@ -50,11 +50,11 @@
}
type mapResult struct {
- value interface{}
+ value any
ok bool
}
-func randValue(r *rand.Rand) interface{} {
+func randValue(r *rand.Rand) any {
b := make([]byte, r.Intn(4))
for i := range b {
b[i] = 'a' + byte(rand.Intn(26))
@@ -71,14 +71,14 @@
return reflect.ValueOf(c)
}
-func applyCalls(m mapInterface, calls []mapCall) (results []mapResult, final map[interface{}]interface{}) {
+func applyCalls(m mapInterface, calls []mapCall) (results []mapResult, final map[any]any) {
for _, c := range calls {
v, ok := c.apply(m)
results = append(results, mapResult{v, ok})
}
- final = make(map[interface{}]interface{})
- m.Range(func(k, v interface{}) bool {
+ final = make(map[any]any)
+ m.Range(func(k, v any) bool {
final[k] = v
return true
})
@@ -86,15 +86,15 @@
return results, final
}
-func applyMap(calls []mapCall) ([]mapResult, map[interface{}]interface{}) {
+func applyMap(calls []mapCall) ([]mapResult, map[any]any) {
return applyCalls(new(syncmap.Map), calls)
}
-func applyRWMutexMap(calls []mapCall) ([]mapResult, map[interface{}]interface{}) {
+func applyRWMutexMap(calls []mapCall) ([]mapResult, map[any]any) {
return applyCalls(new(RWMutexMap), calls)
}
-func applyDeepCopyMap(calls []mapCall) ([]mapResult, map[interface{}]interface{}) {
+func applyDeepCopyMap(calls []mapCall) ([]mapResult, map[any]any) {
return applyCalls(new(DeepCopyMap), calls)
}
@@ -153,7 +153,7 @@
for n := iters; n > 0; n-- {
seen := make(map[int64]bool, mapSize)
- m.Range(func(ki, vi interface{}) bool {
+ m.Range(func(ki, vi any) bool {
k, v := ki.(int64), vi.(int64)
if v%k != 0 {
t.Fatalf("while Storing multiples of %v, Range saw value %v", k, v)