Skip to content

Assert that both C++ and Rust rules use the same generics#101

Merged
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
lucic71:generics
May 17, 2026
Merged

Assert that both C++ and Rust rules use the same generics#101
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
lucic71:generics

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented May 15, 2026

No description provided.

Comment thread cpp2rust/converter/translation_rule.cpp Outdated
}

// Check that both source and target use the same generics.
for (unsigned i = 0; i < generics.size(); ++i) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i = 0, e = generics.size(); i != e

Comment thread cpp2rust/converter/translation_rule.cpp Outdated

// Check that both source and target use the same generics.
for (unsigned i = 0; i < generics.size(); ++i) {
auto placeholder = std::format("T{}", i + 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of allocating memory on every generic for every rule, an alternative is to declare a global constant:
char generic_types[][] = ["T1", "T2", "T3", "T4"];

Comment thread cpp2rust/converter/translation_rule.cpp Outdated
char storage[kMaxGenerics][3]{};

constexpr GenericPlaceholders() {
for (unsigned i = 0; i < kMaxGenerics; ++i) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant a constant array, not this..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a constexpr array with kMaxGeneric elements so that we don't get hard to debug errors when kMaxGeneric changes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use an std::array if you want to make it safe. asserts aren't that need as it throws if you access OOB.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can use std::array, but the idea was not to manually write {"T1", "T2", "T3", ...}, but instead be generated automatically based on kMaxGenerics

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I replaced with std::array

@nunoplopes nunoplopes merged commit 6085fcb into Cpp2Rust:master May 17, 2026
9 checks passed
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