Conversation
|
👯 |
|
\o/ |
|
Woo! |
|
awesome 🍰 |
6bb5e2b to
b8eb099
Compare
|
f? @jld |
src/libcore/nonzero.rs
Outdated
There was a problem hiding this comment.
I don't think this is safe at all, even if right now I can't come up with a memory safety violation in safe code using it.
Though someone more motivated could potentially use it to transmute arbitrary values.
There was a problem hiding this comment.
The existence of NonZero::null does seem like a bit of an oxymoron in any case.
src/librustc_trans/trans/adt.rs
Outdated
There was a problem hiding this comment.
why special case these things? why not just call type_is_sized all the time? seems like it just makes the code less DRY
|
So, this looks basically good to me, the big thing that seems to be missing are tests that do matches and so forth on values that contain |
44ba89c to
56503a4
Compare
|
@nikomatsakis Updated. |
|
@luqmana I'm assuming there were only minor changes here? (r+ under that assumption) |
|
Actually, revoking r+. The main thing I wanted added was unit tests that test matching against a |
src/librustc_trans/trans/adt.rs
Outdated
There was a problem hiding this comment.
Nit: could this return Option<DiscrField>? (And likewise for find_ptr.) That seems to be what it's doing anyway, and it would be a little clearer to read.
There was a problem hiding this comment.
@jld It did originally but I changed it to this way to get rid of all the extraneous allocation.
There was a problem hiding this comment.
I think I expessed that badly: I meant returning an optional vector in reverse order, like it's doing now; the recursive cases would move the Vec, push onto it, and then return it. It's not a big deal in any case.
There was a problem hiding this comment.
Yep, that makes sense. I've updated the code.
49773d8 to
c0badcd
Compare
c0badcd to
766a719
Compare
This extends the nullable enum opt to traverse beyond just the first level to find possible fields to use as the discriminant. So now, it'll work through structs, tuples, and fixed sized arrays. This also introduces a new lang item, NonZero, that you can use to wrap raw pointers or integral types to indicate to rustc that the underlying value is known to never be 0/NULL. We then use this in Vec, Rc and Arc to have them also benefit from the nullable enum opt. As per rust-lang/rfcs#499 NonZero is not exposed via the `libstd` facade. ``` x86_64 Linux: T Option<T> (Before) Option<T> (After) ---------------------------------------------------------------------------------- Vec<int> 24 32 24 String 24 32 24 Rc<int> 8 16 8 Arc<int> 8 16 8 [Box<int>, ..2] 16 24 16 (String, uint) 32 40 32 ``` Fixes #19419. Fixes #13194. Fixes #9378. Fixes #7576.
|
Nice wins. |
There was a problem hiding this comment.
@luqmana: Is there any reason this couldn't have been pub struct NonZero<T: Zeroable>(pub T)?
The way it's currently defined, NonZero cannot be used to initialize statics and consts. :-(
There was a problem hiding this comment.
Because we don't have unsafe fields, this has the same issue as UnsafeCell (public safely modifiable fields can lead to unsafety).
There was a problem hiding this comment.
Is this issue explained anywhere? I must have missed that discussion.
There was a problem hiding this comment.
@vadimcn I don't think there's an extended documentation/discussion written anywhere about this but this falls into the safety guarantees. As @eddyb mentioned, there's no support for unsafe fields, therefore we don't have a way to tell the user that accessing a certain field is considered an unsafe operation.
In the case of UnsafeCell, the field is public but, as the docstring in the file says, it shouldn't be.
Now, I wonder if it'd be fair to allow calls to constructors on static items by requiring them to be in an unsafe block.
There was a problem hiding this comment.
Why accessing the inner value of NonZero is unsafe?
There was a problem hiding this comment.
I think creating, not accessing, the value is unsafe in this case: NonZero(0) is bad!
There was a problem hiding this comment.
In addition to what @huonw said: NonZero guarantees the wrapped raw pointer will never be NULL or 0. If public access to the wrapped pointer is allowed, it would be possible to zero the value out.
There was a problem hiding this comment.
@huonw, @flaper87: I can still do these things, since NonZero::new() does not perform any input validation. So what's gained? That I have to wrap it in an unsafe {} block?
This extends the nullable enum opt to traverse beyond just the first level to find possible fields to use as the discriminant. So now, it'll work through structs, tuples, and fixed sized arrays. This also introduces a new lang item, NonZero, that you can use to wrap raw pointers or integral types to indicate to rustc that the underlying value is known to never be 0/NULL. We then use this in Vec, Rc and Arc to have them also benefit from the nullable enum opt.
As per rust-lang/rfcs#499 NonZero is not exposed via the
libstdfacade.Fixes #19419.
Fixes #13194.
Fixes #9378.
Fixes #7576.