dtolnay / dtolnay/cxx

Extern type aliases to a C++ reference are not well supported

Open
#809 1 comment 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
Rust
Stars
6.8k
Forks
423
Avg merge
1h 19m
Merged PRs (30d)
6

Description

With pointers, the following works perfectly:

```cpp
// include/repro.h

#pragma once

struct Thing;

using Ref = Thing *;
```

```rust
// src/lib.rs

use cxx::{type_id, ExternType};

#[cxx::bridge]
mod ffi {
extern "C++" {
include!("example/include/repro.h");

type Ref = crate::Ref;
}

extern "Rust" {
fn f(r: Ref);
}
}

fn f(_r: Ref) {}

#[repr(transparent)]
pub struct Ref(*mut ());

unsafe impl ExternType for Ref {
type Id = type_id!("Ref");
type Kind = cxx::kind::Trivial;
}
```

However with `Ref` as a C++ reference rather than a C++ pointer, we generate C++ code that does not compile.

```diff
- using Ref = Thing *;
+ using Ref = Thing &;
```

```console
cxxbridge/sources/example/src/lib.rs.cc:60:26: error: cannot declare pointer to ‘using Ref = struct Thing&’ {aka ‘struct Thing&’}
60 | void cxxbridge1$f(::Ref *r) noexcept;
| ^
cxxbridge/sources/example/src/lib.rs.cc: In instantiation of ‘union rust::cxxbridge1::ManuallyDrop’:

cxxbridge/sources/example/src/lib.rs.cc:48:5: error: non-static data member ‘rust::cxxbridge1::ManuallyDrop::value’ in a union may not have reference type ‘Thing&’
48 | T value;
| ^~~~~
cxxbridge/sources/example/src/lib.rs.cc: In function ‘void f(Ref)’:

cxxbridge/sources/example/src/lib.rs.cc:64:45: error: cannot bind non-const lvalue reference of type ‘Thing&’ to an rvalue of type ‘std::remove_reference::type’ {aka ‘Thing’}
64 | ::rust::ManuallyDrop<::Ref> r$(::std::move(r));
| ~~~~~~~~~~~^~~
cxxbridge/sources/example/src/lib.rs.cc:49:20: note: initializing argument 1 of ‘rust::cxxbridge1::ManuallyDrop::ManuallyDrop(T&&) [with T = Thing&]’
49 | ManuallyDrop(T &&value) : value(::std::move(value)) {}
| ~~~~^~~~~
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.