Skip to content
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

Merged
merged 21 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ private bool CheckPropertyValueKind(SyntaxNode node, BoundExpression expr, BindV
if (setMethod is null)
{
var containing = this.ContainingMemberOrLambda;
if (!AccessingAutoPropertyFromConstructor(receiver, propertySymbol, containing)
if (!AccessingAutoPropertyFromConstructor(receiver, propertySymbol, containing, allowFieldKeyword: true)
&& !isAllowedDespiteReadonly(receiver))
{
Error(diagnostics, ErrorCode.ERR_AssgReadonlyProp, node, propertySymbol);
Expand Down
5 changes: 3 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,8 @@ internal static bool AccessingAutoPropertyFromConstructor(BoundPropertyAccess pr
return AccessingAutoPropertyFromConstructor(propertyAccess.ReceiverOpt, propertyAccess.PropertySymbol, fromMember);
}

private static bool AccessingAutoPropertyFromConstructor(BoundExpression receiver, PropertySymbol propertySymbol, Symbol fromMember)
// PROTOTYPE: Review all callers for allowFieldKeyword.
private static bool AccessingAutoPropertyFromConstructor(BoundExpression receiver, PropertySymbol propertySymbol, Symbol fromMember, bool allowFieldKeyword = false)
{
if (!propertySymbol.IsDefinition && propertySymbol.ContainingType.Equals(propertySymbol.ContainingType.OriginalDefinition, TypeCompareKind.IgnoreNullableModifiersForReferenceTypes))
{
Expand All @@ -1765,7 +1766,7 @@ private static bool AccessingAutoPropertyFromConstructor(BoundExpression receive
var propertyIsStatic = propertySymbol.IsStatic;

return (object)sourceProperty != null &&
sourceProperty.IsAutoProperty &&
(allowFieldKeyword ? sourceProperty.IsAutoPropertyOrUsesFieldKeyword : sourceProperty.IsAutoProperty) &&
TypeSymbol.Equals(sourceProperty.ContainingType, fromMember.ContainingType, TypeCompareKind.AllIgnoreOptions) &&
IsConstructorOrField(fromMember, isStatic: propertyIsStatic) &&
(propertyIsStatic || receiver.Kind == BoundKind.ThisReference);
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4891,7 +4891,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>Expected identifier or numeric literal</value>
</data>
<data name="ERR_InitializerOnNonAutoProperty" xml:space="preserve">
<value>Only auto-implemented properties can have initializers.</value>
<value>Only auto-implemented properties, or properties that use the 'field' keyword, can have initializers.</value>
</data>
<data name="ERR_InstancePropertyInitializerInInterface" xml:space="preserve">
<value>Instance properties in interfaces cannot have initializers.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,9 @@ private BoundExpression MakePropertyAssignment(
if (setMethod is null)
{
var autoProp = (SourcePropertySymbolBase)property.OriginalDefinition;
Debug.Assert(autoProp.IsAutoProperty,
Debug.Assert(autoProp.IsAutoPropertyOrUsesFieldKeyword,
"only autoproperties can be assignable without having setters");
Debug.Assert(_factory.CurrentFunction.IsConstructor());
Debug.Assert(property.Equals(autoProp, TypeCompareKind.IgnoreNullableModifiersForReferenceTypes));

var backingField = autoProp.BackingField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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; }

Expand Down Expand Up @@ -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 })
Copy link
Member

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.

Copy link
Member Author

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

{
if (ContainingType.IsReadOnly)
{
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 get override earlier?

Copy link
Member Author

@cston cston Aug 23, 2024

Choose a reason for hiding this comment

The 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:

CS8051: Auto-implemented properties must have get accessors.

And the compiler would not report:

CS8080: Auto-implemented properties must override all accessors of the overridden property.

See sharplab.io.

{
diagnostics.Add(ErrorCode.ERR_AutoPropertyMustOverrideSet, Location);
}
}
}

Expand Down Expand Up @@ -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());
}

Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

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.

Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ public void RefAssignStaticProperty()
class Program
{
static int field = 0;
static ref int P { get { return ref field; } }
static ref int P { get { return ref @field; } }

static void M()
{
Expand Down Expand Up @@ -1789,7 +1789,7 @@ public void RefAssignClassInstanceProperty()
class Program
{
int field = 0;
ref int P { get { return ref field; } }
ref int P { get { return ref @field; } }

void M()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ class Program
{
int field = 0;

ref int P { get { return ref field; } }
ref int P { get { return ref @field; } }

ref int this[int i] { get { return ref field; } }

Expand Down Expand Up @@ -1455,7 +1455,7 @@ class Program
{
int field = 0;

ref int P { get { return ref field; } }
ref int P { get { return ref @field; } }

ref int this[int i] { get { return ref field; } }

Expand Down
Loading