golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.66k stars 17.62k forks source link

fmt: compound object rules don't seem to apply recursively #28625

Open mvdan opened 5 years ago

mvdan commented 5 years ago

https://golang.org/pkg/fmt/#hdr-Printing reads:

For compound objects, the elements are printed using these rules, recursively, laid out like this:

    struct:             {field0 field1 ...}
    array, slice:       [elem0 elem1 ...]
    maps:               map[key1:value1 key2:value2 ...]
    pointer to above:   &{}, &[], &map[]

It is specifically said that these rules apply recursively. However, take a look at this playground link: https://play.golang.org/p/CbrniH9q45I

It currently prints:

{x}
&{x}
{%!s(*main.T2=&{x})}
&{%!s(*main.T2=&{x})}

But I'd expect it to print:

{x}
&{x}
{&{x}}
&{&{x}}

Reading the docs carefully again, I can't find a reason why only top-level pointers would follow the "pointer to above" rule that's clearly documented.

It seems to me like either the code is wrong, or the docs need clarification. If only top-level pointers are supposed to follow the "pointer to above" rule, that should be made clear in the rules.

This issue is split from #27672. I initially thought this was a bug in vet, but then started wondering if this was a bug in fmt instead.

/cc @robpike @martisch @rogpeppe

martisch commented 5 years ago

related: https://github.com/golang/go/issues/27768#issuecomment-423040522 I think we should point this out in the documentation.

mvdan commented 5 years ago

You're right; I hadn't caught that issue as the title seemed unrelated.

Should we keep this issue open and amend the documentation?

martisch commented 5 years ago

I think the issues are slightly different (the other one started as %d not applying to pointers, which IIRC is WAI according to the documentation). So happy if this stays open and we can discuss if we should just update the documentation. With the other issue i wanted to reference the note that the implementation is deliberately not following pointers to arbitrary depth as there could be loops in pointer chains.

deanveloper commented 5 years ago

Those look like rules for %v, but your example uses %s

Output using %v: https://play.golang.org/p/G4sWTdsNIi0

{x}
&{x}
{0x40e150}
&{0x40e160}

Which makes a bit more sense. Since it's a pointer to a pointer, %v would print with &{%p}. Either way, I agree that the documentation should be more clear.

gopherbot commented 5 years ago

Change https://golang.org/cl/147997 mentions this issue: cmd/vet: fix printf false negative with nested pointers

mvdan commented 5 years ago

I don't think the rules only apply to %v - one can use them with %s just fine, as my playground program above shows.

martisch commented 5 years ago

As far as I understand the rules for compounds they should always apply. As far as I remember the implementation it will work for all verbs except for %T and %p as those are handled specially.

robpike commented 5 years ago

Every time we try to fix fmt to be more consistent or correct, we roll back the change because it breaks things. I suspect we should just leave it alone.

mvdan commented 5 years ago

I don't imagine that clarifying the docs will break any program :)