Skip to content

Add value category and cv qualifiers markers - #699

Merged
nunoplopes merged 15 commits into
Cpp2Rust:masterfrom
lucic71:value-category-and-cv-markers
Dec 4, 2026
Merged

nunoplopes merged 15 commits into
Cpp2Rust:masterfrom
lucic71:value-category-and-cv-markers

Conversation

@lucic71

@lucic71 lucic71 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Closes #678; removes ConstructFrom, AssignFrom and Moved from dede30b.

This PR adds LvalueRef, RvalueRef, Const and Volatile markers. They are used by the converter-rules boundary, including trait signatures like CopyConstructible(LvalueRef<Const<T>>) and MoveConstructible(RvalueRef<T>). User code continues to use Ptr for all reference types and pointers.

It also models reference collapsing and reference binding.

@nunoplopes
nunoplopes merged commit 68a125d into Cpp2Rust:master Dec 4, 2026
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.

References get pointer semantics In rules

2 participants