Surprising behavior in `go tool vet -shadow`

669 views
Skip to first unread message

dominic...@gmail.com

unread,
Aug 10, 2015, 1:10:09 AM8/10/15
to golang-nuts
Using the go tool vet -shadow command correctly reports shadowing in the following code:
func correct() {
var a int
{
var a int
a = 3
fmt.Println(a)
}
fmt.Println(a)
}

However, running the same command on this code does not report any shadowing:
func broken() {
var a int
{
a := 3
fmt.Println(a)
}
fmt.Println(a)
}

Should the second example report shadowing?

Rob Pike

unread,
Aug 10, 2015, 1:21:41 AM8/10/15
to dominic...@gmail.com, golang-nuts
Maybe yes, maybe no. In this trivial program it's easy to say this is a mistake, but with the same structure in a larger context it might not be. This is why shadowing is tricky.

In any case the vet -shadow feature is disabled for a reason. It does not give the level of confidence the other analyses do.

-rob


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Val

unread,
Aug 10, 2015, 3:36:02 AM8/10/15
to golang-nuts, dominic...@gmail.com
Hello, I'm confused at this point.
What would be the "rules of thumb" to determine whether vet (or a human) should report a shadowing as suspicious?

Andrew Gerrand

unread,
Aug 10, 2015, 3:56:04 AM8/10/15
to Val, golang-nuts, dominic...@gmail.com
On 10 August 2015 at 17:36, Val <dele...@gmail.com> wrote:
Hello, I'm confused at this point.
What would be the "rules of thumb" to determine whether vet (or a human) should report a shadowing as suspicious?

We don't really know; that's why vet's 'shadow' feature is disabled by default.

Andrew  

Nico Tonozzi

unread,
Aug 13, 2015, 4:46:20 PM8/13/15
to Andrew Gerrand, Val, golang-nuts
OK. I thought there was a difference between the desired behavior and the implementation of `idiomaticShortRedecl`.

If the right hand side AST node is not an ast.Ident or TypeAssertExpr, the whole function should fail and return false. Right now the function returns true even when the assignment being evaluated is not an idiomatic short redeclaration as described at the top of the function.

I would like to submit a patch for this, if you agree that the implementation is not behaving as desired.

Andrew Gerrand

unread,
Aug 18, 2015, 10:42:51 AM8/18/15
to Nico Tonozzi, Val, golang-nuts
I suggest filing an issue about it: https://golang.org/issue/new


Charles Haynes

unread,
Aug 27, 2015, 12:43:02 AM8/27/15
to Nico Tonozzi, Andrew Gerrand, Val, golang-nuts
What should go vet --shadow do in this case:

var a int
{
    a := a
    fmt.Println(a)
}
fmt.Println(a)

?

Because making a local copy in a closure using the outside name is pretty common. Strictly speaking it "shadows" the outside name, but that is exactly the desired behaviour.

-- Charles

C Banning

unread,
Aug 27, 2015, 5:16:10 AM8/27/15
to golang-nuts, dominic...@gmail.com
Reply all
Reply to author
Forward
0 new messages