-
Notifications
You must be signed in to change notification settings - Fork 530
Document the behavior of pointer to smaller int casts #729
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
Comments
In Miri, this is implemented as truncation. But honestly I had no idea this was even allowed, so I'd be all in favor of linting against this, maybe even as a future-compat lint with the goal of deprecating such casts. |
Shall we ping the lang team about this? A related issue is what's the behavior of a pointer to integer cast, where the integer is larger than the pointer. Do we get a zero-extension or a sign-extension, and is the behavior of |
Truncation does seem reasonable. cc @rust-lang/lang |
Some history - rust-lang/rfcs#1052 (comment) and below. |
@petrochenkov thanks, I'm sadly don't think I'm able to follow that discussion without IRC logs. Things that are hard to follow for me:
I suppose all of this was debated more in detail on IRC, and arguments were given there. |
Playground:
As @rkruppe pointed out, this snippet casts a pointer to an int in which the pointer does not fit. AFAICT, the behavior of this operation is not guaranteed anywhere - I suppose it does a truncation. We should document what it does.
cc @Centril @RalfJung
I personally would probably prefer if it was at least a warning that suggests people to write
as X as u8
instead, whereX
is a type in which the pointer precisely fits, e.g.,usize
and/oru{target_pointer_width}
(e.g.u64
) - if that is what that cast actually happens to mean, but this is an issue that should be better discussed in rust-lang/rust, once we know what that cast means.The text was updated successfully, but these errors were encountered: