Skip to content
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

proposal: fmt: %p should accept uintptr #41495

Closed
seebs opened this issue Sep 19, 2020 · 8 comments
Closed

proposal: fmt: %p should accept uintptr #41495

seebs opened this issue Sep 19, 2020 · 8 comments

Comments

@seebs
Copy link
Contributor

seebs commented Sep 19, 2020

What version of Go are you using (go version)?

1.15

Does this issue reproduce with the latest release?

y

What operating system and processor architecture are you using (go env)?

N/A

What did you do?

fmt.Printf("%p", stringHeader.Data)

What did you expect to see?

A pointer value.

What did you see instead?

A fmt warning that %p got a non-pointer.

Rationale: It's true that uintptr is an integer-like thing rather than a pointer-like thing, but it's explicitly the type of integer which is used to represent pointers. It's certainly possible to just add the extra conversion everywhere, but I don't think it makes anything clearer, safer, or better. Allowing %p for uintptr shouldn't break any code except possibly "tests that check for getting that diagnostic" which seems like an edge case.

In the absence of moving GC, etc, I'd want %p to print a uintptr and the corresponding unsafe.Pointer identically.

(As a secondary issue, I would also sort of like %p to work on strings, printing the address of their backing store, but I'm enough less convinced of that that I'm leaving it as a parenthetical.)

@ianlancetaylor ianlancetaylor changed the title %p should accept uintptr proposal: fmt: %p should accept uintptr Sep 19, 2020
@gopherbot gopherbot added this to the Proposal milestone Sep 19, 2020
@ianlancetaylor
Copy link
Member

CC @robpike

@mvdan
Copy link
Member

mvdan commented Sep 20, 2020

#28865 is somewhat related, I think.

@robpike
Copy link
Contributor

robpike commented Sep 21, 2020

I'd rather that this not happen because uintptr is not a pointer, and the use of %p provides a modicum of type safety. If you want the hex you can always ask for it with %#x.

@seebs
Copy link
Contributor Author

seebs commented Sep 21, 2020

Yeah. This ties in somewhat with the occasional proposals for a slice-header-like-thing which contains an unsafe.Pointer instead of a uintptr. That said, I don't think there's a format specifier for uintptr that produces the same results as %p -- because %p can print "nil".

@rsc
Copy link
Contributor

rsc commented Oct 29, 2020

It sounds like %#x is fine.
Printing uintptr(0) as nil would be pretty surprising.
It's an integer, not a pointer.

@rsc
Copy link
Contributor

rsc commented Nov 4, 2020

Adding to minutes this week, but this seems headed for a likely decline based on the conversation.

@rsc
Copy link
Contributor

rsc commented Nov 11, 2020

Based on the discussion above, this seems like a likely decline.

@rsc
Copy link
Contributor

rsc commented Nov 18, 2020

No change in consensus, so declined.

@rsc rsc closed this as completed Nov 18, 2020
@golang golang locked and limited conversation to collaborators Nov 19, 2021
@rsc rsc moved this to Declined in Proposals Aug 10, 2022
@rsc rsc added this to Proposals Aug 10, 2022
@rsc rsc removed this from Proposals Oct 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants