Consider the following:
var v atomic.Value
var err error
err = &http.ProtocolError{}
v.Store(err)
err = io.EOF
v.Store(err)
The intention to have a atomic value store for errors. However, running this code panics:
panic: sync/atomic: store of inconsistently typed value into Value
This is because atomic.Value requires that the underlying concrete type be the same (which is a reasonable expectation for its implementation). When going through the atomic.Value.Store method call, the fact that both these are of the error interface is lost.
Perhaps we should add a vet check that flags usages of atomic.Value where the argument passed in is an interface type?
Vet criterions:
- frequency: not sure, I haven't done an analysis through all Go corpus.
- correctness: this is almost always wrong. Any "correct" usages should type assert to the concrete value first.
- accuracy: if the type information available can conclusively show an argument is an interface type, then very accurate.
\cc @robpike
\cc @dominikh for staticcheck
Consider the following:
The intention to have a atomic value store for errors. However, running this code panics:
This is because
atomic.Valuerequires that the underlying concrete type be the same (which is a reasonable expectation for its implementation). When going through theatomic.Value.Storemethod call, the fact that both these are of theerrorinterface is lost.Perhaps we should add a vet check that flags usages of
atomic.Valuewhere the argument passed in is an interface type?Vet criterions:
\cc @robpike
\cc @dominikh for
staticcheck