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

CS8618 is incorrectly reported for a constructor with SetsRequiredMembersAttribute and a required non-auto property #77013

Closed
vgriph opened this issue Feb 4, 2025 · 3 comments
Labels
Area-Compilers Feature - Required Members Required properties and fields Resolution-By Design The behavior reported in the issue matches the current design

Comments

@vgriph
Copy link

vgriph commented Feb 4, 2025

Version Used:
Dotnet SDK 9.0.101
csc.dll 4.1200.24.57006
C# 13

Steps to Reproduce:

  1. Create a .NET 9 C# project
  2. Add the following class:
  public class Class
  {
      private string @field = string.Empty;

      [SetsRequiredMembers]
      public Class()
      {
      }

      public required string Test
      {
          get => @field;
          set => @field = value;
      }
  }
  1. Compile

Also provided as a gist: https://sharplab.io/#gist:c97d05bc1df387299a3a36043bfcd68f

Diagnostic Id:
CS8618: Non-nullable property 'Test' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Expected Behavior:

The code should compile without warning

Actual Behavior:
The code gets a warning suggesting to add the required modifier, when it is already there

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 4, 2025
@RikkiGibson
Copy link
Contributor

RikkiGibson commented Feb 4, 2025

This is the intended guardrail for [SetsRequiredMembers]. The constructor receiving the warning has an obligation to set that property, which it is not honoring. We don't do a full-on assignment analysis to see if you set the property, but, if it's non-nullable, it had better be put into the non-nullable state.

@333fred in case he wishes to add further information.

@vgriph Thanks for reporting this issue. Feel free to follow up if there are any further questions or concerns.

@RikkiGibson RikkiGibson added Question Resolution-Answered The question has been answered Feature - Required Members Required properties and fields Resolution-By Design The behavior reported in the issue matches the current design Needs API Review Needs to be reviewed by the API review council and removed Question Resolution-Answered The question has been answered Needs API Review Needs to be reviewed by the API review council labels Feb 4, 2025
@RikkiGibson RikkiGibson closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged Issues and PRs which have not yet been triaged by a lead label Feb 4, 2025
@vgriph
Copy link
Author

vgriph commented Feb 4, 2025

I still think the warning message is wrong. It states "Consider adding the 'required' modifier or declaring the property as nullable." But it already has the required modifier.

The example provided is simplified from my actual usage. Where the setter does some calculations to update the backing field. I want the property to be required for other constructors, but in a deserialization constructor I want to avoid having to do the calculations and instead just set the backing field directly.

@RikkiGibson
Copy link
Contributor

We could consider introducing a separate diagnostic message when the property is already marked 'required', which doesn't suggest to add that modifier. If you feel strongly enough about it, I would review a PR which adds that message.

in a deserialization constructor I want to avoid having to do the calculations and instead just set the backing field directly.

I think that if that constructor does not assign the property anywhere, it makes the most sense to wrap the constructor signature in #pragma warning disable/restore CS8618. This expresses that the requirement is being handled in some way the compiler is unable to track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Required Members Required properties and fields Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

2 participants