Rust: document what "thin pointers" are

Created on 14 Apr 2017  路  9Comments  路  Source: rust-lang/rust

I got the following error message:

error: casting `*const T` as `usize` is invalid
  --> src/refset.rs:17:9
   |
17 |         (self.0 as *const T as usize).hash(state);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: cast through a thin pointer first

which was quite clear, except that there seems to be no documentation as to what a "thin" pointer is. I ended up through guesswork casting to a *const usize in between which made this work for me.

A-diagnostics C-enhancement D-confusing T-compiler

Most helpful comment

I just ran into this issue and had no idea what was going on, so it would be great if this could be fixed.

(For anyone else confused about this thin pointer help message, you can fix it by adding an intermediate cast to some primitive pointer type like *const usize)

All 9 comments

/cc @rust-lang/lang ; I wonder what the official terminology here should be. I prefer the "single pointer"/"double pointer" language to the "thin pointer"/"fat pointer" stuff, but it seems to me that this diagnostic could use re-worked and possibly made even more clear. Thoughts?

also, re-tagging as compiler since I believe this is more of a diagnostic issue than a docs issue.

"single pointer"/"double pointer" language

That's not really accurate though, is it? Slices are (pointer, len), not two pointers. There's also the possibility of custom DSTs which would permit fat pointers larger than two words.

I prefer the "single pointer"/"double pointer" language to the "thin pointer"/"fat pointer" stuff, but it seems to me that this diagnostic could use re-worked and possibly made even more clear.

I agree with rkruppe that 'double pointer' could be misleading, but think our language would be slightly more welcoming if we used 'wide pointer' in error messages instead of 'fat'.

not two pointers

When I've heard it, it was referring to "double the size", not "two pointers" though that confusion is certainly a good reason to not have it.

馃憤 for "pointer"/"wide pointer"

Would "non-wide pointer" make sense then?

Yea, I think in most cases "thin pointer" can probably just be "pointer," this error message (trying to cast wide pointer down) seems like the only one where its helpful to explicit mentioning the non-wideness of the pointer, based on a ripgrep.

"non-wide" or even just "regular" seems fine when you need to highlight the distinction of most pointers from wide pointers.

In light of the custom DST proposal, I've come to think of all pointers as "wide pointers", it's sort of just a question of what their auxiliary data is ("thin" pointers would have metadata of type ()). I wonder if we can avoid the terminology altogether, somehow? But it's not obvious how to adapt the error message to this POV. I think of "casting to a thin pointer" as basically replacing the wide pointer metadata with ().

I just ran into this issue and had no idea what was going on, so it would be great if this could be fixed.

(For anyone else confused about this thin pointer help message, you can fix it by adding an intermediate cast to some primitive pointer type like *const usize)

I just ran across this issue after encountering the mentioned error message. I was thoroughly confused, and while the solution of casting to *const usize instead of just usize works, I have no idea why that is the case (I prefer to understand how things work rather than just copying a solution from somewhere). I would really appreciate some further documentation on what thin pointers vs wide pointers are, when they are used and why, specifically when dyn Any is involved.

Was this page helpful?
0 / 5 - 0 ratings