Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/vet: teach vet that putting a large value in an interface makes a copy #14529

Closed
josharian opened this issue Feb 26, 2016 · 6 comments
Closed

Comments

@josharian
Copy link
Contributor

package main

import "sync"

type T struct {
    sync.Mutex
    _ [4]byte
}

func main() {
    var t T

    var s T
    s = t

    var e interface{}
    e = t
}
$ go vet x.go
x.go:14: assignment copies lock value to s: main.T
exit status 1

vet should also complain about the assignment e = t, since it also copies t's value.

@rogpeppe
Copy link
Contributor

This would have caught some bugs for us.

@josharian
Copy link
Contributor Author

cc @valyala :)

@valyala
Copy link
Contributor

valyala commented Mar 17, 2016

It looks like this is already fixed in #14664 (go tip):

go vet 1.go
1.go:14: assignment copies lock value to s: main.T
1.go:17: assignment copies lock value to e: main.T

@valyala
Copy link
Contributor

valyala commented Mar 17, 2016

Other cases must be checked such as passing lock as function argument accepting interface{} or returning lock from function returning interface{}

@valyala
Copy link
Contributor

valyala commented Mar 18, 2016

Prepared a CL, which expands copylock check to function calls and return values - see https://go-review.googlesource.com/20832 . This catches passing lock into interface{} in these cases.

@gopherbot
Copy link

CL https://golang.org/cl/20832 mentions this issue.

@golang golang locked and limited conversation to collaborators Mar 23, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants