autocxx icon indicating copy to clipboard operation
autocxx copied to clipboard

Inline method returning a reference type fails to compile due to incorrect cast to pointer type

Open vlad-rw opened this issue 1 year ago • 3 comments

Describe the bug

Disclaimer: Not a C++ expert, please amend the description if incorrect

To Reproduce

#[test]
fn test_reference_const_char_cast() {
    let hdr = indoc! {"
        #include <cstddef>

        class Bytes {
          public:
            typedef char value_type;
            typedef std::size_t size_type;
            typedef const char& reference;
          private:
            const value_type *d_ptr;
            size_type d_length;
          public:
            reference front() const;
        };

        inline Bytes::reference Bytes::front() const
        {
            return d_ptr[0];
        }
    "};
    let rs = quote! {};
    run_test("", hdr, rs, &["Bytes"], &[]);
}

Fails with:

target/cxx/gen0.cxx:137:26: error: cannot initialize a variable of type 'const char *(Bytes::*)() const' with an rvalue of type 'Bytes::reference (Bytes::*)() const': different return type ('const char *' vs 'Bytes::reference' (aka 'const char &'))  char const *(::Bytes::*front$)() const = &::Bytes::front;

This example taken from a vendor library header (and minimised).

Expected behavior

It compiles and returns a reference.

Additional context

Apologies if this is actually the same issue as something else - I tried to search the issues and this seems original.

vlad-rw avatar Jun 23 '23 16:06 vlad-rw

Thanks very much for minimizing the test case - I can reproduce this.

adetaylor avatar Jun 27 '23 10:06 adetaylor

I've done a bit of work here but I'm not likely to complete this soon, so writing up progress for my future reference.

  • autocxx_bindgen changes here
  • autocxx changes in #1308

Current status is that this correctly causes a function to return a reference type. But we also now generate

pub type Bytes_reference = & :: std :: os :: raw :: c_char;

so we'd need to parameterize the type with a lifetime.

adetaylor avatar Jun 28 '23 17:06 adetaylor

Yuck. I almost wish past-me hadn't written up such a helpful comment. It's full of bad news.

adetaylor avatar Aug 25 '23 12:08 adetaylor