PR tree-optimization/86274 - SEGFAULT when logging std::to_string(NAN)
[official-gcc.git] / libgo / go / cmd / vet / testdata / copylock_func.go
blob280747a3bf46d3b54539a4c0af140f919674e3d7
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.
5 // This file contains tests for the copylock checker's
6 // function declaration analysis.
8 package testdata
10 import "sync"
12 func OkFunc(*sync.Mutex) {}
13 func BadFunc(sync.Mutex) {} // ERROR "BadFunc passes lock by value: sync.Mutex"
14 func BadFunc2(sync.Map) {} // ERROR "BadFunc2 passes lock by value: sync.Map contains sync.Mutex"
15 func OkRet() *sync.Mutex {}
16 func BadRet() sync.Mutex {} // Don't warn about results
18 var (
19 OkClosure = func(*sync.Mutex) {}
20 BadClosure = func(sync.Mutex) {} // ERROR "func passes lock by value: sync.Mutex"
21 BadClosure2 = func(sync.Map) {} // ERROR "func passes lock by value: sync.Map contains sync.Mutex"
24 type EmbeddedRWMutex struct {
25 sync.RWMutex
28 func (*EmbeddedRWMutex) OkMeth() {}
29 func (EmbeddedRWMutex) BadMeth() {} // ERROR "BadMeth passes lock by value: testdata.EmbeddedRWMutex"
30 func OkFunc(e *EmbeddedRWMutex) {}
31 func BadFunc(EmbeddedRWMutex) {} // ERROR "BadFunc passes lock by value: testdata.EmbeddedRWMutex"
32 func OkRet() *EmbeddedRWMutex {}
33 func BadRet() EmbeddedRWMutex {} // Don't warn about results
35 type FieldMutex struct {
36 s sync.Mutex
39 func (*FieldMutex) OkMeth() {}
40 func (FieldMutex) BadMeth() {} // ERROR "BadMeth passes lock by value: testdata.FieldMutex contains sync.Mutex"
41 func OkFunc(*FieldMutex) {}
42 func BadFunc(FieldMutex, int) {} // ERROR "BadFunc passes lock by value: testdata.FieldMutex contains sync.Mutex"
44 type L0 struct {
48 type L1 struct {
49 l L2
52 type L2 struct {
53 sync.Mutex
56 func (*L0) Ok() {}
57 func (L0) Bad() {} // ERROR "Bad passes lock by value: testdata.L0 contains testdata.L1 contains testdata.L2"
59 type EmbeddedMutexPointer struct {
60 s *sync.Mutex // safe to copy this pointer
63 func (*EmbeddedMutexPointer) Ok() {}
64 func (EmbeddedMutexPointer) AlsoOk() {}
65 func StillOk(EmbeddedMutexPointer) {}
66 func LookinGood() EmbeddedMutexPointer {}
68 type EmbeddedLocker struct {
69 sync.Locker // safe to copy interface values
72 func (*EmbeddedLocker) Ok() {}
73 func (EmbeddedLocker) AlsoOk() {}
75 type CustomLock struct{}
77 func (*CustomLock) Lock() {}
78 func (*CustomLock) Unlock() {}
80 func Ok(*CustomLock) {}
81 func Bad(CustomLock) {} // ERROR "Bad passes lock by value: testdata.CustomLock"
83 // Passing lock values into interface function arguments
84 func FuncCallInterfaceArg(f func(a int, b interface{})) {
85 var m sync.Mutex
86 var t struct{ lock sync.Mutex }
88 f(1, "foo")
89 f(2, &t)
90 f(3, &sync.Mutex{})
91 f(4, m) // ERROR "call of f copies lock value: sync.Mutex"
92 f(5, t) // ERROR "call of f copies lock value: struct.lock sync.Mutex. contains sync.Mutex"
93 var fntab []func(t)
94 fntab[0](t) // ERROR "call of fntab.0. copies lock value: struct.lock sync.Mutex. contains sync.Mutex"
97 // Returning lock via interface value
98 func ReturnViaInterface(x int) (int, interface{}) {
99 var m sync.Mutex
100 var t struct{ lock sync.Mutex }
102 switch x % 4 {
103 case 0:
104 return 0, "qwe"
105 case 1:
106 return 1, &sync.Mutex{}
107 case 2:
108 return 2, m // ERROR "return copies lock value: sync.Mutex"
109 default:
110 return 3, t // ERROR "return copies lock value: struct.lock sync.Mutex. contains sync.Mutex"
114 // Some cases that we don't warn about.
116 func AcceptedCases() {
117 x := EmbeddedRwMutex{} // composite literal on RHS is OK (#16227)
118 x = BadRet() // function call on RHS is OK (#16227)
119 x = *OKRet() // indirection of function call on RHS is OK (#16227)
122 // TODO: Unfortunate cases
124 // Non-ideal error message:
125 // Since we're looking for Lock methods, sync.Once's underlying
126 // sync.Mutex gets called out, but without any reference to the sync.Once.
127 type LocalOnce sync.Once
129 func (LocalOnce) Bad() {} // ERROR "Bad passes lock by value: testdata.LocalOnce contains sync.Mutex"
131 // False negative:
132 // LocalMutex doesn't have a Lock method.
133 // Nevertheless, it is probably a bad idea to pass it by value.
134 type LocalMutex sync.Mutex
136 func (LocalMutex) Bad() {} // WANTED: An error here :(