]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: check for copying of array of locks
authorAliaksandr Valialkin <valyala@gmail.com>
Wed, 22 Jun 2016 17:16:41 +0000 (20:16 +0300)
committerRob Pike <r@golang.org>
Mon, 29 Aug 2016 21:56:31 +0000 (21:56 +0000)
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: Rob Pike <r@golang.org>
src/cmd/vet/copylock.go
src/cmd/vet/testdata/copylock.go

index 65337688bc069ca9fab5537e6e8cb86a3f88cbd5..31c1257a47017401a53811c7d874608bf85f0de2 100644 (file)
@@ -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)
index d49f46862758716e77669755a43f798b94926a18..35ed766f1da7e26010bcd7d3285c534c5ebf8b83 100644 (file)
@@ -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