Stream: general

Topic: transmute and repr(transparent)


Jake Goulding (Oct 21 2019 at 19:34, on Zulip):

It's safe to transmute Vec<Foo<T>> to Vec<T> if Foo is repr(transparent), yeah?

RalfJ (Oct 21 2019 at 19:50, on Zulip):

and I assume struct Foo(T)?

RalfJ (Oct 21 2019 at 19:51, on Zulip):

I think yes (but beware an extra Drop impl for Foo)

RalfJ (Oct 21 2019 at 19:51, on Zulip):

also see https://github.com/rust-lang/rfcs/pull/2756

Laurențiu Nicola (Oct 21 2019 at 19:53, on Zulip):

Should miri detect a missing #[repr(transparent)]?

RalfJ (Oct 21 2019 at 19:54, on Zulip):

no, miri doesn't detect relying on unspecified layout details

RalfJ (Oct 21 2019 at 19:55, on Zulip):

we could conceivably add a flag to randomize repr(Rust) layout and add some redundant padding or so to detect such cases, but currently we dont have anything like that

Laurențiu Nicola (Oct 21 2019 at 19:55, on Zulip):

Ah, okay

centril (Oct 21 2019 at 19:55, on Zulip):

I don't believe we've ever made a commitment wrt. type constructors in this way

RalfJ (Oct 21 2019 at 19:57, on Zulip):

well it kind of follows from repr(transparent) allowing transmutes

RalfJ (Oct 21 2019 at 19:57, on Zulip):

and the fact that Vec extensively documents its internals

RalfJ (Oct 21 2019 at 19:58, on Zulip):

if you wanted to be really safe you'd go via to/from_raw_parts

RalfJ (Oct 21 2019 at 19:58, on Zulip):

well probably you should actually, given https://github.com/rust-lang/rust-clippy/issues/4484 ^^

RalfJ (Oct 21 2019 at 19:58, on Zulip):

also see https://github.com/rust-lang/rust/issues/64073

Jake Goulding (Oct 21 2019 at 20:11, on Zulip):

and I assume struct Foo(T)?

struct Foo<T> { value: T } (but repr(transparent) enforces only one value, IIRC)

rkruppe (Oct 21 2019 at 20:18, on Zulip):

As a general principle, transmuting under type constructors is plainly incorrect: associated types and constants as well as various parametricity-piercing widgets can make Foo<Wrapper<T>> and Foo<T> differ
So it's always up to what the specific Foo (in this case Vec) guarantees

rkruppe (Oct 21 2019 at 20:19, on Zulip):

Vec could make guarantee that it only depends on size and alignment for these matters, but it currently doesn't, so the transmute is at least "library UB"

Jake Goulding (Oct 21 2019 at 20:25, on Zulip):

I wish there was a Vec::to_raw_parts

Jake Goulding (Oct 21 2019 at 20:27, on Zulip):

I'm happy I asked. I was 95% sure it was fine.

simulacrum (Oct 21 2019 at 20:28, on Zulip):

I wish there was a Vec::to_raw_parts

This feels like an API that we can and should add

Jake Goulding (Oct 21 2019 at 20:29, on Zulip):

As I said it, I was wondering what it'd return. A tuple? A little struct?

RalfJ (Oct 21 2019 at 20:29, on Zulip):

I guess a tuple would be fine

simulacrum (Oct 21 2019 at 20:32, on Zulip):

tuple, I'd say, to let you easily destructure and pass to from_raw_parts

centril (Oct 21 2019 at 20:36, on Zulip):

Seems sensible

rkruppe (Oct 21 2019 at 21:06, on Zulip):

To be easier to use correctly than just writing out (v.as_ptr(), v.len(), v.capacity()) (which requires mem::forget afterwards) it should probably be into_raw_parts(self)

Jake Goulding (Oct 22 2019 at 13:51, on Zulip):

Alrighty, I'm running the tests locally (also added it for String), will push when those pass.

Jake Goulding (Oct 22 2019 at 15:08, on Zulip):

And by "tests" I really mean building rust / LLVM so hard that my screen glitches and my machine restarted

Lokathor (Oct 22 2019 at 16:16, on Zulip):

for a PR to add those methods?

Jake Goulding (Oct 22 2019 at 16:50, on Zulip):

yep

Jake Goulding (Oct 22 2019 at 16:51, on Zulip):

https://github.com/rust-lang/rust/pull/65705

RalfJ (Oct 22 2019 at 20:04, on Zulip):

these LLVM builds also feel like they take longer and longer... and compared to earlier times I have to rebuild LLVM much more often, too

Jake Goulding (Oct 22 2019 at 20:11, on Zulip):

I'm doing ../x.py --jobs 3 test src/liballoc/ and one thing I always know is that there's some faster way to run the tests I want, but I never know what.

Jake Goulding (Oct 22 2019 at 20:11, on Zulip):

(set to 3 to allow me to use my computer for other things)

RalfJ (Oct 25 2019 at 18:20, on Zulip):

@Jake Goulding did you edit rustc at all? if you only edit libstd you can do stage 0 tests... add --stage 0

Last update: Nov 21 2019 at 23:25UTC