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

The RBox SharedStableAbi implementation should have IsNonZeroType == True #36

Open
afranchuk opened this issue Jul 20, 2020 · 4 comments

Comments

@afranchuk
Copy link
Contributor

Unless I'm missing something, isn't it the case that RBox should have IsNonZeroType as True rather than False?

@rodrimati1992
Copy link
Owner

The reason I haven't done it yet is because I haven't seen documentation guaranteeing that it has a stable representation across compiler versions.

#[repr(C)]
pub struct RBox<T>{
    ptr: std::ptr::NonNull<T>,
    vtable: *const [T; 0],
}

extern {
    pub fn foo(_: RBox<usize>);
    pub fn bar(_: Option<RBox<usize>>);
}

And the compiler warns with this message:

warning: `extern` block uses type `std::option::Option<RBox<usize>>`, which is not FFI-safe
 --> src/lib.rs:9:19
  |
9 |     pub fn bar(_: Option<RBox<usize>>);
  |                   ^^^^^^^^^^^^^^^^^^^ not FFI-safe
  |
  = note: `#[warn(improper_ctypes)]` on by default
  = help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
  = note: enum has no representation hint

warning: 1 warning emitted

But this lint is known to have some false positives, so it might actually be ffi-safe.

@afranchuk
Copy link
Contributor Author

Ah, understood. Yeah I think that they are being overly cautious there. After reading the documentation of IsNonZeroType, I just expected RBox to be able to satisfy that (it has a unique representation for zero). Otherwise the documentation for NonNull states that Option<NonNull<T>> is the same as *mut T, so that seems like it declares a stable representation across compiler versions (though it'd be best to verify against the standard library specification).

@rodrimati1992
Copy link
Owner

I decided to ask the question in the zulip unsafe-code-guidelines chat ,it looks like it's not guaranteed.

@afranchuk
Copy link
Contributor Author

Ah, thanks for looking further into it, that's a revealing discussion.

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

No branches or pull requests

2 participants