Skip to content

Commit c2eba53

Browse files
kortschakrobpike
authored andcommitted
cmd/vet,sync: check lock values more precisely
Fixes #26165 Change-Id: I1f3bd193af9b6f8461c736330952b6e50d3e00d9 Reviewed-on: https://go-review.googlesource.com/121876 Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Rob Pike <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 00ebfcd commit c2eba53

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Diff for: src/cmd/vet/copylock.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,11 @@ func lockPath(tpkg *types.Package, typ types.Type) typePath {
234234
return nil
235235
}
236236

237-
// We're looking for cases in which a reference to this type
238-
// can be locked, but a value cannot. This differentiates
237+
// We're looking for cases in which a pointer to this type
238+
// is a sync.Locker, but a value is not. This differentiates
239239
// embedded interfaces from embedded values.
240-
if plock := types.NewMethodSet(types.NewPointer(typ)).Lookup(tpkg, "Lock"); plock != nil {
241-
if lock := types.NewMethodSet(typ).Lookup(tpkg, "Lock"); lock == nil {
242-
return []types.Type{typ}
243-
}
240+
if types.Implements(types.NewPointer(typ), lockerType) && !types.Implements(typ, lockerType) {
241+
return []types.Type{typ}
244242
}
245243

246244
nfields := styp.NumFields()
@@ -254,3 +252,15 @@ func lockPath(tpkg *types.Package, typ types.Type) typePath {
254252

255253
return nil
256254
}
255+
256+
var lockerType *types.Interface
257+
258+
// Construct a sync.Locker interface type.
259+
func init() {
260+
nullary := types.NewSignature(nil, nil, nil, false) // func()
261+
methods := []*types.Func{
262+
types.NewFunc(token.NoPos, nil, "Lock", nullary),
263+
types.NewFunc(token.NoPos, nil, "Unlock", nullary),
264+
}
265+
lockerType = types.NewInterface(methods, nil).Complete()
266+
}

Diff for: src/sync/cond.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,5 @@ func (c *copyChecker) check() {
9494
type noCopy struct{}
9595

9696
// Lock is a no-op used by -copylocks checker from `go vet`.
97-
func (*noCopy) Lock() {}
97+
func (*noCopy) Lock() {}
98+
func (*noCopy) Unlock() {}

0 commit comments

Comments
 (0)