Skip to content

Commit

Permalink
go/analysis/passes/atomic: add a test that uses typeparams
Browse files Browse the repository at this point in the history
This atomic check simply inspects whether sync/atomic.Add*
are used and LHS looks identical to the first arg of the call.
In the current implementation, this does not require handling
of type params. This test shows the addition of typeparams
doesn't crash the vet.

Update golang/go#48704

Change-Id: I79f9d782595f6bf2db82237afdbef1ffdf6f808e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/353550
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
hyangah committed Oct 5, 2021
1 parent 2dc2755 commit d477ef3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion go/analysis/passes/atomic/atomic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import (

"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/atomic"
"golang.org/x/tools/internal/typeparams"
)

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, atomic.Analyzer, "a")
tests := []string{"a"}
if typeparams.Enabled {
tests = append(tests, "typeparams")
}
analysistest.Run(t, testdata, atomic.Analyzer, tests...)
}
37 changes: 37 additions & 0 deletions go/analysis/passes/atomic/testdata/src/typeparams/typeparams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021 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.

// This file contains tests for the atomic checker.

package a

import (
"sync/atomic"
)

type Subtractable interface {
~int64
}

func Sub[T Subtractable](addr *T, delta T) T {
// the followings result in type errors, but doesn't stop this vet check
*addr = atomic.AddInt64(addr, -delta) // want "direct assignment to atomic value"
*addr = atomic.AddUintptr(addr, delta) // want "direct assignment to atomic value"
atomic.AddInt64() // vet ignores it
return *addr
}

type _S[T Subtractable] struct {
x *T
}

func (v _S) AddInt64(_ *int64, delta int64) int64 {
*v.x = atomic.AddInt64(v.x, delta) // want "direct assignment to atomic value"
return *v.x
}

func NonAtomicInt64() {
var atomic _S[int64]
*atomic.x = atomic.AddInt64(atomic.x, 123) // ok; AddInt64 is not sync/atomic.AddInt64.
}

0 comments on commit d477ef3

Please sign in to comment.