Skip to content

Commit

Permalink
go/analysis/passes/copylock: test for noCopy for sync Map, Mutex, Once
Browse files Browse the repository at this point in the history
Right now the copylock tests check for details on these types that are
implementation details and/or have some other problem (a less useful
error message, or a false negative altogether).

CL 627777 modifies the sync package to attach explicit anonymous noCopy
fields on each of these types. This change updates the tests to match
that, which also helps with and/or resolves a couple issues with
copylock captured in the test suite.

Note: this change also temporarily disables a couple of the problematic
tests. In the next CL we'll re-enable them, once CL 627777 lands.

Change-Id: I100c71ea05b4f08595e37d0c8e81f9543abe7d74
Reviewed-on: https://go-review.googlesource.com/c/tools/+/628435
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Tim King <taking@google.com>
  • Loading branch information
mknyszek committed Nov 18, 2024
1 parent a54bd37 commit 9b9871d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
4 changes: 4 additions & 0 deletions go/analysis/passes/copylock/copylock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (

func Test(t *testing.T) {
testdata := analysistest.TestData()
// TODO(mknyszek): Add "unfortunate" package once CL 627777 lands. That CL changes
// the internals of the sync package structures to carry an explicit noCopy to prevent
// problems from changes to the implementations of those structures, such as these
// tests failing, or a bad user experience.
analysistest.Run(t, testdata, copylock.Analyzer, "a", "typeparams", "issue67787")
}

Expand Down
25 changes: 7 additions & 18 deletions go/analysis/passes/copylock/testdata/src/a/copylock_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
// This file contains tests for the copylock checker's
// function declaration analysis.

// There are two cases missing from this file which
// are located in the "unfortunate" package in the
// testdata directory. Once the go.mod >= 1.26 for this
// repository, merge local_go124.go back into this file.

package a

import "sync"

func OkFunc(*sync.Mutex) {}
func BadFunc(sync.Mutex) {} // want "BadFunc passes lock by value: sync.Mutex"
func BadFunc2(sync.Map) {} // want "BadFunc2 passes lock by value: sync.Map contains sync.Mutex"
func BadFunc2(sync.Map) {} // want "BadFunc2 passes lock by value: sync.Map contains sync.(Mutex|noCopy)"
func OkRet() *sync.Mutex {}
func BadRet() sync.Mutex {} // Don't warn about results

var (
OkClosure = func(*sync.Mutex) {}
BadClosure = func(sync.Mutex) {} // want "func passes lock by value: sync.Mutex"
BadClosure2 = func(sync.Map) {} // want "func passes lock by value: sync.Map contains sync.Mutex"
BadClosure2 = func(sync.Map) {} // want "func passes lock by value: sync.Map contains sync.(Mutex|noCopy)"
)

type EmbeddedRWMutex struct {
Expand Down Expand Up @@ -118,19 +123,3 @@ func AcceptedCases() {
x = BadRet() // function call on RHS is OK (#16227)
x = *OKRet() // indirection of function call on RHS is OK (#16227)
}

// TODO: Unfortunate cases

// Non-ideal error message:
// Since we're looking for Lock methods, sync.Once's underlying
// sync.Mutex gets called out, but without any reference to the sync.Once.
type LocalOnce sync.Once

func (LocalOnce) Bad() {} // want `Bad passes lock by value: a.LocalOnce contains sync.\b.*`

// False negative:
// LocalMutex doesn't have a Lock method.
// Nevertheless, it is probably a bad idea to pass it by value.
type LocalMutex sync.Mutex

func (LocalMutex) Bad() {} // WANTED: An error here :(
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !go1.24

package unfortunate

import "sync"

// TODO: Unfortunate cases

// Non-ideal error message:
// Since we're looking for Lock methods, sync.Once's underlying
// sync.Mutex gets called out, but without any reference to the sync.Once.
type LocalOnce sync.Once

func (LocalOnce) Bad() {} // want `Bad passes lock by value: a.LocalOnce contains sync.\b.*`

// False negative:
// LocalMutex doesn't have a Lock method.
// Nevertheless, it is probably a bad idea to pass it by value.
type LocalMutex sync.Mutex

func (LocalMutex) Bad() {} // WANTED: An error here :(
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.24

package unfortunate

import "sync"

// Cases where the interior sync.noCopy shows through.

type LocalOnce sync.Once

func (LocalOnce) Bad() {} // want "Bad passes lock by value: a.LocalOnce contains sync.noCopy"

type LocalMutex sync.Mutex

func (LocalMutex) Bad() {} // want "Bad passes lock by value: a.LocalMutex contains sync.noCopy"

0 comments on commit 9b9871d

Please sign in to comment.