Skip to content

Distinguish between empty match and non-match#103

Merged
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:matching
May 17, 2026
Merged

Distinguish between empty match and non-match#103
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:matching

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented May 16, 2026

After 4cb7e30, the matcher inside mapper.cpp broke for the following case:

template:     "T1 & std::vector<T1>::back()"
instantiated: "& std::vector<int> & std::vector<std::vector<int>>::back()"

Leading & comes from the call inside VisitUnaryOperator

The first T1 is matched to the empty string:

T1 & std::vector<   T1>::back()
   & std::vector<   int> & std::vector<std::vector<int>>::back()

Then, when the second T1 is matched (int> & std::vector<std::vector<int>), it goes to the else branch because it believes that the first T1 was never encountered:

if (!repl.empty()) {
size_t end_pos = 0;
if (!matchLiteralAt(instantiated, si, repl, end_pos)) {
return std::nullopt;
}
si = end_pos;
} else {
if (!nextLit.empty()) {

The fix is to replace the captured std::vector<std::string> with std::vector<std::optional<std::string>> so that empty matched string can be distinguished from non-matched stirng.

@lucic71 lucic71 marked this pull request as draft May 16, 2026 13:17
@lucic71 lucic71 marked this pull request as ready for review May 16, 2026 14:17
@nunoplopes nunoplopes merged commit 91fc4be 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