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

Replace zero-size structs in FFI with empty enums #442

Closed
nukep opened this issue Jul 28, 2015 · 8 comments · Fixed by #445
Closed

Replace zero-size structs in FFI with empty enums #442

nukep opened this issue Jul 28, 2015 · 8 comments · Fixed by #445

Comments

@nukep
Copy link
Contributor

nukep commented Jul 28, 2015

In Rust nightly, a bunch of warnings are being emitted for zero-size structs in sdl2-sys:
https://travis-ci.org/AngryLawyer/rust-sdl2/jobs/72803339

These are opaque types, and it isn't appropriate to add any fields to them. A mentioned alternative in rust-lang/rust#27303 is to use enum MyStruct {} instead.

@bfops
Copy link
Contributor

bfops commented Jul 31, 2015

For me, zero-size enums cause the error

sdl2-sys/src/video.rs:14:1: 14:23 error: unsupported representation for zero-variant enum [E0084]
sdl2-sys/src/video.rs:14 pub enum SDL_Window {}

@bfops
Copy link
Contributor

bfops commented Jul 31, 2015

I think c_void might make sense in these contexts, since these types are only ever used as pointers, as far as I know.

@nukep
Copy link
Contributor Author

nukep commented Jul 31, 2015

You might need to get rid of the #[repr(C)] before it, because it will cause an error.

@bfops
Copy link
Contributor

bfops commented Jul 31, 2015

Removing the #[repr(C)] just causes different warnings, since these types are part of the FFI.

@bfops
Copy link
Contributor

bfops commented Jul 31, 2015

Specifically warning: found struct without foreign-function-safe representation annotation in foreign module, consider adding a #[repr(C)] attribute to the type, #[warn(improper_ctypes)] on by default

@nukep
Copy link
Contributor Author

nukep commented Aug 1, 2015

I wasn't able to reproduce the issue. http://is.gd/8SpNgV

It won't hurt to use c_void realistically, but I like the empty enum version because that way you can only use the type via a pointer, i.e. *mut SDL_Renderer.

@bfops
Copy link
Contributor

bfops commented Aug 1, 2015

It still happens for me on the latest nightly. Did you try with actual sdl2-sys, or just in the sandbox?

As far as I know, by design, c_void can't be instantiated either (it wouldn't make much sense if it could), so it can also only be used via pointer. In my opinion, c_void makes it more explicit that the intent is for a type not to be instantiated, where an empty enum could imply that variants may be added later. I agree that it doesn't really make a difference here though.

@nukep
Copy link
Contributor Author

nukep commented Aug 1, 2015

That's weird. I only tried in the sandbox with nightly, though. shrugs

c_void works perfectly, so I won't worry about it. The only downside I can think of for using c_void is that std::mem::size_of::<c_void>() > 0, but that's not a realistic downside if c_void can't be instantiated.

bors-servo pushed a commit to servo/core-text-rs that referenced this issue Jul 5, 2017
Fix core-text related warnings (#62)

### Fix private-in-public warnings (error E0446)
Fixed by prepending `pub` to the offending types. Not sure if its the best way but it was the only thing that worked. Other methods I've tried to maintain privateness:
* Replace with an empty enum. That threw an error.
* Make it public and wrap within a private module. That threw an error too as `#[repr(C)]` does not accept modules

### Fix use of extern static warnings by wrapping in unsafe block (error E0133)
I think this is self-explanatory.

### Fix zero-size struct warnings on certain CT* types
Wrap around c_void as I believe the types are only used as pointers. Again, let me know if there is a better way to do this. I've tried following [this discussion](rust-lang/rust#27303) on opaque C types but I don't think there is a consensus. I came across this [discussion](Rust-SDL2/rust-sdl2#442) from the Rust SDL2 bindings and they eventually [opted](Rust-SDL2/rust-sdl2#445) for the solution that I implemented.

This is my first pull request (hence, first open source contribution) and I've been playing with Rust for the past few weeks. So please, if anything isnt up to standards or correctness, let me know! Also, to squash the other warnings, those changes would have to made in the modules where those types reside (core-graphics and core-foundation). I would gladly open a PR to fix those too if all looks good.

Thanks!

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-text-rs/65)
<!-- Reviewable:end -->
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 a pull request may close this issue.

2 participants