-
Notifications
You must be signed in to change notification settings - Fork 15
Legal values for a Box<T> where T is zero-sized #3
Comments
If this is a legal box as well, is it safe to assume that the compiler will never decide to not propagate the given value, and just give 1 whenever asked for what the pointer value is for a boxed zero sized type? |
I'm of the opinion that Conceptually, we are allocating a zero-sized block of memory (which may have malloc headers, so the real allocation may be bigger than zero), and we should use the same pointer value when we are freeing it. The fact that |
My preference is that any non-null value is a valid pointer to a 0-sized type. Also, for things to make sense, all reborrows would have to prefer that value, e.g.: struct Foo((), ());
fn addr_of<T>(t: &T) -> usize { t as *const T as usize }
fn main() {
let foo = unsafe { &*(0x1337 as *const Foo) };
assert_eq!(addr_of(foo), 0x1337);
assert_eq!(addr_of(&*foo), 0x1337);
assert_eq!(addr_of(&foo.0), 0x1337);
assert_eq!(addr_of(&foo.1), 0x1337);
} |
@arielb1 I don't think so. edit: nvm :P |
Whatever is legal for |
@arielb1 I... don't know if I agree? I think we should be able to say that |
That would be a breaking change. |
Is it? Do we ever say that we won't allocate? I guess it is literally a breaking change... but have we made promises in this area? |
A breaking change is a change that breaks user code. If we're going around making these kinds of breaking changes we might as well reverse struct drop order and be done with it. |
@arielb1 well... I just need to write that RFC :) |
So really there are two questions here that should be separated out. One is what is legal in terms of |
My feeling is that This would mean that, for example, it may well be legal to take a (Also, of course, if Box becomes customizable with respect to its allocator, then basically all of this logic still stands, but with respect to the allocator for that given box.) |
Should Depending on the answer to that question, FWIW the current implementation returns EDIT: @rkruppe mentioned Also related to #10 |
For references and |
That makes sense, for me that settles it. Is there a memory model document anywhere where these kind of things are actually written to? |
No :( We haven't really started writing down anything yet... or even found a good structure to put stuff in. I hope to be able to work on that some more this summer. |
This is now being discussed as part of the validity invariants: rust-lang/unsafe-code-guidelines#145. |
Er- it looks like the linked thread doesn't talk at all about Boxing ZSTs, unless I'm missing something? |
On IRC, @mystor asked me whether, given some zero-sized type
ZST
, it would be legal to representBox<ZST>
using some arbitrary non-null value, or must it be a particular value (e.g., 1)?For example, if there is a pointer
p
to some stack variable, can one writeunsafe { Box::from_raw(p as *mut ZST) }
and have that be a legal box?The text was updated successfully, but these errors were encountered: