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

Handle various cases for field-backed properties #74641

Merged
merged 19 commits into from
Aug 12, 2024

Conversation

cston
Copy link
Member

@cston cston commented Aug 2, 2024

Changes:

  • Generate implicit accessor bodies
  • Support [field:] attributes targeting synthesized backing field
  • Diagnostics for properties with ref-like type

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 2, 2024
@cston cston changed the title Support attributes targeting synthesized backing field Handle various cases for field-backed properties Aug 3, 2024
@cston cston marked this pull request as ready for review August 4, 2024 23:59
@cston cston requested a review from a team as a code owner August 4, 2024 23:59
string source = $$"""
{{typeKind}} A
{
public static object P1 { get; set { _ = field; } }
Copy link
Member Author

@cston cston Aug 5, 2024

Choose a reason for hiding this comment

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

{ get; set { _ = field; } }

Allowing mix of auto- and explicitly-implemented accessors should be tied to language version. #Resolved

@RikkiGibson RikkiGibson self-assigned this Aug 5, 2024
{
static object P1 { get; set { _ = field; } }
static object P2 { get { return field; } set; }
object Q1 { get; set { _ = field; } }
Copy link
Member Author

Choose a reason for hiding this comment

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

{ get; set { _ = field; } }

Should this be reported as an error, with ErrorCode.ERR_InterfacesCantContainFields perhaps?

Copy link
Contributor

Choose a reason for hiding this comment

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

Will review the tests in depth later, but it feels reasonable for the synthesized field's "location" to be the first usage of the field keyword, and to report the exact error you are saying there.

Copy link
Contributor

@RikkiGibson RikkiGibson left a comment

Choose a reason for hiding this comment

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

Had a few comments, and it looks like some assertions are failing in tests

}
}
""";
var verifier = CompileAndVerify(source, verify: Verification.Skipped, targetFramework: TargetFramework.Net80);
Copy link
Contributor

@RikkiGibson RikkiGibson Aug 6, 2024

Choose a reason for hiding this comment

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

Perhaps should also verify execution of the accessors. Not necessarily in this same test.

Should also verify that the manually-implemented accessors behave the way we expect. (and, for example, the impl is not being unexpectedly replaced with an auto-impl.) #Resolved

usesFieldKeyword = body is { } && containsFieldKeyword(body);
Debug.Assert(accessorsHaveImplementation); // it's not clear how this even parsed as a property if it has no accessor list and no arrow expression.
Debug.Assert(hasGetAccessorImplementation); // it's not clear how this even parsed as a property if it has no accessor list and no arrow expression.
Copy link
Member

Choose a reason for hiding this comment

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

Since you're modifying this line anyway, consider making this comment an actual assert message.

@RikkiGibson RikkiGibson self-requested a review August 12, 2024 17:50
(hasGetAccessor && !hasSetAccessor) ? MessageID.IDS_FeatureReadonlyAutoImplementedProperties : MessageID.IDS_FeatureAutoImplementedProperties,
hasGetAccessor && hasSetAccessor ?
(hasAutoPropertyGet && hasAutoPropertySet ? MessageID.IDS_FeatureAutoImplementedProperties : MessageID.IDS_FeatureFieldKeyword) :
MessageID.IDS_FeatureReadonlyAutoImplementedProperties,
Copy link
Contributor

@RikkiGibson RikkiGibson Aug 12, 2024

Choose a reason for hiding this comment

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

nit: It seems like the new factoring is going to diagnose a setter-only auto property (error case) as requiring the readonly auto-properties feature. #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, thanks.

@@ -140,7 +142,9 @@ internal static SourcePropertySymbol Create(SourceMemberContainerTypeSymbol cont
{
Binder.CheckFeatureAvailability(
syntax,
(hasGetAccessor && !hasSetAccessor) ? MessageID.IDS_FeatureReadonlyAutoImplementedProperties : MessageID.IDS_FeatureAutoImplementedProperties,
hasGetAccessor && hasSetAccessor ?
Copy link
Contributor

@RikkiGibson RikkiGibson Aug 12, 2024

Choose a reason for hiding this comment

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

nit: I think it would be easier to grasp what scenario we are in, in each branch, if the above IsAutoProperty were replaced with hasAutoPropertyGet || hasAutoPropertySet. #Resolved

public int P3 { get; set { } }
public int P4 { get { return -4; } set; }
public int P5 { get; init { } }
public int P6 { get { return -6; } init; }
Copy link
Contributor

@RikkiGibson RikkiGibson Aug 12, 2024

Choose a reason for hiding this comment

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

for the future, I would def consider a "half-auto" prop, where the manually implemented accessor never reads field, to possibly be a warning scenario.

For example, if you have get => notField; set; or get; set => notField = value, it practically means you want to ignore the value that was set. There are better ways to do that than introducing a backing field. It very likely means you are making a mistake.

Quite possibly this was already considered, apologies for reiterating these points if so.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is an open question in the proposal.

Copy link
Contributor

Choose a reason for hiding this comment

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

To obtain these warnings, I added https://github.com/dotnet/csharplang/blob/main/proposals/field-keyword.md#field-usage-warnings since the proposal hadn't spoken to this aspect at all yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers New Feature - Field Keyword untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants