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

std::format! auto-dereferences pointers? #17988

Closed
jkleint opened this issue Oct 12, 2014 · 6 comments
Closed

std::format! auto-dereferences pointers? #17988

jkleint opened this issue Oct 12, 2014 · 6 comments

Comments

@jkleint
Copy link

jkleint commented Oct 12, 2014

Is this a 'feature?'

fn main() {
    let x = 5i;
    assert_eq!(format!("{}", &x), format!("{:p}", &x));
}
task '<main>' failed at 'assertion failed: `(left == right) && (right == left)` (left: `5`, right: `0x7fffba8927f8`)', format_ptr.rs:3

This is with rustc 0.13.0-nightly (adb44f53d 2014-10-12 00:07:15 +0000).

@sfackler
Copy link
Member

&T's implementation of Show defers to T's.

@jkleint
Copy link
Author

jkleint commented Oct 13, 2014

Seems a little surprising, especially as it's not documented.

@sfackler
Copy link
Member

I don't think it's all that surprising. 99%+ of the time when I'm working with a &-reference, I care about the thing it's pointing to, not that thing's location in memory.

@huonw
Copy link
Member

huonw commented Oct 13, 2014

Minor quibble: it is documented, the list of implementors for Show includes impl<'a, T: Show> Show for &'a T. In general, Rust tries to treat references as if they are the thing to which they point, looking at the value rather than just its memory location.

@jkleint
Copy link
Author

jkleint commented Oct 15, 2014

Oh I agree that having an auto-deref format is very useful, and that's what I would use most of the time, too. But do you think people would find it surprising as the default? Imagine the C programmer who writes printf("%p\n", &x); and gets 5; is s/he going to think, "Oh, how nice, printf is dereferencing things for me;" or "WTF?!?" The problem is more serious when you are printing (pointers to) large integers that are indistinguishable from addresses. I can see some serious hair-tearing as a result. I would expect a systems programming language to do what I say, and not second-guess me.

99%+ of the time when I'm working with a &-reference, I care about the thing it's pointing to

Then 99%+ of the time you aren't going to get what you care about with double indirection:

let x = 5i;
let p = &x;
assert_eq!(format!("{}", &p), format!("{:p}", &p));
task '<main>' failed at 'assertion failed: `(left == right) && (right == left)` (left: `5`, right: `0x7fff65681958`)', fmt_deref.rs:4

Regardless of where you fall on the preceeding, do you really want it to be recursive? Imagine all the poor students writing their first linked list in Rust... ;)

Minor quibble: it is documented, the list of implementors for Show includes impl<'a, T: Show> Show for &'a T.

Would you expect a Rust newbie to look there, or std::fmt?

Rust is going to be many people's first experience with pointers. Let's make it not suck.

@steveklabnik
Copy link
Member

Given that this is intended behavior, I'm giving it a close. If you'd like to change it, please pursue an RFC, thanks!

lnicola pushed a commit to lnicola/rust that referenced this issue Aug 29, 2024
Fix incorrect symbol definitions in SCIP output

The SCIP output incorrectly marks some symbols as definitions because it doesn't account for the file ID when comparing the token's range to its definition's range.

This means that if a symbol is referenced in a file at the same position at which it is defined in another file, that reference will be marked as a definition. I was quite surprised by how common this is. For example, `PartialEq` is defined [here](https://github.com/rust-lang/rust/blob/1.80.1/library/core/src/cmp.rs#L273) and `uuid` references it [here](https://github.com/uuid-rs/uuid/blob/1.8.0/src/lib.rs#L329). And what do you know, they're both at offset 10083! In our large monorepo, this happens for basically every common stdlib type!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants