Skip to content

Avoid hoisting for function call arguments that don't alias#203

Merged
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:alias
Jun 23, 2026
Merged

Avoid hoisting for function call arguments that don't alias#203
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:alias

Conversation

@lucic71

@lucic71 lucic71 commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

In terms of converter, the PR is small. I moved GetAllVars and MayCauseBorrowMutError from converter to converter_lib because they are converter-agnostic. Then I added the aliasing logic in CollectCallInfo 2 functions in converter_lib: ReferencesThis and ArgsMayAlias.

The core idea is: if an argument of a function call does not alias with any other argument, then it can be inlined instead of hoisted. This includes aliases over this, for example this.method(this.member)

Comment thread cpp2rust/converter/converter_lib.cpp Outdated
vars.insert(decl_ref->getDecl());
} else if (auto *member = clang::dyn_cast<clang::MemberExpr>(stmt)) {
vars.insert(member->getMemberDecl());
auto child_vars = GetAllVars(member->getBase());

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.

It's better to split this method in two: one that returns the set (just calls the 2nd function), and another that returns void and received an additional set parameter and adds stuff there.
This avoid allocating an extra set on the recursive call.

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

@nunoplopes nunoplopes merged commit b48802c into Cpp2Rust:master Jun 23, 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