Skip to content

Create storage in caller for by-value non-trivial arguments - #694

Merged
nunoplopes merged 15 commits into
Cpp2Rust:masterfrom
lucic71:by-value-args-in-caller-storage
Dec 2, 2026
Merged

nunoplopes merged 15 commits into
Cpp2Rust:masterfrom
lucic71:by-value-args-in-caller-storage

Conversation

@lucic71

@lucic71 lucic71 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

For non-trivial by-value arguments, create the storage in the caller and pass a Ptr<T> insted of T to the function. Trivial by-value arguments continue to pass T.

Pass a Ptr<T> instead of Value<T> to the callee so that the caller continues having ownership. This is important later when we fix the destructor calls.

@nunoplopes
nunoplopes merged commit a554a35 into Cpp2Rust:master Dec 2, 2026
5 of 9 checks passed
nunoplopes pushed a commit that referenced this pull request Dec 4, 2026
Replace CppFrom with ConstructibleFrom and ConvertibleTo. Closes #700 

ConstructibleFrom models `T t(args...)` and ConvertibleTo models `To t =
from`.

---

Each constructor now implements `Constructor<Args>`, where `Args` is the
tuple of its parameter types:

```cpp
struct S {
  S(const S &);
  S(NonTrivial, S *);
  S(NonTrivial, const S *);
};
```

```rust
impl Constructor<(LvalueRef<Const<S>>,)> for S { ... }                           // S(const S &)
impl Constructor<(ParameterObject<NonTrivial>, Pointer<S>)> for S { ... }         // S(NonTrivial, S *)
impl Constructor<(ParameterObject<NonTrivial>, Pointer<Const<S>>)> for S { ... }  // S(NonTrivial, const S *)
```

One `Constructor` impl per constructor means two constructors must not
have the same Rust parameter types. Parameters use markers to
distinguish between overloads:

- `LvalueRef<T>`, `LvalueRef<Const<T>>`, etc (added in #699)
- `Pointer<T>` and `Pointer<Const<T>>` for `T *` and `const T *`. I
preferred a new Pointer marker instead of Ptr because `Ptr<Const<T>>`
requires the slow reinterpret_cast to convert to `Ptr<T>`
- `ParameterObject<T>` for a by-value argument passed as reference. The
mechanism was added in #694, ParameterObject is added in this PR because
otherwise it clashes with LvalueRef

The special constructors are blanket impls over `Constructor`:

- `DefaultConstructible`: `Constructor<()>`
- `CopyConstructible`: `Constructor<(LvalueRef<Const<Self>>,)>`
- `MoveConstructible`: `Constructor<(RvalueRef<Self>,)>`

---

`Constructor<Args>` only matches exactly. `ConstructibleFrom` and
`ConvertibleTo` must also do overload resolution, which Rust traits
can't express. So both resolve overloads at run time
(`libcc2rs/src/overload.rs`), using a macro called `Conversions` that
the converter fills:

```rust
#[derive(Conversions)]
#[conversions(copy)]
#[constructor(constructor_0, LvalueRef<Const<S>>)]
#[constructor(constructor_1, ParameterObject<NonTrivial>, Pointer<S>)]
#[constructor(constructor_2, ParameterObject<NonTrivial>, Pointer<Const<S>>)]
#[conversion_function(to_i32, LvalueRef<Const<S>>, i32)]   // operator int() const
pub struct S { ... }
```

For example, the `vector::insert(pos, first, last)` rule mentioned in
#700 constructs each element from `*it`, as C++ does:

```rust
fn f101<T1, T3, T4>(
    o: Ptr<Vec<T1>>,
    pos: RandomAccessIterator<T1>,
    first: T3,
    last: T3,
) -> RandomAccessIterator<T1>
where
    T1: ConstructibleFrom<(LvalueRef<Const<T4>>,)>,
    T3: CppInputIterator<T4>,
{
    // for each element, from *it:
    ConstructibleFrom::construct(
        element,
        (LvalueRef::<Const<T4>>::bind(it.iter_deref()),),
    );
}
```

So these now behave as in C++:

```cpp
struct Ratio { int num; operator int() const; };
struct Level { int raw; operator short() const; operator long() const; };

std::vector<long> longs;
longs.insert(longs.end(), ratios, ratios + 2);  // operator int, then int -> long

std::vector<int> ints;
ints.insert(ints.end(), levels, levels + 2);    // operator short: short -> int is a promotion,
                                                // better than long -> int
```

---

The runtime overload resolution algorithm in libcc2rs is not complete.
It mostly covers what is needed for the initialisation sequences of
ConvertibleTo and ConstructibleFrom, some notable things that are
missing and we can add later:
* add deleted functions in the candidate list
* prefer non-tempaltes functions over instantiations coming from
templates
* enum promotions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants