Skip to content

Commit bd9aa98

Browse files
valyalarobpike
authored andcommitted
cmd/vet: check for copying of array of locks
Updates #14664 Change-Id: I1f7b1116cfe91466816c760f136ce566da3e80a9 Reviewed-on: https://go-review.googlesource.com/24340 Run-TryBot: Rob Pike <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent c9fbe0f commit bd9aa98

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/cmd/vet/copylock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ func lockPath(tpkg *types.Package, typ types.Type) typePath {
210210
return nil
211211
}
212212

213+
for {
214+
atyp, ok := typ.Underlying().(*types.Array)
215+
if !ok {
216+
break
217+
}
218+
typ = atyp.Elem()
219+
}
220+
213221
// We're only interested in the case in which the underlying
214222
// type is a struct. (Interfaces and pointers are safe to copy.)
215223
styp, ok := typ.Underlying().(*types.Struct)

src/cmd/vet/testdata/copylock.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ func BadFunc() {
6868
// override 'new' keyword
6969
new := func(interface{}) {}
7070
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
7189
}
7290

7391
// SyncTypesCheck checks copying of sync.* types except sync.Mutex

0 commit comments

Comments
 (0)