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: readonly backing field #74922

Merged
merged 3 commits into from
Aug 29, 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 @@ -172,15 +172,34 @@ protected SourcePropertySymbolBase(
{
Debug.Assert(!IsIndexer);
string fieldName = GeneratedNames.MakeBackingFieldName(_name);
BackingField = new SynthesizedBackingFieldSymbol(this,
fieldName,
// Synthesized backing field for 'field' should not be marked 'initonly'
// since the field might be modified in the get accessor.
// PROTOTYPE: Should the backing field be 'initonly' when the containing
// type, property, or accessor is declared 'readonly'?
isReadOnly: !usesFieldKeyword && ((hasGetAccessor && !hasSetAccessor) || isInitOnly),
this.IsStatic,
hasInitializer);

// The backing field is readonly if any of the following holds:
// - The containing type is declared readonly and the property is an instance property.
bool isReadOnly;
if (!IsStatic && containingType.IsReadOnly)
{
isReadOnly = true;
}
// - The property is declared readonly.
else if (HasReadOnlyModifier)
{
isReadOnly = true;
}
// - The property has no set accessor (but may have an init accessor) and
// the get accessor, if any, is automatically implemented.
else if ((!hasSetAccessor || isInitOnly) && (!hasGetAccessor || hasAutoPropertyGet))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the field also be readonly if all manually implemented get and set accessors are declared readonly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight leaning towards "no", but not a strong opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not valid to declare both accessors as readonly, you'd have to declare the containing property as readonly instead, which would make the field readonly per these rules.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declaring both as readonly is an error, but we may want to treat the backing field as readonly in the following case:

struct S
{
    object P { get; readonly set { } }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more compelling example is:

struct S
{
    object P { readonly get => field; init; }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I can't think of a case where things are made worse by the backing field not being readonly, in cases involving the accessors being marked readonly but not the containing type or property.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a PROTOTYPE comment and a test for now.

{
isReadOnly = true;
}
else
{
// PROTOTYPE: We could treat the field as readonly if all manually implemented get and set
// accessors are declared readonly. Although, to do so, we might need to bind the accessor
// declarations before creating the backing field. See FieldKeywordTests.ReadOnly_05().
isReadOnly = false;
}

BackingField = new SynthesizedBackingFieldSymbol(this, fieldName, isReadOnly: isReadOnly, this.IsStatic, hasInitializer);
}

if (hasGetAccessor)
Expand Down
Loading