Commit bd9aa981 authored by Aliaksandr Valialkin's avatar Aliaksandr Valialkin Committed by Rob Pike

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 <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRob Pike <r@golang.org>
parent c9fbe0f2
......@@ -210,6 +210,14 @@ func lockPath(tpkg *types.Package, typ types.Type) typePath {
return nil
}
for {
atyp, ok := typ.Underlying().(*types.Array)
if !ok {
break
}
typ = atyp.Elem()
}
// We're only interested in the case in which the underlying
// type is a struct. (Interfaces and pointers are safe to copy.)
styp, ok := typ.Underlying().(*types.Struct)
......
......@@ -68,6 +68,24 @@ func BadFunc() {
// override 'new' keyword
new := func(interface{}) {}
new(t) // ERROR "function call copies lock value: testdata.Tlock contains sync.Once contains sync.Mutex"
// copy of array of locks
var muA [5]sync.Mutex
muB := muA // ERROR "assignment copies lock value to muB: sync.Mutex"
muA = muB // ERROR "assignment copies lock value to muA: sync.Mutex"
muSlice := muA[:] // OK
// multidimensional array
var mmuA [5][5]sync.Mutex
mmuB := mmuA // ERROR "assignment copies lock value to mmuB: sync.Mutex"
mmuA = mmuB // ERROR "assignment copies lock value to mmuA: sync.Mutex"
mmuSlice := mmuA[:] // OK
// slice copy is ok
var fmuA [5][][5]sync.Mutex
fmuB := fmuA // OK
fmuA = fmuB // OK
fmuSlice := fmuA[:] // OK
}
// SyncTypesCheck checks copying of sync.* types except sync.Mutex
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment