-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Policy for assumptions about the size of usize
#1748
Comments
Let's see if we can narrow the bounds just a little.
|
Makes good sense to me. Those proposals still leave the question of what to do about
|
Gate impls on
It would make porting simpler because incorrect range assumptions and overflows will be caught at compile time. |
Caught at compile time when you're porting. If we put in |
@durka Impls like |
I like the idea of having a lint if an impl is selected that's tagged with |
I don't know about wider types, but |
Maybe a set of special purpose lints? #[allow(assume_usize_ge_32_bits)]
#[allow(assume_usize_le_64_bits)] The standard library really should provide some way to safely cast under such assumptions, whether From or something else. If it doesn't, most people won't avoid making them; they'll just hide them in |
Are we actually confident this is a reasonable assumption over the next 50 years? I guess if it becomes untrue we can make a breaking change. |
Nominated for lang team discussion. |
I wrote up the @rust-lang/lang team discussion in this internals thread. |
cc #1868 |
https://en.wikibooks.org/wiki/C_Programming/stdint.h#Integers_wide_enough_to_hold_pointers claims that |
@SimonSapin: I checked the C standards, because the linked page cites the manpage, which might have been overconstrained (both C and POSIX apply constraints to some types and constants).
So yes, C's |
PR rust-lang/rust#49305 includes:
|
Perhaps all Making this work (or at least work efficiently) might require an extension to |
A possible way forward: Define some new submodules, e.g. No new language features would be required. |
Unfortunately, the idea doesn't work because |
Keep in mind that those modules wouldn't exist for targets that don't meet the limits. |
Oh, I see, you're saying that the conversions would still be possible even if the program didn't have the |
But when they do there's no way to enforce the requirement to import them.
The impls are visible regardless. I can't think of a way to do this with
imports, but maybe there is some hack with generics and specialization or
something.
…On Thu, Dec 13, 2018 at 12:11 AM Brian Smith ***@***.***> wrote:
Unfortunately, the idea doesn't work because impls don't respect module
scope like that. A portability lint is the way to go.
Keep in mind that those modules *wouldn't exist* for targets that don't
meet the limits.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1748 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAC3n463tqs3icNe7eB3OyT_HZLI1z5yks5u4eFrgaJpZM4J6_rn>
.
|
I see that Also note that there are attempts to define a "maximum object size" and so far many people have suggested that |
There is no conversion here, even implicit. A |
You and I are saying the same thing in different ways. The point is that this works for most, but not all, platforms: fn foo(n: usize) -> libc::size_t { n } In rust-lang/unsafe-code-guidelines#99 at least one person claimed that that code isn't guaranteed to work for all targets because sometimes |
I agree that this is incompatible with the way the (This is somewhat besides the point, but what are some platforms where |
A 64-bit CHERI-based platform will have 256-bit or 128-bit pointers and 64-bit (Also potentially the ordering of I am particularly interested in Rust supporting these security-oriented ABIs in the future as they become practical. |
Note that we can only control the maximum allowed size of Rust objects (
AFAICT this would only mean that the maximum allowed size of |
Sure, in theory you could define the maximum object size to be |
The exact same can be argued of |
I propose that Rust code that is targetting |
When in the course of
humanrusty events, something incore
orstd
depends on the actual width ofusize
/isize
, there are currently (at least) two policies in place:usize
may be as narrow as 8 bits.usize: From<u8> + !From<u16>
usize
is at least 32 bits wide (as it is on all current officially supported platforms).Range<u32>: ExactSizeIterator
Let me know if I missed any other corners of the standard library which make assumptions (identical to one of these or not).
As these policies are in conflict, it seems like one or both of them should be changed. In principle, we can't remove trait implementations from
Range<u32>
and the like, so we could just declaretarget_pointer_width
-liberalism to be the law of the land. However, this will make it difficult to port Rust to a 16-bit system. In doing such porting, trait implementations likeFrom<u32> for usize
andExactSizeIterator for Range<u32>
would need to be gated by a#[cfg]
. But, this would make it difficult to port Rust code from, say, a 32-bit target to a 16-bit target, because some code would stop compiling (N.B. this is already potentially the case, because literals given for enum variants are interpreted asisize
literals).So, what should we do?
The text was updated successfully, but these errors were encountered: