Skip to content

When only generating functions, type parameter is lost #848

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
2 tasks
fitzgen opened this issue Jul 25, 2017 · 19 comments
Closed
2 tasks

When only generating functions, type parameter is lost #848

fitzgen opened this issue Jul 25, 2017 · 19 comments

Comments

@fitzgen
Copy link
Member

fitzgen commented Jul 25, 2017

We still have more issues like #835...

Input C/C++ Header

  • TODO

Bindgen Invocation

  • TODO

Actual Results

https://treeherder.mozilla.org/logviewer.html#?job_id=117196247&repo=try&lineNumber=6725

error[E0243]: wrong number of type arguments: expected 1, found 0
   --> /home/worker/workspace/build/src/obj-firefox/toolkit/library/x86_64-unknown-linux-gnu/release/build/style-077f5b469d68e8b7/out/gecko/bindings.rs:526:14
    |
526 |      -> *mut nsTArray;
    |              ^^^^^^^^ expected 1 type argument
error[E0243]: wrong number of type arguments: expected 1, found 0
   --> /home/worker/workspace/build/src/obj-firefox/toolkit/library/x86_64-unknown-linux-gnu/release/build/style-077f5b469d68e8b7/out/gecko/bindings.rs:529:65
    |
529 |     pub fn Gecko_DestroyAnonymousContentList(anon_content: *mut nsTArray);
    |                                                                 ^^^^^^^^ expected 1 type argument
error[E0243]: wrong number of type arguments: expected 1, found 0
    --> /home/worker/workspace/build/src/obj-firefox/toolkit/library/x86_64-unknown-linux-gnu/release/build/style-077f5b469d68e8b7/out/gecko/bindings.rs:1073:53
     |
1073 |     pub fn Gecko_ResizeTArrayForStrings(array: *mut nsTArray, length: u32);
     |                                                     ^^^^^^^^ expected 1 type argument
error: aborting due to 3 previous errors
error: Could not compile `style`.

Expected Results

nsTArray should have a type argument.

@fitzgen
Copy link
Member Author

fitzgen commented Jul 25, 2017

cc @emilio @Manishearth

@fitzgen
Copy link
Member Author

fitzgen commented Jul 25, 2017

This is really hard to reproduce. I've preprocessed all of nsTArray.h and added nsTArray_Simple and Gecko_ResizeTArrayForStrings to the bottom and it doesn't repro.

Trying reproducing with the full servo bindings next.

@fitzgen
Copy link
Member Author

fitzgen commented Jul 25, 2017

Blech I can't even reproduce with the full preprocessed servo bindings.

@upsuper
Copy link
Contributor

upsuper commented Jul 27, 2017

OK, I eventually figured out a real way to reproduce it...

@upsuper
Copy link
Contributor

upsuper commented Jul 27, 2017

And this is reproducible on Windows as well. I have no idea why it didn't cause any issue on other platforms when with Gecko...

@upsuper
Copy link
Contributor

upsuper commented Jul 27, 2017

You need two files, in different directory. Let's say input.hpp and subdir/another.h, they have code:

// input.hpp
#include "another.h"
// subdir/another.h
template<typename T>
class nsTArray;

/**
 * <div rustbindgen replaces="nsTArray"></div>
 */
template<typename T>
class nsTArray_Simple {
  T* m;
};

extern "C" {
nsTArray<int>* func();
}

Then invoke bindgen with:

bindgen input.hpp -- -I subdir

@upsuper
Copy link
Contributor

upsuper commented Jul 27, 2017

If you move all the code from subdir/another.h into input.hpp directly, then this bug wouldn't appear, that is why a fully preprocessed file doesn't trigger this bug.

@upsuper
Copy link
Contributor

upsuper commented Jul 27, 2017

What seem to be necessary for triggering this bug are:

  • The other header file need to be searched from a directory specified by -I. If you use "subdir/another.h" directly in input.hpp, it doesn't happen.
  • The replacing type (nsTArray_Simple here) must be in the header file mentioned above.

What doesn't seem to be related:

  • Where are other things, including the replaced type (nsTArray) and the function. They can be in either file and the result wouldn't change.
  • Whether the replaced type is a definition or a declaration.
  • The order of things, as far as nsTArray is declared before the function.

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

Thanks a lot for the investigation an the test-case Xidorn, trying to repro now.

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

So, I can repro, but the generated code is:

/* automatically generated by rust-bindgen */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct nsTArray {
    pub mBuffer: *mut ::std::os::raw::c_void,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct nsTArray_Simple<T> {
    pub m: *mut T,
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
extern "C" {
    pub fn func() -> *mut nsTArray;
}

Which means that we're not managing to see the replacement. Does that match what you see @upsuper?

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

I think I know why this is...

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

So, we don't even find the replacement annotation, which is surprising, at a glance. I'm trying to bisect when this broke.

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

I can confirm that v0.26.1 works, so far.

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

Well, this is fun:

$ g bisect bad                                                                                                                                                                             
ee478a123eec1141e7555ec84eff8a10723d0e00 is the first bad commit
commit ee478a123eec1141e7555ec84eff8a10723d0e00
Author: Shing Lyu <[email protected]>
Date:   Sat Jul 1 01:38:01 2017 +0800

    Passing additional clang arguments for Linux 32 cross compiling

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

I can confirm that reverting that patch fixes the issue for me... Now, I'm going to see if this is a regression in clang-sys itself (that patch pulled a clang-sys upgrade), or on the options we pass.

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

So, I can confirm that passing empty flags to the new clang_sys also fixes this, so I bet what is happening is that clang-sys is returning all the paths, not only the system ones, and we're adding them as -isystem when there's no --target argument (which explains why this works on OSX and windows, cc @Manishearth).

I bet clang does different stuff when parsing system headers which makes us not recognise annotations.

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

So clang-4.0 gives me, as expected:

$ clang -E -x c++ - -v -Iinclude                                                                                                                                                           
clang version 4.0.0 (tags/RELEASE_400/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/7
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
 "/usr/bin/clang-4.0" -cc1 -triple x86_64-unknown-linux-gnu -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name - -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/bin/../lib64/clang/4.0.0 -I include -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/7/../../../../include/c++/7 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/7/../../../../include/c++/7/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/7/../../../../include/c++/7/backward -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/clang/4.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /home/emilio/projects/moz/rust-bindgen -ferror-limit 19 -fmessage-length 190 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o - -x c++ -
clang -cc1 version 4.0.0 based upon LLVM 4.0.0 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 include
 /usr/bin/../lib/gcc/x86_64-redhat-linux/7/../../../../include/c++/7
 /usr/bin/../lib/gcc/x86_64-redhat-linux/7/../../../../include/c++/7/x86_64-redhat-linux
 /usr/bin/../lib/gcc/x86_64-redhat-linux/7/../../../../include/c++/7/backward
 /usr/local/include
 /usr/bin/../lib64/clang/4.0.0/include
 /usr/include
End of search list.

Which means that we're incorrectly promoting -Iinclude to -isystem include.

@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

I've investigated more, and the key is that clang doesn't try to parse documentation comments in system includes.

@emilio emilio self-assigned this Jul 27, 2017
@emilio
Copy link
Contributor

emilio commented Jul 27, 2017

Got a fix for this... Maybe we want to rethink all the path fixup, but it is needed so far to cross-compile so...

emilio added a commit to emilio/rust-bindgen that referenced this issue Jul 27, 2017
emilio added a commit to emilio/rust-bindgen that referenced this issue Jul 27, 2017
bors-servo pushed a commit that referenced this issue Jul 27, 2017
lib: Filter out include paths when looking for clang paths.

Fixes #848
emilio added a commit to emilio/rust-bindgen that referenced this issue Jul 27, 2017
bors-servo pushed a commit that referenced this issue Jul 28, 2017
lib: Filter out include paths when looking for clang paths.

Fixes #848
tmfink pushed a commit to tmfink/rust-bindgen that referenced this issue Aug 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants