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

Bindings to std::function produce poor error messages #1279

Open
mukunth1987 opened this issue May 2, 2023 · 2 comments
Open

Bindings to std::function produce poor error messages #1279

mukunth1987 opened this issue May 2, 2023 · 2 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@mukunth1987
Copy link

I am trying to call c++ function which takes std::function as argument. But std::function is being interpreted as std::array

Below is my code.

test.h

#include "cxx.h"
#include <functional> 

class A {
  public:
     A() {}
     using RespHandler = std::function<void(int val)>;
     void sendRequest(RespHandler handler) {
          // handler(100);
     }
}

main.rs

use autocxx::prelude::*; 

include_cpp! {
    #include "test.h"
    safety!(unsafe)
    generate!("A")
} 

fn main() {
}

Cargo build error:

gen0.cxx: error: cannot convert 'void (A::*) (A::RespHandler)' {aka 'void (A::*) (std::function<void(int)>)'} to 'void (A::*) (std::array<long unsigned int, 4>)' in initialization

Specifications

autocxx = "0.25.0"
cxx = "1.0.94"

Could you please help if i miss something ?

@adetaylor
Copy link
Collaborator

Thanks - this is, I think, likely to be a limitation for a long time (I believe bindgen doesn't give us the right information, and even if it did, cxx does not yet support std::function. However these diagnostics are not OK, and I regard that as a bug.

@adetaylor adetaylor changed the title Bindings to std::function Bindings to std::function produce poor error messages May 2, 2023
@adetaylor adetaylor added bug Something isn't working good first issue Good for newcomers labels May 2, 2023
@adetaylor
Copy link
Collaborator

FWIW when testing this by adding your code into the demo, I get something different. The build process says:

  Error:   × cxx couldn't handle our generated bindings - could be a bug in autocxx:
    │ unsupported type

This is because we are including this in the #[cxx::bridge] mod:

pub fn sendRequest(self: Pin<&mut A>, handler: [root::u128; 3usize]);

Solutions to improve this, then, would be one or several of the following:

  • In bindgen, output something more useful than an array when it encounters such a type.
  • In autocxx, during the function analysis phase, if we find a function has an array argument, then reject that function with a marginally more useful message. ("Arrays are not yet supported by cxx. Sometimes bindgen can generate arrays if it's not sure how to interpret a type, so the problem might be some other type not yet supported by bindgen.")
  • Either way, include span information from bindgen so that we can point to a more useful location in our error messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants