Skip to content

Improve C++ compound-assignment operator and C compound literal support#824

Merged
tannergooding merged 3 commits into
dotnet:mainfrom
tannergooding:tannergooding-bookish-bassoon
Jul 18, 2026
Merged

Improve C++ compound-assignment operator and C compound literal support#824
tannergooding merged 3 commits into
dotnet:mainfrom
tannergooding:tannergooding-bookish-bassoon

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Maps C++ compound-assignment operators to their .NET metadata names so they project into C# instead of being skipped. operator+= becomes op_AdditionAssignment, operator<<= becomes op_LeftShiftAssignment, and so on -- consistent with how operator= is already emitted as op_Assign.

Fixes #821


When generating latest code (.NET 10 / C# 14 currently), a compound-assignment operator is instead projected onto the new C# 14 user-defined compound-assignment operator feature. This is only safe for an instance member that returns a pointer/reference to its declaring type and whose body just returns this/*this, so the transform is gated on exactly that: the return type is dropped to void, the trailing return is elided, and the result is public void operator +=(MyStruct rhs) { ... }.

An operator that returns something other than this (e.g. a parameter) does not qualify and continues to emit under its op_*Assignment metadata name -- covered by a regression case in CompoundAssignmentOperatorTest.


Emits C compound literals ((MyStruct){ .x = 1 }) as C# object initializers rather than dropping them.

Fixes #819

tannergooding and others added 3 commits July 18, 2026 12:35
Projects operator+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= to op_AdditionAssignment and friends, consistent with the existing operator= to op_Assign mapping.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
A C compound literal (T){ ... } has no distinct C# spelling, so it projects as the object initializer emitted by its inner initializer list.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…argeting latest

When generating latest code, a C++ compound-assignment operator that returns a pointer/reference to its declaring type and just returns \	his\/\*this\ is projected onto a C# 14 user-defined \operator +=\ with a void return, dropping the trailing return, rather than the \op_*Assignment\ metadata-name projection.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding merged commit ff5a3ce into dotnet:main Jul 18, 2026
12 checks passed
@tannergooding
tannergooding deleted the tannergooding-bookish-bassoon branch July 18, 2026 20:16
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.

operator *= in C++ header generates invalid C# Unsupported statement: 'CompoundLiteralExpr'

1 participant