Skip to content
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

Gc::from_raw: Rely only on documented guarantees to compute layout #125

Merged
merged 2 commits into from
Feb 9, 2021

Conversation

andersk
Copy link
Collaborator

@andersk andersk commented Jan 30, 2021

The claim “Because it is ?Sized, it will always be the last field in memory” was not reliable: the ?Sized type may in fact be Sized (which the compiler will know after monomorphization), and in any case, the language provides no guarantees about the layout of repr(Rust) structs.

Switch GcBox to repr(C), and rewrite the layout computation based on Layout::extend, which has a documented guarantee to match the layout of repr(C) structs.

(RcBox in the standard library is repr(C) for the same reason.)

Cc @ravern

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
The claim “Because it is ?Sized, it will always be the last field in
memory” was not reliable: the ?Sized type may in fact be Sized (which
the compiler will know after monomorphization), and in any case, the
language provides no guarantees about the layout of repr(Rust)
structs.

Switch GcBox to repr(C), and rewrite the layout computation based on
Layout::extend, which has a documented guarantee to match the layout
of repr(C) structs.

(RcBox in the standard library is repr(C) for the same reason.)

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
@ravern
Copy link
Contributor

ravern commented Jan 31, 2021

Learnt something new today, thanks! 😀

The fix looks good, but I'm not in a position to test it out right now. I'm also not a contributor so will defer to @Manishearth to approve and merge.

@Manishearth
Copy link
Owner

the ?Sized type may in fact be Sized (which the compiler will know after monomorphization), and in any case, the language provides no guarantees about the layout of repr(Rust) structs.

Technically this can be enforced via CoerceUnsized, but yeah this is nicer

@Manishearth Manishearth merged commit c9d3803 into Manishearth:master Feb 9, 2021
@Manishearth
Copy link
Owner

Manishearth commented Feb 9, 2021

Thanks! A lot of this code was written before Layout, so nice to have better code!

@andersk andersk deleted the layout branch April 3, 2021 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants