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

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

Open
vlad-rw opened this issue Jun 23, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@vlad-rw
Copy link

vlad-rw commented Jun 23, 2023

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.

@adetaylor
Copy link
Collaborator

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

adetaylor added a commit that referenced this issue Jun 27, 2023
adetaylor added a commit that referenced this issue Jun 28, 2023
@adetaylor
Copy link
Collaborator

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.

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
Copy link
Collaborator

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

@adetaylor adetaylor added the bug Something isn't working label Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants