Skip to content

Rewrite compound assignment in a single expression#120

Merged
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:assignment
May 20, 2026
Merged

Rewrite compound assignment in a single expression#120
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:assignment

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented May 18, 2026

No description provided.

@nunoplopes
Copy link
Copy Markdown
Contributor

Uhm, it seems that computing the ptr and storing it in a temporary variable is a good thing for long expressions. This change regresses the performance in those cases. Can we keep that part?

@lucic71
Copy link
Copy Markdown
Contributor Author

lucic71 commented May 18, 2026

Uhm, it seems that computing the ptr and storing it in a temporary variable is a good thing for long expressions. This change regresses the performance in those cases. Can we keep that part?

So it should be:

{
  let _ptr = ptr.clone;
  _ptr.write(_ptr.read() + 1)
}

?

@lucic71
Copy link
Copy Markdown
Contributor Author

lucic71 commented May 18, 2026

Uhm, it seems that computing the ptr and storing it in a temporary variable is a good thing for long expressions. This change regresses the performance in those cases. Can we keep that part?

So it should be:

{
  let _ptr = ptr.clone;
  _ptr.write(_ptr.read() + 1)
}

?

I did this

" ", rhs_as_string, "; __ptr.write(__tmp) }");
{
PushBrace brace(*this);
StrCat(std::format("let _ptr = {}.clone();", ptr));
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.

why doesn't this use the emitFreshPointer functionality instead of always doing a clone?

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.

No good reason. I will replace with ConvertFreshPointer

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.

I took a closer look and it's not trivial to replace with ConvertFreshPointer. This path uses the pending_deref_ mechanism. pending_deref_ is generally used for rewriting mutable method receivers to use Ptr::with_mut. Or more generally, to convert pointer dereferences in AddrOf/LValue contexts.

pending_deref_ is triggered inside ConvertLValue, line 1796 above, so every user of ConvertLValue must also check pending_deref_.

@nunoplopes nunoplopes merged commit 7791f22 into Cpp2Rust:master May 20, 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