gvisor icon indicating copy to clipboard operation
gvisor copied to clipboard

[checklocks] Annotations ignored on generic structs

Open rodaine opened this issue 1 year ago • 3 comments

Description

It appears checklocks (sha: 69e0c7643d821f03ed797ea3911f6ab458e2a2d6) fails to recognize annotations on generic types. For the following code:

package example

import (
	"sync"
)

type X struct {
	mu sync.Mutex
	// +checklocks:mu
	m map[string]struct{}
}

func (x *X) Add(k string) {
	x.mu.Lock()
	defer x.mu.Unlock()
	x.m[k] = struct{}{}
}

type Y[K comparable] struct {
	mu sync.Mutex
	// +checklocks:mu
	m map[K]struct{}
}

func (y *Y[K]) Add(k K) {
	y.mu.Lock()
	defer y.mu.Unlock()
	y.m[k] = struct{}{}
}

checklocks returns the following:

$ go version
go version go1.22.2 darwin/arm64
$ go vet -vettool=.bin/checklocks ./example/...
# example.com/example
example/x.go:22:2: may require checklocks annotation for mu, used with lock held 100% of the time

rodaine avatar May 03 '24 19:05 rodaine

A friendly reminder that this issue had no activity for 120 days.

github-actions[bot] avatar Sep 13 '24 00:09 github-actions[bot]

I also see this -- is there a workaround? It seems like you can't disable the checks on the entire struct

bprosnitz avatar Sep 16 '24 19:09 bprosnitz