-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
x/tools/go/analysis/passes/shadow: is confused by newtypes #49652
Comments
@matloob PTAL. |
IIUC this is the expected behavior. From the code:
From the documentation "A shadowed variable is a variable declared in an inner scope In the example, I am missing the history for why shadow looks for the types to be identical and not the Underlying type to be identical. I am guessing there was a good reason though. |
As I recall it's simply that if the shadowing (inner) variable |
@ianlancetaylor I think my question is essentially, why is the test on the type (current Analyzer code): if types.Identical(obj.Type(), shadowed.Type()) { instead of the underlying type: if types.Identical(obj.Type().Underlying(), shadowed.Type().Underlying()) { Both seem somewhat reasonable for a shadow checker to me. For very different types, a type checking error does seem quite likely. |
Here is a modified version of the original example that gives me some pause for whether switching from type identity to Underlying type identity makes sense. Here we have package main
import "fmt"
type x int
func (v x) Foo() { fmt.Println("x.Foo()", v)}
type y x
func (v y) Foo() { fmt.Println("y.Foo()", v)}
func main() {
as := []int{1, 2, 3}
bs := []int{10, 20, 30}
for _, a := range as {
v := x(a)
for _, b := range bs {
v := y(b)
v.Foo()
}
v.Foo()
}
} |
I see. I think I did misunderstand this. I have no answer for your question. It's probably fine to change it. |
Reproducer
https://play.golang.org/p/Xx4xqdtocit
Expected
Got
Details
If I remove the type conversions, the result is as expected.
OS, Versions, And So On
Formalities
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?go env
OutputThe text was updated successfully, but these errors were encountered: