-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Field-backed properties: update uses of IsAutoProperty #74798
Changes from all commits
5188aac
793a04b
9630eb5
571a392
e083fc0
2adf311
889c4e4
4501eb8
8fdd1e3
2c5fa50
5172975
f8fa5d4
2aa8e19
3724d79
8704d0c
cf61a16
cb56137
ad68a0f
7720d0d
5cda547
d03acb2
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 |
---|---|---|
|
@@ -302,7 +302,7 @@ protected void CheckInitializerIfNeeded(BindingDiagnosticBag diagnostics) | |
{ | ||
diagnostics.Add(ErrorCode.ERR_InstancePropertyInitializerInInterface, Location); | ||
} | ||
else if (!IsAutoProperty) | ||
else if (!IsAutoPropertyOrUsesFieldKeyword) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_InitializerOnNonAutoProperty, Location); | ||
} | ||
|
@@ -653,7 +653,7 @@ public bool HasSkipLocalsInitAttribute | |
internal bool IsAutoPropertyOrUsesFieldKeyword | ||
=> IsAutoProperty || UsesFieldKeyword; | ||
|
||
protected bool UsesFieldKeyword | ||
private bool UsesFieldKeyword | ||
=> (_propertyFlags & Flags.UsesFieldKeyword) != 0; | ||
|
||
protected bool HasExplicitAccessModifier | ||
|
@@ -666,8 +666,9 @@ protected bool AccessorsHaveImplementation | |
=> (_propertyFlags & Flags.AccessorsHaveImplementation) != 0; | ||
|
||
/// <summary> | ||
/// Backing field for automatically implemented property, or | ||
/// for a property with an initializer. | ||
/// Backing field for an automatically implemented property, or | ||
/// a property with an accessor using the 'field' keyword, or | ||
/// a property with an initializer. | ||
/// </summary> | ||
internal SynthesizedBackingFieldSymbol BackingField { get; } | ||
|
||
|
@@ -715,9 +716,9 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions, | |
diagnostics.Add(ErrorCode.ERR_RefReturningPropertiesCannotBeRequired, Location); | ||
} | ||
|
||
if (IsAutoProperty) | ||
if (IsAutoPropertyOrUsesFieldKeyword) | ||
{ | ||
if (!IsStatic && SetMethod is { IsInitOnly: false }) | ||
if (!IsStatic && ((_propertyFlags & Flags.HasAutoPropertySet) != 0) && SetMethod is { IsInitOnly: false }) | ||
{ | ||
if (ContainingType.IsReadOnly) | ||
{ | ||
|
@@ -738,10 +739,15 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions, | |
diagnostics.Add(ErrorCode.ERR_AutoPropertyCannotBeRefReturning, Location); | ||
} | ||
|
||
// get-only auto property should not override settable properties | ||
if (this.IsOverride && SetMethod is null && !this.IsReadOnly) | ||
// Auto property should override both accessors. | ||
if (this.IsOverride) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_AutoPropertyMustOverrideSet, Location); | ||
var overriddenProperty = (PropertySymbol)this.GetLeastOverriddenMember(this.ContainingType); | ||
if ((overriddenProperty.GetMethod is { } && GetMethod is null) || | ||
(overriddenProperty.SetMethod is { } && SetMethod is null)) | ||
Comment on lines
+746
to
+747
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. Correct me if I'm wrong, but this appears to be a fundamentally different check than was here earlier. What was reporting the error on a missing 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. Previously, for a set-only auto-property that overrides a virtual property with a get, the compiler would report:
And the compiler would not report:
See sharplab.io. |
||
{ | ||
diagnostics.Add(ErrorCode.ERR_AutoPropertyMustOverrideSet, Location); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -791,6 +797,9 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions, | |
} | ||
else if (!hasGetAccessor && IsAutoProperty) | ||
{ | ||
// The only forms of auto-property that are disallowed are { set; } and { init; }. | ||
// Other forms of auto- or manually-implemented accessors are allowed | ||
// including equivalent field cases such as { set { field = value; } }. | ||
diagnostics.Add(ErrorCode.ERR_AutoPropertyMustHaveGetAccessor, _setMethod!.GetFirstLocation()); | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I'm not sure why we'd condition this on having
set;
, as opposed to any setter.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.
Added an open question for language design: dotnet/csharplang#8389