cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Duplicate const generated on cxx::bridge file in C++

Open andfoy opened this issue 4 years ago • 2 comments

Thanks for your work on this useful library! Right now I'm using cxx::bridge to provide Elixir bindings for PyTorch via Rustler (https://github.com/andfoy/extorch). I'm trying to use a rust::Slice with std::shared_ptr<CrossTensor>, where CrossTensor is a C++ opaque type as follows:

/// Index a tensor using a list of tensors.
fn index(
    tensor: &SharedPtr<CrossTensor>,
    indices: &[&SharedPtr<CrossTensor>]
) -> Result<SharedPtr<CrossTensor>>;

Where index has the following signature:

std::shared_ptr<CrossTensor> index(
    const std::shared_ptr<CrossTensor> &tensor,
    rust::Slice<const std::shared_ptr<CrossTensor>&> indices);

When I try to compile the library, it seems that the code generator is producing a bridge signature with duplicate const keywords, which causes compilation errors:

::rust::repr::PtrLen cxxbridge1$index(const ::std::shared_ptr<::CrossTensor> &tensor, ::rust::Slice<const const ::std::shared_ptr<::CrossTensor> &> indices, ::std::shared_ptr<::CrossTensor> *return$) noexcept 

I don't know if the &[&SharedPtr<CrossTensor>] declaration is malformed or if I should not use a slice in this case. I would appreciate any help with this issue.

andfoy avatar Apr 13 '21 20:04 andfoy

This looks similar to #750. We will need to generate something like rust::Slice<const std::shared_ptr<CrossTensor> &const> (I think...).

  • One const is the one in const shared_ptr<T> & corresponding to your &SharedPtr<T>;
  • The other const is the one in rust::Slice<const T> corresponding to your &[T].

dtolnay avatar Apr 13 '21 21:04 dtolnay

I've just opened https://github.com/dtolnay/cxx/pull/831, that should fix the duplicate const const.

andfoy avatar Apr 13 '21 22:04 andfoy