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

Codegen results in function call with incorrect number of arguments #1342

Open
paulyoung opened this issue Nov 12, 2023 · 8 comments
Open

Codegen results in function call with incorrect number of arguments #1342

paulyoung opened this issue Nov 12, 2023 · 8 comments
Labels
bug Something isn't working need-test-case Needs a test case

Comments

@paulyoung
Copy link

Describe the bug
The build fails with the following:

cargo:warning=In file included from /Users/py/projects/paulyoung/autocxx-bug/main/target/debug/build/autocxx-bug-94a6f68b4aecae68/out/autocxx-build-dir/cxx/gen0.cxx:2:

cargo:warning=/Users/py/projects/paulyoung/autocxx-bug/main/target/debug/build/autocxx-bug-94a6f68b4aecae68/out/autocxx-build-dir/include/autocxxgen_ffi.h:57:144: error: no matching function for call to 'operator new'

cargo:warning=inline void MySubclassCpp_new_autocxx_autocxx_wrapper_0xc63676881d849a16(MySubclassCpp* autocxx_gen_this, rust::Box<MySubclassHolder> arg1)  { new (autocxx_gen_this) MySubclassCpp(std::move(arg1)); }

cargo:warning=                                                                                                                                               ^   ~~~~~~~~~~~~~~~~~~

cargo:warning=include/MyClass.h:12:15: note: candidate function not viable: requires single argument 'size', but 2 arguments were provided

cargo:warning=        static void* operator new( size_t size );

cargo:warning=                     ^

To Reproduce
Try to build the project at https://github.com/paulyoung/autocxx-bug

Expected behavior
Codegen results in code supplying the correct number of arguments.

Additional context
In reality I don't control the header files so any potential workarounds would preferably not involve modifying them.

@paulyoung
Copy link
Author

I just updated the repo to use generate! instead of subclass! since I figured that would be a more minimal example, even though my use case is subclassing.

The build now fails with:

cargo:warning=In file included from /Users/py/projects/paulyoung/autocxx-bug/main/target/debug/build/autocxx-bug-94a6f68b4aecae68/out/autocxx-build-dir/cxx/gen0.cxx:2:

cargo:warning=/Users/py/projects/paulyoung/autocxx-bug/main/target/debug/build/autocxx-bug-94a6f68b4aecae68/out/autocxx-build-dir/include/autocxxgen_ffi.h:43:148: error: no matching function for call to 'operator new'

cargo:warning=inline void new_synthetic_const_copy_ctor_0x42aaa771a89cc55d_autocxx_wrapper_0x42aaa771a89cc55d(MyClass* autocxx_gen_this, const MyClass& arg1)  { new (autocxx_gen_this) MyClass(arg1); }

cargo:warning=                                                                                                                                                   ^   ~~~~~~~~~~~~~~~~~~

cargo:warning=include/MyClass.h:12:15: note: candidate function not viable: requires single argument 'size', but 2 arguments were provided

cargo:warning=        static void* operator new( size_t size );

cargo:warning=                     ^

@stevefan1999-personal
Copy link

Here's a concrete real life example (attemping to use autocxx with Half-Life SDK) stevefan1999-personal/autocxx-bug-operator-new (github.com)

@pevers
Copy link

pevers commented May 11, 2024

I just updated the repo to use generate! instead of subclass! since I figured that would be a more minimal example, even though my use case is subclassing.

The build now fails with:

cargo:warning=In file included from /Users/py/projects/paulyoung/autocxx-bug/main/target/debug/build/autocxx-bug-94a6f68b4aecae68/out/autocxx-build-dir/cxx/gen0.cxx:2:

cargo:warning=/Users/py/projects/paulyoung/autocxx-bug/main/target/debug/build/autocxx-bug-94a6f68b4aecae68/out/autocxx-build-dir/include/autocxxgen_ffi.h:43:148: error: no matching function for call to 'operator new'

cargo:warning=inline void new_synthetic_const_copy_ctor_0x42aaa771a89cc55d_autocxx_wrapper_0x42aaa771a89cc55d(MyClass* autocxx_gen_this, const MyClass& arg1)  { new (autocxx_gen_this) MyClass(arg1); }

cargo:warning=                                                                                                                                                   ^   ~~~~~~~~~~~~~~~~~~

cargo:warning=include/MyClass.h:12:15: note: candidate function not viable: requires single argument 'size', but 2 arguments were provided

cargo:warning=        static void* operator new( size_t size );

cargo:warning=                     ^

Hi @paulyoung , did you manage to solve this in the end? I'm also running into this issue.

@paulyoung
Copy link
Author

@pevers I didn't.

I thought about trying to fix the issue and submitting a PR or writing something by hand instead but my project is fairly low priority so I haven't invested any more time into it.

@paulyoung
Copy link
Author

I might at least add some failing test cases.

@adetaylor
Copy link
Collaborator

Please do!

@adetaylor adetaylor added bug Something isn't working need-test-case Needs a test case labels Mar 5, 2025
@paulyoung
Copy link
Author

@adetaylor was just thinking about doing this again yesterday.

@paulyoung
Copy link
Author

I tried following the steps at https://github.com/google/autocxx/blob/ca48925c04ae84cc3c61153fa230db9c899fcb53/book/src/reporting_bugs.md

I wasn't able to produce a reduce test case. The command in the above document appears to be out of date but even with that I kept getting an error. Perhaps I was doing something wrong.

Error:   × the include_cpp! macro couldn't be expanded into Rust bindings to C++: The 'generate' or 'generate_pod' directive for 'A' did not result in any code being generated. Perhaps this was mis-spelled or you didn't qualify the name with any namespaces?
  │ Otherwise please report a bug.

I figured the test case at https://github.com/paulyoung/autocxx-bug is probably minimal enough so moved on to the next step.


With bindgen, I ran into this:

cargo run -- ~/projects/paulyoung/autocxx-bug/main/include/MyClass.h --no-layout-tests --enable-cxx-namespaces --allowlist-type MyClass -- -std=c++14                                                                                    ✘ 101
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/bindgen /Users/py/projects/paulyoung/autocxx-bug/main/include/MyClass.h --no-layout-tests --enable-cxx-namespaces --allowlist-type MyClass -- -std=c++14`
panicked at bindgen/ir/context.rs:562:15:
libclang error; possible causes include:
- Invalid flag syntax
- Unrecognized flags
- Invalid flag arguments
- File I/O errors
- Host vs. target architecture mismatch
If you encounter an error missing from this list, please file an issue or a PR!

Apparently I needed to add the -x c++ flag:

cargo run -- ~/projects/paulyoung/autocxx-bug/main/include/MyClass.h --no-layout-tests --enable-cxx-namespaces --allowlist-type MyClass -- -x c++ -std=c++14101
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/bindgen /Users/py/projects/paulyoung/autocxx-bug/main/include/MyClass.h --no-layout-tests --enable-cxx-namespaces --allowlist-type MyClass -- -x c++ -std=c++14`
clang diag: /nix/store/j3dhkx4m14m38drh3f40bccrwrgzlsg5-libcxx-19.1.7-dev/include/c++/v1/__configuration/compiler.h:41:8: warning: "Libc++ only supports AppleClang 15 and later" [-W#warnings]
Warning: can't set `binop_separator = Back`, unstable features are only available in nightly channel.
/* automatically generated by rust-bindgen 0.71.1 */

#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
pub mod root {
    #[allow(unused_imports)]
    use self::super::root;
    #[repr(C)]
    pub struct MyClass__bindgen_vtable(::std::os::raw::c_void);
    #[repr(C)]
    #[derive(Debug)]
    pub struct MyClass {
        pub vtable_: *const MyClass__bindgen_vtable,
    }
    unsafe extern "C" {
        #[link_name = "\u{1}__ZN7MyClassC1Ev"]
        pub fn MyClass_MyClass(this: *mut root::MyClass);
    }
    impl MyClass {
        #[inline]
        pub unsafe fn new() -> Self {
            let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit();
            MyClass_MyClass(__bindgen_tmp.as_mut_ptr());
            __bindgen_tmp.assume_init()
        }
    }
    unsafe extern "C" {
        #[link_name = "\u{1}__ZN7MyClassD1Ev"]
        pub fn MyClass_MyClass_destructor(this: *mut root::MyClass);
    }
}

I've created a draft PR at #1475 with some test cases I added. I've been running them with cargo test integration_test::test_generate_class -- --exact and cargo test integration_test::test_subclass -- --exact

Some of the existing tests (e.g. test_pv_subclass_mut and test_pv_subclass_const) fail for me locally so I wasn't able to verify if these demonstrate the issue yet.

clang diag: /nix/store/j3dhkx4m14m38drh3f40bccrwrgzlsg5-libcxx-19.1.7-dev/include/c++/v1/__configuration/compiler.h:41:8: warning: "Libc++ only supports AppleClang 15 and later" [-W#warnings]

thread 'integration_test::test_pv_subclass_mut' panicked at integration-tests/src/lib.rs:272:6:
called `Result::unwrap()` on an `Err` value: AutoCxx(ParseError(AutocxxCodegenError(Bindgen(ClangDiagnostic("/nix/store/j3dhkx4m14m38drh3f40bccrwrgzlsg5-libcxx-19.1.7-dev/include/c++/v1/cstdint:159:1: error: expected identifier or '{'\n/nix/store/j3dhkx4m14m38drh3f40bccrwrgzlsg5-libcxx-19.1.7-dev/include/c++/v1/cstdint:159:1: error: C++ requires a type specifier for all declarations\n/nix/store/j3dhkx4m14m38drh3f40bccrwrgzlsg5-libcxx-19.1.7-dev/include/c++/v1/cstdint:159:1: error: expected expression\n/var/folders/f1/rffgvyz94lqc3kl6wtx18w6w0000gn/T/.tmpgifwQG/input.h:4:1: error: expected ';' after top level declarator\n")))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working need-test-case Needs a test case
Projects
None yet
Development

No branches or pull requests

4 participants