github.com/likebike/go--@v0.0.0-20190911215757-0bd925d16e96/go/src/cmd/vet/testdata/copylock.go (about)

     1  package testdata
     2  
     3  import (
     4  	"sync"
     5  	"sync/atomic"
     6  	"unsafe"
     7  	. "unsafe"
     8  	unsafe1 "unsafe"
     9  )
    10  
    11  func OkFunc() {
    12  	var x *sync.Mutex
    13  	p := x
    14  	var y sync.Mutex
    15  	p = &y
    16  
    17  	var z = sync.Mutex{}
    18  	w := sync.Mutex{}
    19  
    20  	w = sync.Mutex{}
    21  	q := struct{ L sync.Mutex }{
    22  		L: sync.Mutex{},
    23  	}
    24  
    25  	yy := []Tlock{
    26  		Tlock{},
    27  		Tlock{
    28  			once: sync.Once{},
    29  		},
    30  	}
    31  
    32  	nl := new(sync.Mutex)
    33  	mx := make([]sync.Mutex, 10)
    34  	xx := struct{ L *sync.Mutex }{
    35  		L: new(sync.Mutex),
    36  	}
    37  }
    38  
    39  type Tlock struct {
    40  	once sync.Once
    41  }
    42  
    43  func BadFunc() {
    44  	var x *sync.Mutex
    45  	p := x
    46  	var y sync.Mutex
    47  	p = &y
    48  	*p = *x // ERROR "assignment copies lock value to \*p: sync.Mutex"
    49  
    50  	var t Tlock
    51  	var tp *Tlock
    52  	tp = &t
    53  	*tp = t // ERROR "assignment copies lock value to \*tp: testdata.Tlock contains sync.Once contains sync.Mutex"
    54  	t = *tp // ERROR "assignment copies lock value to t: testdata.Tlock contains sync.Once contains sync.Mutex"
    55  
    56  	y := *x   // ERROR "assignment copies lock value to y: sync.Mutex"
    57  	var z = t // ERROR "variable declaration copies lock value to z: testdata.Tlock contains sync.Once contains sync.Mutex"
    58  
    59  	w := struct{ L sync.Mutex }{
    60  		L: *x, // ERROR "literal copies lock value from \*x: sync.Mutex"
    61  	}
    62  	var q = map[int]Tlock{
    63  		1: t,   // ERROR "literal copies lock value from t: testdata.Tlock contains sync.Once contains sync.Mutex"
    64  		2: *tp, // ERROR "literal copies lock value from \*tp: testdata.Tlock contains sync.Once contains sync.Mutex"
    65  	}
    66  	yy := []Tlock{
    67  		t,   // ERROR "literal copies lock value from t: testdata.Tlock contains sync.Once contains sync.Mutex"
    68  		*tp, // ERROR "literal copies lock value from \*tp: testdata.Tlock contains sync.Once contains sync.Mutex"
    69  	}
    70  
    71  	// override 'new' keyword
    72  	new := func(interface{}) {}
    73  	new(t) // ERROR "call of new copies lock value: testdata.Tlock contains sync.Once contains sync.Mutex"
    74  
    75  	// copy of array of locks
    76  	var muA [5]sync.Mutex
    77  	muB := muA        // ERROR "assignment copies lock value to muB: sync.Mutex"
    78  	muA = muB         // ERROR "assignment copies lock value to muA: sync.Mutex"
    79  	muSlice := muA[:] // OK
    80  
    81  	// multidimensional array
    82  	var mmuA [5][5]sync.Mutex
    83  	mmuB := mmuA        // ERROR "assignment copies lock value to mmuB: sync.Mutex"
    84  	mmuA = mmuB         // ERROR "assignment copies lock value to mmuA: sync.Mutex"
    85  	mmuSlice := mmuA[:] // OK
    86  
    87  	// slice copy is ok
    88  	var fmuA [5][][5]sync.Mutex
    89  	fmuB := fmuA        // OK
    90  	fmuA = fmuB         // OK
    91  	fmuSlice := fmuA[:] // OK
    92  }
    93  
    94  func LenAndCapOnLockArrays() {
    95  	var a [5]sync.Mutex
    96  	aLen := len(a) // OK
    97  	aCap := cap(a) // OK
    98  
    99  	// override 'len' and 'cap' keywords
   100  
   101  	len := func(interface{}) {}
   102  	len(a) // ERROR "call of len copies lock value: sync.Mutex"
   103  
   104  	cap := func(interface{}) {}
   105  	cap(a) // ERROR "call of cap copies lock value: sync.Mutex"
   106  }
   107  
   108  func SizeofMutex() {
   109  	var mu sync.Mutex
   110  	unsafe.Sizeof(mu)  // OK
   111  	unsafe1.Sizeof(mu) // OK
   112  	Sizeof(mu)         // OK
   113  	unsafe := struct{ Sizeof func(interface{}) }{}
   114  	unsafe.Sizeof(mu) // ERROR "call of unsafe.Sizeof copies lock value: sync.Mutex"
   115  	Sizeof := func(interface{}) {}
   116  	Sizeof(mu) // ERROR "call of Sizeof copies lock value: sync.Mutex"
   117  }
   118  
   119  // SyncTypesCheck checks copying of sync.* types except sync.Mutex
   120  func SyncTypesCheck() {
   121  	// sync.RWMutex copying
   122  	var rwmuX sync.RWMutex
   123  	var rwmuXX = sync.RWMutex{}
   124  	rwmuX1 := new(sync.RWMutex)
   125  	rwmuY := rwmuX     // ERROR "assignment copies lock value to rwmuY: sync.RWMutex"
   126  	rwmuY = rwmuX      // ERROR "assignment copies lock value to rwmuY: sync.RWMutex"
   127  	var rwmuYY = rwmuX // ERROR "variable declaration copies lock value to rwmuYY: sync.RWMutex"
   128  	rwmuP := &rwmuX
   129  	rwmuZ := &sync.RWMutex{}
   130  
   131  	// sync.Cond copying
   132  	var condX sync.Cond
   133  	var condXX = sync.Cond{}
   134  	condX1 := new(sync.Cond)
   135  	condY := condX     // ERROR "assignment copies lock value to condY: sync.Cond contains sync.noCopy"
   136  	condY = condX      // ERROR "assignment copies lock value to condY: sync.Cond contains sync.noCopy"
   137  	var condYY = condX // ERROR "variable declaration copies lock value to condYY: sync.Cond contains sync.noCopy"
   138  	condP := &condX
   139  	condZ := &sync.Cond{
   140  		L: &sync.Mutex{},
   141  	}
   142  	condZ = sync.NewCond(&sync.Mutex{})
   143  
   144  	// sync.WaitGroup copying
   145  	var wgX sync.WaitGroup
   146  	var wgXX = sync.WaitGroup{}
   147  	wgX1 := new(sync.WaitGroup)
   148  	wgY := wgX     // ERROR "assignment copies lock value to wgY: sync.WaitGroup contains sync.noCopy"
   149  	wgY = wgX      // ERROR "assignment copies lock value to wgY: sync.WaitGroup contains sync.noCopy"
   150  	var wgYY = wgX // ERROR "variable declaration copies lock value to wgYY: sync.WaitGroup contains sync.noCopy"
   151  	wgP := &wgX
   152  	wgZ := &sync.WaitGroup{}
   153  
   154  	// sync.Pool copying
   155  	var poolX sync.Pool
   156  	var poolXX = sync.Pool{}
   157  	poolX1 := new(sync.Pool)
   158  	poolY := poolX     // ERROR "assignment copies lock value to poolY: sync.Pool contains sync.noCopy"
   159  	poolY = poolX      // ERROR "assignment copies lock value to poolY: sync.Pool contains sync.noCopy"
   160  	var poolYY = poolX // ERROR "variable declaration copies lock value to poolYY: sync.Pool contains sync.noCopy"
   161  	poolP := &poolX
   162  	poolZ := &sync.Pool{}
   163  
   164  	// sync.Once copying
   165  	var onceX sync.Once
   166  	var onceXX = sync.Once{}
   167  	onceX1 := new(sync.Once)
   168  	onceY := onceX     // ERROR "assignment copies lock value to onceY: sync.Once contains sync.Mutex"
   169  	onceY = onceX      // ERROR "assignment copies lock value to onceY: sync.Once contains sync.Mutex"
   170  	var onceYY = onceX // ERROR "variable declaration copies lock value to onceYY: sync.Once contains sync.Mutex"
   171  	onceP := &onceX
   172  	onceZ := &sync.Once{}
   173  }
   174  
   175  // AtomicTypesCheck checks copying of sync/atomic types
   176  func AtomicTypesCheck() {
   177  	// atomic.Value copying
   178  	var vX atomic.Value
   179  	var vXX = atomic.Value{}
   180  	vX1 := new(atomic.Value)
   181  	// These are OK because the value has not been used yet.
   182  	// (And vet can't tell whether it has been used, so they're always OK.)
   183  	vY := vX
   184  	vY = vX
   185  	var vYY = vX
   186  	vP := &vX
   187  	vZ := &atomic.Value{}
   188  }