Stream: t-lang/wg-unsafe-code-guidelines

Topic: ucg#224

gnzlbg (Jan 09 2020 at 17:53, on Zulip):

@Lokathor do you have any examples in mind ?

Lokathor (Jan 09 2020 at 17:54, on Zulip):


gnzlbg (Jan 09 2020 at 17:54, on Zulip):

I am very against this because it would greatly complicate the unsafe code writing experience for users of the language.

gnzlbg (Jan 09 2020 at 17:54, on Zulip):

What gets complicated by the proposal in ucg#224 ?

gnzlbg (Jan 09 2020 at 17:55, on Zulip):

For slice references, we only support [T]::into_raw() and [T]::from_raw_parts, and currently do not support neither type punning through unions, nor the usage of transmute.

gnzlbg (Jan 09 2020 at 17:57, on Zulip):

The only complication I can think of is that (ptr, len) != [T]::into_raw([T]::from_raw(ptr, len))

gnzlbg (Jan 09 2020 at 17:58, on Zulip):

but I don't really know how it can show up, since if you have a ZST, all pointers to it will be align_of::<ZST>() already, and all pointer arithmetic on those does nothing (e.g. zst_ptr.add(N) == zst_ptr)

gnzlbg (Jan 09 2020 at 18:03, on Zulip):

I'm not sure if it is even possible to allocate a ZST in Rust at a different address, e.g., Box<ZST>::new() will return align_of, and GlobalAlloc cannot be used to allocate ZSTs.

gnzlbg (Jan 09 2020 at 18:04, on Zulip):

One can definitely call a C function that returns a pointer to a ZST at a different address, or materialize one such pointer from an usize (e.g. 42_usize as *const ZST).

bjorn3 (Jan 09 2020 at 18:06, on Zulip):

You can use &Box::new((42u8, ()).1 to get a different address, but I don't believe there is a guarantee for this.

gnzlbg (Jan 09 2020 at 18:08, on Zulip):

not for that type, but for something like #[repr(C)) struct S(u8, ()) we have such a guarantee I think

gnzlbg (Jan 09 2020 at 18:08, on Zulip):

since we do specify how the offsets are computed

Lokathor (Jan 09 2020 at 18:15, on Zulip):

i replied on the github issue, i didn't want to split the discussion into two places

Last update: Jun 04 2020 at 17:00UTC