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