]> Cypherpunks repositories - gostls13.git/commitdiff
sync: add explicit noCopy fields to Map, Mutex, and Once
authorMichael Anthony Knyszek <mknyszek@google.com>
Fri, 15 Nov 2024 19:22:16 +0000 (19:22 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 18 Nov 2024 18:52:54 +0000 (18:52 +0000)
Following CLs will refactor Mutex and change the internals of Map. This
ends up breaking tests in x/tools for the copylock vet check, because
the error message changes. Let's insulate ourselves from such things
permanently by adding an explicit noCopy field. We'll update the vet
check to accept that as the problem, rather than depend on less explicit
internals.

We capture Once here too to clean up the error message as well.

Change-Id: Iead985fc8ec9ef3ea5ff615f26dde17bb03aeadb
Reviewed-on: https://go-review.googlesource.com/c/go/+/627777
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Tim King <taking@google.com>
src/sync/map.go
src/sync/mutex.go
src/sync/once.go
test/assign.go

index 33bc8141abd4c86724880fc9f7d150714549abf9..4f1395110a2044a83e21b351438639aa370ffba9 100644 (file)
@@ -36,6 +36,8 @@ import (
 //
 // [the Go memory model]: https://go.dev/ref/mem
 type Map struct {
+       _ noCopy
+
        mu Mutex
 
        // read contains the portion of the map's contents that are safe for
index e4ed47c75c2a3c14df72be10731c201c07aba291..cd50fcbbb5067eff21302a6000a49ae6e535328d 100644 (file)
@@ -34,6 +34,8 @@ func fatal(string)
 //
 // [the Go memory model]: https://go.dev/ref/mem
 type Mutex struct {
+       _ noCopy
+
        state int32
        sema  uint32
 }
index 168c7bbdd38f61957644de6de568117ddf6d76df..90840b19b5e70540e55f781b4f18c1cbe5f1d272 100644 (file)
@@ -18,6 +18,8 @@ import (
 //
 // [the Go memory model]: https://go.dev/ref/mem
 type Once struct {
+       _ noCopy
+
        // done indicates whether the action has been performed.
        // It is first in the struct because it is used in the hot path.
        // The hot path is inlined at every call site.
index bdec58b710f4210a7741a6973a5cd4ed0ce64211..5beffee6f36fc071bd1641849da25a9e5c65f02c 100644 (file)
@@ -9,7 +9,10 @@
 
 package main
 
-import "sync"
+import (
+       "sync"
+       "time"
+)
 
 type T struct {
        int
@@ -38,7 +41,7 @@ func main() {
                _ = x
        }
        {
-               x := sync.Mutex{0, 0} // ERROR "assignment.*Mutex"
+               x := time.Time{0, 0, nil} // ERROR "assignment.*Time"
                _ = x
        }
        {