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