github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/cmd/vet/testdata/atomic.go (about)

     1  // Copyright 2013 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 testdata
     8  
     9  import (
    10  	"sync/atomic"
    11  )
    12  
    13  type Counter uint64
    14  
    15  func AtomicTests() {
    16  	x := uint64(1)
    17  	x = atomic.AddUint64(&x, 1)        // ERROR "direct assignment to atomic value"
    18  	_, x = 10, atomic.AddUint64(&x, 1) // ERROR "direct assignment to atomic value"
    19  	x, _ = atomic.AddUint64(&x, 1), 10 // ERROR "direct assignment to atomic value"
    20  
    21  	y := &x
    22  	*y = atomic.AddUint64(y, 1) // ERROR "direct assignment to atomic value"
    23  
    24  	var su struct{ Counter uint64 }
    25  	su.Counter = atomic.AddUint64(&su.Counter, 1) // ERROR "direct assignment to atomic value"
    26  	z1 := atomic.AddUint64(&su.Counter, 1)
    27  	_ = z1 // Avoid err "z declared and not used"
    28  
    29  	var sp struct{ Counter *uint64 }
    30  	*sp.Counter = atomic.AddUint64(sp.Counter, 1) // ERROR "direct assignment to atomic value"
    31  	z2 := atomic.AddUint64(sp.Counter, 1)
    32  	_ = z2 // Avoid err "z declared and not used"
    33  
    34  	au := []uint64{10, 20}
    35  	au[0] = atomic.AddUint64(&au[0], 1) // ERROR "direct assignment to atomic value"
    36  	au[1] = atomic.AddUint64(&au[0], 1)
    37  
    38  	ap := []*uint64{&au[0], &au[1]}
    39  	*ap[0] = atomic.AddUint64(ap[0], 1) // ERROR "direct assignment to atomic value"
    40  	*ap[1] = atomic.AddUint64(ap[0], 1)
    41  
    42  	x = atomic.AddUint64() // Used to make vet crash; now silently ignored.
    43  }