Skip to content

Make vtables non-zero-size to fix a rustc warning. #597

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

Merged
merged 2 commits into from
Mar 21, 2017
Merged

Conversation

SimonSapin
Copy link
Contributor

warning: found non-foreign-function-safe member in struct marked #[repr(C)]: found zero-size struct in foreign module, consider adding a member to this struct

Emilio said on IRC:

the empty vtable means that we don't care of figuring out the proper vtable layout, so we create an empty struct

Sounds like all that matters is to have a pointer, we don’t look at the data behind it. Using c_void seems appropriate, then.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fitzgen
Copy link
Member

fitzgen commented Mar 20, 2017

r=me

@fitzgen fitzgen closed this Mar 20, 2017
@fitzgen fitzgen reopened this Mar 20, 2017
@SimonSapin
Copy link
Contributor Author

SimonSapin commented Mar 20, 2017

This fails with:

   Compiling tests_expectations v0.1.0 (file:///home/simon/projects/servo-deps/rust-bindgen/tests/expectations)
error[E0277]: the trait bound `std::os::raw::c_void: std::default::Default` is not satisfied
  --> tests/vtable_recursive_sig.rs:27:33
   |
27 | pub struct Base__bindgen_vtable(::std::os::raw::c_void);
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `std::os::raw::c_void`
   |
   = note: required by `std::default::Default::default`

I think it’s some CanDeriveDefault impl incorrectly returning true? I don’t really understand these.

@emilio
Copy link
Contributor

emilio commented Mar 20, 2017

Seems like that would prevent us from deriving Default for every type with virtual functions.

What about making it Vtable(*const c_void) instead? That would have a similar effect, and also grant proper alignment in the vtable struct (as useless as it is in the current state of affairs though).

@SimonSapin
Copy link
Contributor Author

How was Default derived before? What was the vtable pointer set to?

@SimonSapin
Copy link
Contributor Author

Specifically, the code generated before this PR was:

#[repr(C)]
#[derive(Default)]
pub struct Base__bindgen_vtable {
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct Base {
    pub vtable_: *const Base__bindgen_vtable,
}

Base does not derive Default, and can’t because *const _ does not implement Default. Why is Default for Base__bindgen_vtable useful?

@emilio
Copy link
Contributor

emilio commented Mar 21, 2017

It's null. The utility of Default is very debatable, that's why it's not default (no pun intended).

@SimonSapin
Copy link
Contributor Author

Base__bindgen_vtable: Default is not needed for Base::vtable_ to be set to null (however wrong that it), right?

@emilio
Copy link
Contributor

emilio commented Mar 21, 2017

Right, but it's needed to derive Default in Base.

@SimonSapin
Copy link
Contributor Author

Is it? That would require Default for *const Base__bindgen_vtable, not for Base__bindgen_vtable

@@ -716,7 +716,9 @@ impl<'a> CodeGenerator for Vtable<'a> {
.item()
.pub_()
.with_attrs(attributes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, you're totally right, you only need to remove the if a few lines above this one that pushes the Default attr :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

```
warning: found non-foreign-function-safe member in struct marked #[repr(C)]: found zero-size struct in foreign module, consider adding a member to this struct
```
@emilio
Copy link
Contributor

emilio commented Mar 21, 2017

@bors-servo r=fitzgen,emilio

Thanks!

@bors-servo
Copy link

📌 Commit f67967a has been approved by fitzgen,emilio

bors-servo pushed a commit that referenced this pull request Mar 21, 2017
Make vtables non-zero-size to fix a rustc warning.

```
warning: found non-foreign-function-safe member in struct marked #[repr(C)]: found zero-size struct in foreign module, consider adding a member to this struct
```

Emilio said on IRC:
> the empty vtable means that we don't care of figuring out the proper vtable layout, so we create an empty struct

Sounds like all that matters is to have a pointer, we don’t look at the data behind it. Using `c_void` seems appropriate, then.
@bors-servo
Copy link

⌛ Testing commit f67967a with merge b4e3be9...

@bors-servo
Copy link

☀️ Test successful - status-travis
Approved by: fitzgen,emilio
Pushing b4e3be9 to master...

@SimonSapin SimonSapin deleted the void-vtable branch March 21, 2017 14:16
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.

5 participants