-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[semi-auto-props]: Avoid using IsAutoProperty
when possible
#59109
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
Changes from all commits
694bdf4
aa5826d
978ffaf
e98aac1
cf8c473
2848e88
ed988e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,8 @@ | |
using System; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis.CSharp.Emit; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
@@ -319,7 +317,7 @@ internal bool IsExpressionBodied | |
=> (_propertyFlags & Flags.IsExpressionBodied) != 0; | ||
|
||
private void CheckInitializer( | ||
bool isAutoProperty, | ||
bool allowsInitializer, | ||
bool isInterface, | ||
bool isStatic, | ||
Location location, | ||
|
@@ -329,7 +327,7 @@ private void CheckInitializer( | |
{ | ||
diagnostics.Add(ErrorCode.ERR_InstancePropertyInitializerInInterface, location, this); | ||
} | ||
else if (!isAutoProperty) | ||
else if (!allowsInitializer) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_InitializerOnNonAutoProperty, location, this); | ||
} | ||
|
@@ -783,6 +781,36 @@ internal SynthesizedBackingFieldSymbol? FieldKeywordBackingField | |
return null; | ||
} | ||
} | ||
|
||
private bool AllowInitializer | ||
{ | ||
get | ||
{ | ||
// PROTOTYPE(semi-auto-props): Fix implementation for semi auto properties. | ||
return (_setMethod is null && _getMethod?.BodyShouldBeSynthesized == true) || | ||
_setMethod?.BodyShouldBeSynthesized == true; | ||
} | ||
} | ||
|
||
private bool AllowFieldAttributeTarget | ||
{ | ||
get | ||
{ | ||
// PROTOTYPE(semi-auto-props): Fix implementation for semi auto properties. | ||
return _getMethod?.BodyShouldBeSynthesized == true || | ||
_setMethod?.BodyShouldBeSynthesized == true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if using the same implementation as for
For an attribute, we just need to have a field. Requirements for an initializer are more strict. Especially taking https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-01-12.md#conclusion into consideration. #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@AlekseyTs Both of the logics don't seem to take semi auto properties into account, unless I'm missing something. For example, |
||
} | ||
} | ||
|
||
private bool DisallowRefLikeTypes | ||
{ | ||
get | ||
{ | ||
// PROTOTYPE(semi-auto-props): Fix implementation for semi auto properties. | ||
return _getMethod?.BodyShouldBeSynthesized == true || | ||
_setMethod?.BodyShouldBeSynthesized == true; | ||
Youssef1313 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
#nullable disable | ||
|
||
internal override bool MustCallMethodsDirectly | ||
|
@@ -824,7 +852,7 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions, | |
bool hasInitializer = (_propertyFlags & Flags.HasInitializer) != 0; | ||
if (hasInitializer) | ||
{ | ||
CheckInitializer(IsAutoProperty, ContainingType.IsInterface, IsStatic, Location, diagnostics); | ||
CheckInitializer(AllowInitializer, ContainingType.IsInterface, IsStatic, Location, diagnostics); | ||
} | ||
|
||
if (IsAutoPropertyWithGetAccessor) | ||
|
@@ -1196,7 +1224,7 @@ private SynthesizedSealedPropertyAccessor MakeSynthesizedSealedAccessor() | |
AttributeLocation IAttributeTargetSymbol.DefaultAttributeLocation => AttributeLocation.Property; | ||
|
||
AttributeLocation IAttributeTargetSymbol.AllowedAttributeLocations | ||
=> IsAutoPropertyWithGetAccessor // PROTOTYPE(semi-auto-props): Adjust this and add tests. | ||
=> AllowFieldAttributeTarget | ||
? AttributeLocation.Property | AttributeLocation.Field | ||
: AttributeLocation.Property; | ||
|
||
|
@@ -1647,7 +1675,7 @@ protected virtual void ValidatePropertyType(BindingDiagnosticBag diagnostics) | |
{ | ||
diagnostics.Add(ErrorCode.ERR_FieldCantBeRefAny, TypeLocation, type); | ||
} | ||
else if (this.IsAutoPropertyWithGetAccessor && type.IsRefLikeType && (this.IsStatic || !this.ContainingType.IsRefLikeType)) | ||
else if (DisallowRefLikeTypes && type.IsRefLikeType && (this.IsStatic || !this.ContainingType.IsRefLikeType)) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_FieldAutoPropCantBeByRefLike, TypeLocation, type); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.