github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/src/cmd/vet/testdata/copylock.go (about)

     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 "function call 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  // SyncTypesCheck checks copying of sync.* types except sync.Mutex
    92  func SyncTypesCheck() {
    93  	// sync.RWMutex copying
    94  	var rwmuX sync.RWMutex
    95  	var rwmuXX = sync.RWMutex{}
    96  	rwmuX1 := new(sync.RWMutex)
    97  	rwmuY := rwmuX     // ERROR "assignment copies lock value to rwmuY: sync.RWMutex"
    98  	rwmuY = rwmuX      // ERROR "assignment copies lock value to rwmuY: sync.RWMutex"
    99  	var rwmuYY = rwmuX // ERROR "variable declaration copies lock value to rwmuYY: sync.RWMutex"
   100  	rwmuP := &rwmuX
   101  	rwmuZ := &sync.RWMutex{}
   102  
   103  	// sync.Cond copying
   104  	var condX sync.Cond
   105  	var condXX = sync.Cond{}
   106  	condX1 := new(sync.Cond)
   107  	condY := condX     // ERROR "assignment copies lock value to condY: sync.Cond contains sync.noCopy"
   108  	condY = condX      // ERROR "assignment copies lock value to condY: sync.Cond contains sync.noCopy"
   109  	var condYY = condX // ERROR "variable declaration copies lock value to condYY: sync.Cond contains sync.noCopy"
   110  	condP := &condX
   111  	condZ := &sync.Cond{
   112  		L: &sync.Mutex{},
   113  	}
   114  	condZ = sync.NewCond(&sync.Mutex{})
   115  
   116  	// sync.WaitGroup copying
   117  	var wgX sync.WaitGroup
   118  	var wgXX = sync.WaitGroup{}
   119  	wgX1 := new(sync.WaitGroup)
   120  	wgY := wgX     // ERROR "assignment copies lock value to wgY: sync.WaitGroup contains sync.noCopy"
   121  	wgY = wgX      // ERROR "assignment copies lock value to wgY: sync.WaitGroup contains sync.noCopy"
   122  	var wgYY = wgX // ERROR "variable declaration copies lock value to wgYY: sync.WaitGroup contains sync.noCopy"
   123  	wgP := &wgX
   124  	wgZ := &sync.WaitGroup{}
   125  
   126  	// sync.Pool copying
   127  	var poolX sync.Pool
   128  	var poolXX = sync.Pool{}
   129  	poolX1 := new(sync.Pool)
   130  	poolY := poolX     // ERROR "assignment copies lock value to poolY: sync.Pool contains sync.noCopy"
   131  	poolY = poolX      // ERROR "assignment copies lock value to poolY: sync.Pool contains sync.noCopy"
   132  	var poolYY = poolX // ERROR "variable declaration copies lock value to poolYY: sync.Pool contains sync.noCopy"
   133  	poolP := &poolX
   134  	poolZ := &sync.Pool{}
   135  
   136  	// sync.Once copying
   137  	var onceX sync.Once
   138  	var onceXX = sync.Once{}
   139  	onceX1 := new(sync.Once)
   140  	onceY := onceX     // ERROR "assignment copies lock value to onceY: sync.Once contains sync.Mutex"
   141  	onceY = onceX      // ERROR "assignment copies lock value to onceY: sync.Once contains sync.Mutex"
   142  	var onceYY = onceX // ERROR "variable declaration copies lock value to onceYY: sync.Once contains sync.Mutex"
   143  	onceP := &onceX
   144  	onceZ := &sync.Once{}
   145  }
   146  
   147  // AtomicTypesCheck checks copying of sync/atomic types
   148  func AtomicTypesCheck() {
   149  	// atomic.Value copying
   150  	var vX atomic.Value
   151  	var vXX = atomic.Value{}
   152  	vX1 := new(atomic.Value)
   153  	vY := vX     // ERROR "assignment copies lock value to vY: sync/atomic.Value contains sync/atomic.noCopy"
   154  	vY = vX      // ERROR "assignment copies lock value to vY: sync/atomic.Value contains sync/atomic.noCopy"
   155  	var vYY = vX // ERROR "variable declaration copies lock value to vYY: sync/atomic.Value contains sync/atomic.noCopy"
   156  	vP := &vX
   157  	vZ := &atomic.Value{}
   158  }