Skip to content

cmd/vet: copylocks false positives #16227

@josharian

Description

@josharian

Examples from the standard library that I believe were introduced between Go 1.6 and Go 1.7.

context/context.go:230: assignment copies lock value to c: context.cancelCtx contains sync.Mutex
context/context.go:236: newCancelCtx returns lock by value: context.cancelCtx contains sync.Mutex
context/context.go:375: literal copies lock value from newCancelCtx(parent): context.cancelCtx contains sync.Mutex

func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
    c := newCancelCtx(parent) // LINE 230
    propagateCancel(parent, &c)
    return &c, func() { c.cancel(true, Canceled) }
}

func newCancelCtx(parent Context) cancelCtx {  // LINE 236
    return cancelCtx{
        Context: parent,
        done:    make(chan struct{}),
    }
}

    c := &timerCtx{
        cancelCtx: newCancelCtx(parent), // LINE 375
        deadline:  deadline,
    }

cancelCtx is a struct type containing a sync.Mutex. But all of these are just fine--in newCancelCtx (line 236), the sync.Mutex is (implicitly) being set to its zero value, which is fine. And since it is the zero value, and these are used as initializations of new values (lines 230, 375), they are also ok.

go/internal/gcimporter/bimport.go:347: assignment copies lock value to *t: go/types.Struct contains sync.Once contains sync.Mutex

        *t = *types.NewStruct(p.fieldList(parent))

Types.NewStruct constructs a new struct and omits (implicitly zeros) the lock fields.

I think we should consider rolling back the new checks for 1.7 and restoring them in 1.8 without these false positives. At least in the case of struct initialization, ignoring fields being initialized to the zero value will help. I'm not sure how to detect copying of values that are known to contain zero-valued locks. Marking Go 1.7; Rob can adjust milestone from there.

cc @valyala @robpike

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions