Skip to content

Zero-sized structs should be FFI-safe #17679

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

Closed
mzabaluev opened this issue Oct 1, 2014 · 9 comments
Closed

Zero-sized structs should be FFI-safe #17679

mzabaluev opened this issue Oct 1, 2014 · 9 comments

Comments

@mzabaluev
Copy link
Contributor

If I get a struct by pointer from a foreign library, and I know the structure cannot safely be copied because the actual object is larger than the struct, I want to declare it as something like this:

#[repr(C)]
struct A {
    // ...
    no_copy: std::kinds::marker::NoCopy;
}

Now the compiler complains at use site that the struct is not foreign-function-safe.
As the markers are zero-sized, they have no bearing on the layout and no consequences for the structure data, so they, as long as any other zero-sized structures, should not waive FFS.

@thestinger suggests the same should apply to structures with one field as well, but I'm not so certain about that: there could be optimizations regarding alignment which would not be compatible with the (default) C ABI.

@mzabaluev mzabaluev changed the title Zero-sized structs should be foreign-function-safe Zero-sized structs should be FFI-safe Oct 1, 2014
@zwarich
Copy link

zwarich commented Oct 4, 2014

Strictly speaking, the grammar for the C language does not allow you to define structs with no members, and the specification confusingly claims separately that they have undefined behavior. The C++ language allows them, but requires that they have nonzero size.

@mzabaluev
Copy link
Contributor Author

@zwarich: This is about struct definitions in Rust which don't have to follow C word by word.

A tool for converting Rust struct definitions into C or C++ ought to be able to detect and remove zero-size members.

@thestinger
Copy link
Contributor

GNU C supports zero-size structs, and they are zero-size (unlike C++ / GNU C++). It's similar to the situation with packed structs which aren't defined by standard C either.

@mzabaluev
Copy link
Contributor Author

There may be a special case for zero-size structs by themselves being FFI-unsafe when used by value.

@kornelski
Copy link
Contributor

Related #20660

I'm using 0-sized structures to represent unknown C types (C's struct foo; without definition), so I'm hoping that pointers to such structs are OK.

@thestinger
Copy link
Contributor

@pornel: Rust has truly opaque types (enum foo {}) so there's no need to abuse zero-size types for that.

@thestinger
Copy link
Contributor

Raw pointers to anything are FFI safe, since they have a uniform representation.

@kornelski
Copy link
Contributor

(edit: nevermind the compile problem)

I've been assuming struct foo; is more appropriate, since it supports neater syntax without {}.

@steveklabnik
Copy link
Member

I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.

This issue has been moved to the RFCs repo: rust-lang/rfcs#760

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

5 participants