Skip to content

Translate structured bindings - #436

Open
lucic71 wants to merge 9 commits into
Cpp2Rust:masterfrom
lucic71:decomposition
Open

lucic71 wants to merge 9 commits into
Cpp2Rust:masterfrom
lucic71:decomposition

Conversation

@lucic71

@lucic71 lucic71 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Translate auto [a, b] = ...; a == 0; as let __decomp_N: T = ...; __decomp_N.a == 0.

Currently it only works for decompositions over user-defined records and arrays. Decompositions over std::pair or other STL types don't work because they need get<0>, get<1> rules which are blocked by #435.

@nunoplopes

nunoplopes commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

auto [x, y] = pair; should generate the same code as:

auto x = pair.first;
auto y = pair.seond

It's a copy and variables shouldn't be pointers, but rather Value<> on their own.

@lucic71

lucic71 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

auto [x, y] = pair; should generate the same code as:

auto x = pair.first;
auto y = pair.seond

It's a copy and variables shouldn't be pointers, but rather Value<> on their own.

That is not the decomposition semantics. It's: copy construct pair, access x and y on the new object.

@nunoplopes

Copy link
Copy Markdown
Contributor

The generated code should not loose the variable names. It should create the same bindings as in the original code.

@lucic71

lucic71 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

The generated code should not loose the variable names. It should create the same bindings as in the original code.

Creating individual variables is wrong. It would call the copy/move constructor of each variable which is not what auto [x, y] = pair does.

auto [x, y] = pair calls only the copy/move constructor of pair.

@nunoplopes

Copy link
Copy Markdown
Contributor

When the struct has a default copy constructor, you can decompose the clone straight away.
If there's a copy constructor, you can still introduce (Rust) mutable references to each of the fields so the names are preserved.

@lucic71

lucic71 commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

When the struct has a default copy constructor, you can decompose the clone straight away.

Does not work if the struct has a default copy ctor but the fields have custom copy ctors.

If there's a copy constructor, you can still introduce (Rust) mutable references to each of the fields so the names are preserved.

This breaks the refcount model. If each decomposed variable becomes a &mut then taking the address of the decomposed variable does not work: &mut -> Ptr does not exist.

Creating separate variables is not the C++ semantics.

@nunoplopes

Copy link
Copy Markdown
Contributor

Creating variables with the same name is important for readability. C++ injects those names on the scope.

This branch has not been deployed

No deployments
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