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

extern "C" fn with zero-sized argument? #342

Open
madsmtm opened this issue Jun 8, 2022 · 2 comments
Open

extern "C" fn with zero-sized argument? #342

madsmtm opened this issue Jun 8, 2022 · 2 comments

Comments

@madsmtm
Copy link

madsmtm commented Jun 8, 2022

C and C++ have a hard time with zero-sized types, they're basically not possible without special compiler extensions as far as I can tell? However, the following Rust code compiles fine without any warnings:

#[repr(C)]
pub struct X([u8; 0]);

extern "C" {
    fn my_fn1(_x: X);
}

pub extern "C" fn my_fn2(_x: X) {}

fn main() {
    my_fn1(X([]));
    my_fn2(X([]));
}

How do we currently handle this (as far as I can tell, they're just ignored)? Do we want to make any guarantees about how they work? Are there any calling conventions that reserve "space" for zero-sized arguments?

(I have a little bit of a use-case for this: In objc2, it would make the API slightly nicer to be able to "act" as-if a function took a specific zero-sized marker type MainThreadMarker).

If this is UB, I think we should at least extend the improper_c_types lint to warn on zero-sized arguments.

@madsmtm madsmtm changed the title Calling extern "C" fn with zero-sized argument? extern "C" fn with zero-sized argument? Jun 8, 2022
@bjorn3
Copy link
Member

bjorn3 commented Jun 8, 2022

Are there any calling conventions that reserve "space" for zero-sized arguments?

Unfortunately there are:

https://github.com/rust-lang/rust/blob/263d8682d6e01bb02727b15b1c72ffabc0e7396b/compiler/rustc_middle/src/ty/layout.rs#L3306-L3314

@CAD97
Copy link

CAD97 commented Jun 23, 2022

Also note that the specific case of a #[repr(C)] struct containing a single zero-length array is already thorny for other reasons.

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

3 participants