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

Comments

Projects
None yet
4 participants
@josharian
Copy link
Contributor

commented Feb 26, 2016

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

This comment has been minimized.

Copy link
Contributor

commented Mar 17, 2016

This would have caught some bugs for us.

@josharian

This comment has been minimized.

Copy link
Contributor Author

commented Mar 17, 2016

cc @valyala :)

@valyala

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link

commented Mar 18, 2016

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

@gopherbot gopherbot closed this in 1374515 Mar 23, 2016

@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.
You can’t perform that action at this time.