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/compile: prevent interface conversion panic at compile time #31585
Comments
It is a panic at run time, not compile time. The output clearly states the reason: the dynamic type of A failed type assertion without the second optional result will panic. You can write the last assertion as the following to avoid panic. test8, _ := test4.(Test3) // present the second optional result
if test8 != nil {
fmt.Println(test8.test("hello"))
} |
Thanks, but what I ment was that I think the compiler should pick it up and prevent it from building in the first place. So what I mean is, it should be a compile error instead of a runtime error. |
The spec requires that this code compile successfully. We'd have to adjust the spec in order to do what you suggest, which is a very high bar. If you want to move forward with a vet strategy, we'd need to know what such a detector would look like. How would the analysis happen? What would be the error rate (both positive and negative)? Are there actual instances of this bug in the wild? |
|
I think the gopls tool which is currently being developed here will do what you want. |
As far as I can tell |
|
ok so the best place to implement a list of all interfaces to begin with is still go vet right? Maybe step two to compare it with other types to see if a cast makes sense could be implemented in gopls directly then. But step one is still a grouped by interface list of all interfaces variables (
|
Thank you @gertcuykens for filing this issue and everyone for chiming in. As @go101 and @randall77 have mentioned, this in deed is a runtime error and we can't change this, but as @beoran and @stamblerre have noted, you can add vet rules for such. I shall also kindly cc @johanbrandhorst @dominikh who might be interested in these vet rules, but for now I shall close this issue. Please feel free to reopen if I've missed something. |
I think the compiler at compile time should stop building to prevent this panic at runtime ?
https://play.golang.org/p/ffP9vSHdHof
panic: interface conversion: main.Test4 is main.Test1, not main.Test3
The text was updated successfully, but these errors were encountered: