-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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/gopls: completing argument to %p
verb adds unwanted &
for some types
#65609
Comments
Thanks! I imagine this is straightforward to fix. High level notes:
Adding to the next milestone. |
I will work on this issue |
Thanks @naeemaei! Let us know if you need any additional guidance. |
Maps, channels, unsafe.Pointer, and functions as well. Anything pointer-esque, really. Also, interfaces are probably a special case separate from that and not really limited to |
I added the But when I print an interface variable, the print output is as below: structInterface = person{FirstName: "name"}
fmt.Printf("structInterface: %p\n", structInterface) // structInterface: %!p(main.person={name})
fmt.Printf("structInterface pointer: %p\n", &structInterface) // structInterface pointer: 0xc000014070 When the interface underlying type is a pointer output is the underlying variable address: strct := &person{FirstName: "name"}
structInterface = strct
fmt.Printf("struct pointer: %p\n", strict) // 0xc000014090
fmt.Printf("structInterface: %p\n", structInterface) // structInterface: 0xc000014090
fmt.Printf("structInterface pointer: %p\n", &structInterface) // structInterface pointer: 0xc000014070
This part is a bit vague, I need your guidance regarding the specific logic that should be handled after recognizing interfaces in |
@naeemaei I think the point is that when the argument is an interface, we don't generally know whether or not it holds a pointer (or any other kind of type). In the absence of certainty, it is better not to apply the &. We should only apply type modifiers when they are strictly required to have a valid type. So it sounds like gopls is working as intended in your example. |
Another way to fix this might be to make As for what completion candidates the "%p" verb prefers, we should also include "chan", "map" and "func". I don't think we should make it prefer interfaces since those aren't necessarily printable with "%p". |
Change https://go.dev/cl/593575 mentions this issue: |
gopls version
f4fa7a7
go env
What did you do?
Trigger completion at the end of
What did you see happen?
The line completes to
What did you expect to see?
The variable has an interface type. I intended to print the pointer stored in the interface value, not the address of the local variable.
The same happens with maps, channels, unsafe.Pointer, and functions, all of which are accepted by
%p
. It does not happen with true pointers, or slices.Editor and settings
No response
Logs
No response
The text was updated successfully, but these errors were encountered: