Skip to content

Commit d477ef3

Browse files
committed
go/analysis/passes/atomic: add a test that uses typeparams
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 <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 2dc2755 commit d477ef3

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

go/analysis/passes/atomic/atomic_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import (
99

1010
"golang.org/x/tools/go/analysis/analysistest"
1111
"golang.org/x/tools/go/analysis/passes/atomic"
12+
"golang.org/x/tools/internal/typeparams"
1213
)
1314

1415
func Test(t *testing.T) {
1516
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, atomic.Analyzer, "a")
17+
tests := []string{"a"}
18+
if typeparams.Enabled {
19+
tests = append(tests, "typeparams")
20+
}
21+
analysistest.Run(t, testdata, atomic.Analyzer, tests...)
1722
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// This file contains tests for the atomic checker.
6+
7+
package a
8+
9+
import (
10+
"sync/atomic"
11+
)
12+
13+
type Subtractable interface {
14+
~int64
15+
}
16+
17+
func Sub[T Subtractable](addr *T, delta T) T {
18+
// the followings result in type errors, but doesn't stop this vet check
19+
*addr = atomic.AddInt64(addr, -delta) // want "direct assignment to atomic value"
20+
*addr = atomic.AddUintptr(addr, delta) // want "direct assignment to atomic value"
21+
atomic.AddInt64() // vet ignores it
22+
return *addr
23+
}
24+
25+
type _S[T Subtractable] struct {
26+
x *T
27+
}
28+
29+
func (v _S) AddInt64(_ *int64, delta int64) int64 {
30+
*v.x = atomic.AddInt64(v.x, delta) // want "direct assignment to atomic value"
31+
return *v.x
32+
}
33+
34+
func NonAtomicInt64() {
35+
var atomic _S[int64]
36+
*atomic.x = atomic.AddInt64(atomic.x, 123) // ok; AddInt64 is not sync/atomic.AddInt64.
37+
}

0 commit comments

Comments
 (0)