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

[Nullable Context] Proposal for better null-state static analysis with IPostConfigureOptions #6950

Conversation

schmittjoseph
Copy link
Member

@schmittjoseph schmittjoseph commented Jul 3, 2024

Summary

This PR is a proposal for how we can have better null-state static analysis when dealing with our Options classes the rely on IPostConfigureOptions to set default values.

(Also partially addresses #6929)

Release Notes Entry

// Guaranteed to not be null by StoragePostConfigureOptions.PostConfigure.
string dumpTempFolder = _storageOptions.CurrentValue.DumpTempFolder!;
StorageOptions options = _storageOptions.CurrentValue;
OptionsUtils.ThrowIfNotConfigured<StorageOptions>(options.Configured);
Copy link
Member Author

@schmittjoseph schmittjoseph Jul 3, 2024

Choose a reason for hiding this comment

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

This properly informs the compiler's null-state static analysis because:

  • Options.ThrowIfNotConfigured statically tell the compiler that if options.Configured is false it will never return.
  • StorageOptions.Configured's getter method statically tells the compiler that if it is true then certain members (e.g. DumpTempFolder) will not be null.

Ref: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis

@schmittjoseph schmittjoseph changed the title [Nullable Context] Proposal for better null-aware context with IPostConfigureOptions [Nullable Context] Proposal for better null-state static analysis with IPostConfigureOptions Jul 3, 2024

internal bool Configured
{
[MemberNotNullWhen(true, nameof(DumpTempFolder))]
Copy link
Member Author

@schmittjoseph schmittjoseph Jul 3, 2024

Choose a reason for hiding this comment

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

note: This supports specifying multiple members, e.g.
[MemberNotNullWhen(true, nameof(DumpTempFolder), nameof(SharedLibraryPath))]

@schmittjoseph schmittjoseph marked this pull request as draft July 17, 2024 17:59
@dotnet-policy-service dotnet-policy-service bot added the no-recent-activity Automatically added by bot after four weeks of inactivity label Aug 15, 2024
Copy link
Contributor

The 'no-recent-activity' label has been added to this pull request due to four weeks without any activity. If there is no activity in the next six weeks, this pull request will automatically be closed. You can learn more about our stale PR policy here: https://github.com/dotnet/dotnet-monitor/blob/main/CONTRIBUTING.md#stale-pr-policy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-review no-recent-activity Automatically added by bot after four weeks of inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant