-
Notifications
You must be signed in to change notification settings - Fork 1.9k
C# 14: Support the field keyword.
#21309
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
5
commits into
github:main
Choose a base branch
from
michaelnebel:csharp14/field
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.
+432
−2
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
edb2ed8
C#: Extract the implicit property backing field when referenced via t…
michaelnebel 113f3e8
C#: Add property test case where the field keyword is used.
michaelnebel 97c0267
C#: Add data flow test for properties using the field keyword.
michaelnebel d93f485
C#: Add change-note.
michaelnebel c3a1eb1
C#: Extract field modifiers and tag the field as being compiler gener…
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
28 changes: 28 additions & 0 deletions
28
csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/PropertyFieldAccess.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,28 @@ | ||
| using System.IO; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Semmle.Extraction.Kinds; | ||
|
|
||
| namespace Semmle.Extraction.CSharp.Entities.Expressions | ||
| { | ||
| internal class PropertyFieldAccess : Expression<FieldExpressionSyntax> | ||
| { | ||
| private PropertyFieldAccess(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.FIELD_ACCESS)) { } | ||
|
|
||
| public static Expression Create(ExpressionNodeInfo info) => new PropertyFieldAccess(info).TryPopulate(); | ||
|
|
||
| protected override void PopulateExpression(TextWriter trapFile) | ||
| { | ||
| var symbolInfo = Context.GetSymbolInfo(Syntax); | ||
| if (symbolInfo.Symbol is IFieldSymbol field) | ||
| { | ||
| var target = PropertyField.Create(Context, field); | ||
| trapFile.expr_access(this, target); | ||
| if (!field.IsStatic) | ||
| { | ||
| This.CreateImplicit(Context, field.ContainingType, Location, this, -1); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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
53 changes: 53 additions & 0 deletions
53
csharp/extractor/Semmle.Extraction.CSharp/Entities/PropertyField.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,53 @@ | ||
| using System.IO; | ||
| using Microsoft.CodeAnalysis; | ||
| using Semmle.Extraction.CSharp.Util; | ||
| using Semmle.Extraction.Kinds; | ||
|
|
||
| namespace Semmle.Extraction.CSharp.Entities | ||
| { | ||
| /// <summary> | ||
| /// Represents the autogenerated backing field `field` for a property. | ||
| /// It is only created for properties that use the `field` keyword in their getter or setter, and | ||
| /// is not created for auto-properties. | ||
| /// </summary> | ||
| internal class PropertyField : Field | ||
| { | ||
| protected PropertyField(Context cx, IFieldSymbol init) | ||
| : base(cx, init) | ||
| { | ||
| } | ||
|
|
||
| public static new PropertyField Create(Context cx, IFieldSymbol field) => PropertyFieldFactory.Instance.CreateEntity(cx, (field, field.AssociatedSymbol), field); | ||
|
|
||
| public override bool NeedsPopulation => true; | ||
|
|
||
| public override void Populate(TextWriter trapFile) | ||
| { | ||
| PopulateNullability(trapFile, Symbol.GetAnnotatedType()); | ||
|
|
||
| var unboundFieldKey = PropertyField.Create(Context, Symbol.OriginalDefinition); | ||
| var name = Symbol.AssociatedSymbol is not null ? $"{Symbol.AssociatedSymbol.GetName()}.field" : Symbol.Name; | ||
| trapFile.fields(this, VariableKind.None, name, ContainingType!, Type.TypeRef, unboundFieldKey); | ||
| trapFile.compiler_generated(this); | ||
|
|
||
| PopulateModifiers(trapFile); | ||
|
|
||
| if (Context.OnlyScaffold) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (Context.ExtractLocation(Symbol)) | ||
| { | ||
| WriteLocationsToTrap(trapFile.field_location, this, Locations); | ||
| } | ||
| } | ||
|
|
||
| private class PropertyFieldFactory : CachedEntityFactory<IFieldSymbol, PropertyField> | ||
| { | ||
| public static PropertyFieldFactory Instance { get; } = new PropertyFieldFactory(); | ||
|
|
||
| public override PropertyField Create(Context cx, IFieldSymbol init) => new PropertyField(cx, init); | ||
| } | ||
| } | ||
| } | ||
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 the `field` keyword in properties. |
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
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.
PropertyField.Populateonly emits thefieldsrow (plus nullability/location) but does not extract attributes/modifiers/ref-kind likeField.Populatedoes. As a result the synthetic backing field will be missinghas_modifiers(e.g. private/static/readonly/required) and any[field: ...]attributes that Roslyn attaches to the backing field symbol. Consider reusing the same extraction steps asField.Populate(at leastPopulateAttributes()+PopulateModifiers(trapFile)andPopulateRefKind).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.
Uh, it is a really good point that we should extract the modifiers. For static properties, the generated backing field is also static (which might be relevant - otherwise it looks like a static property accesses a non-static field).
However,
ref,in,outetc, so there is no need to attempt to extract such information.