-
Notifications
You must be signed in to change notification settings - Fork 1.9k
C# 14: User defined compound assignment operators. #21372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
michaelnebel
wants to merge
14
commits into
github:main
Choose a base branch
from
michaelnebel:csharp14/usercompoundassignment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2383a5a
C#: Use a dictionary for translating operator methods to operator sym…
michaelnebel 9f67a33
C#: Support user defined compound assignment operators.
michaelnebel 5fdd980
C#: Add new name for multiplication used in conjunction with compound…
michaelnebel 0ab7e31
C#: Re-factor TargetSymbol into an extension method.
michaelnebel a55841e
C#: Extract calls to user defined compound assignments as operator ca…
michaelnebel 770bfce
C#: Add QL implementation for the new operators.
michaelnebel 2c6064f
C#: Add compound assignment operator call class.
michaelnebel 7651aa7
C#: Add QL compound assignment operator tests and updated expected te…
michaelnebel f08c98a
C#: Add compound assignment operator data flow example.
michaelnebel ea04efc
C#: Add dispatch logic for compound assignment operator calls.
michaelnebel c739bb6
C#: Update test expected file for data flow.
michaelnebel e237344
C#: Take compound assignment operator argument shift into account for…
michaelnebel fabb502
C#: Add extension data flow example for user defined compound assignm…
michaelnebel fafe360
C#: Add change note.
michaelnebel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/CompoundAssignment.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace Semmle.Extraction.CSharp.Entities.Expressions | ||
| { | ||
| internal static class CompoundAssignment | ||
| { | ||
| public static Expression Create(ExpressionNodeInfo info) | ||
| { | ||
| if (info.SymbolInfo.Symbol is IMethodSymbol op && | ||
| op.MethodKind == MethodKind.UserDefinedOperator && | ||
| !op.IsStatic) | ||
| { | ||
| // This is a user-defined instance operator such as `a += b` where `a` is of a type that defines an `operator +=`. | ||
| // In this case, we want to extract the operator call rather than desugar it into `a = a + b`. | ||
| return UserCompoundAssignmentInvocation.Create(info); | ||
| } | ||
| return Assignment.Create(info); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...tractor/Semmle.Extraction.CSharp/Entities/Expressions/UserCompoundAssignmentInvocation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using System.IO; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Semmle.Extraction.Kinds; | ||
|
|
||
| namespace Semmle.Extraction.CSharp.Entities.Expressions | ||
| { | ||
| /// <summary> | ||
| /// Represents a user-defined compound assignment operator such as `a += b` where `a` is of a type that defines an `operator +=`. | ||
| /// In this case, we don't want to desugar it into `a = a + b`, but instead extract the operator call directly as it should | ||
| /// be considered an instance method call on `a` with `b` as an argument. | ||
| /// </summary> | ||
| internal class UserCompoundAssignmentInvocation : Expression<AssignmentExpressionSyntax> | ||
| { | ||
| private readonly ExpressionNodeInfo info; | ||
|
|
||
| protected UserCompoundAssignmentInvocation(ExpressionNodeInfo info) | ||
| : base(info.SetKind(ExprKind.OPERATOR_INVOCATION)) | ||
| { | ||
| this.info = info; | ||
| } | ||
|
|
||
| public static Expression Create(ExpressionNodeInfo info) => new UserCompoundAssignmentInvocation(info).TryPopulate(); | ||
|
|
||
| protected override void PopulateExpression(TextWriter trapFile) | ||
| { | ||
| Create(Context, Syntax.Left, this, -1); | ||
| Create(Context, Syntax.Right, this, 0); | ||
|
|
||
| var target = info.GetTargetSymbol(Context); | ||
| if (target is null) | ||
| { | ||
| Context.ModelError(Syntax, "Unable to resolve target method for user-defined compound assignment operator"); | ||
| return; | ||
| } | ||
|
|
||
| var targetKey = Method.Create(Context, target); | ||
| trapFile.expr_call(this, targetKey); | ||
| } | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
csharp/ql/lib/change-notes/2026-03-06-compound-assignment-operations.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * C# 14: Added support for user-defined compound assignment operators. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Can be a
ReadOnlyDictionary.