Skip to content

Commit 098d779

Browse files
committed
Restore nullable attributes added in 9
These two attributes were mistakenly added in #1191. They were removed in #1218 This PR adds them back into the `draft-v9` branch, where they are valid.
1 parent f3c6647 commit 098d779

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

standard/attributes.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,8 @@ The code-analysis attributes are declared in namespace `System.Diagnostics.CodeA
877877
`MaybeNullWhen` ([§22.5.7.7](attributes.md#22577-the-maybenullwhen-attribute)) | A non-nullable argument may be null when the method returns the specified `bool` value.
878878
`NotNullWhen` ([§22.5.7.10](attributes.md#225710-the-notnullwhen-attribute)) | A nullable argument won’t be null when the method returns the specified `bool` value.
879879
`NotNullIfNotNull` ([§22.5.7.9](attributes.md#22579-the-notnullifnotnull-attribute)) | A return value isn’t null if the argument for the specified parameter isn’t null.
880+
`MemberNotNull` (§membernotnull-attribute) | The listed member won’t be null when the method returns.
881+
`MemberNotNullWhen` (§membernotnullwhen-attribute) | The listed member won’t be null when the method returns the specified `bool` value.
880882
`DoesNotReturn` ([§22.5.7.4](attributes.md#22574-the-doesnotreturn-attribute)) | This method never returns.
881883
`DoesNotReturnIf` ([§22.5.7.5](attributes.md#22575-the-doesnotreturnif-attribute)) | This method never returns if the associated `bool` parameter has the specified value.
882884
@@ -1030,6 +1032,47 @@ Specifies that a non-nullable return value may be null.
10301032
10311033
Specifies that a non-nullable argument may be `null` when the method returns the specified `bool` value. This is similar to the `MaybeNull` attribute ([§22.5.7.6](attributes.md#22576-the-maybenull-attribute)), but includes a parameter for the specified return value.
10321034
1035+
#### §membernotnull-attribute The MemberNotNull attribute
1036+
1037+
Specifies that the given member wont be `null` when the method returns.
1038+
1039+
> *Example*: A helper method may include the `MemberNotNull` attribute to list any fields that are assigned to a non-null value in that method. A compiler that analyzes constructors to determine whether all non-nullable reference fields have been initialized may then use this attribute to discover which fields have been set by those helper methods. Consider the following example:
1040+
>
1041+
> <!-- Example: {template:"standalone-lib", name:"MemberNotNullAttribute"} -->
1042+
> ```csharp
1043+
> #nullable enable
1044+
> public class Container
1045+
> {
1046+
> private string _uniqueIdentifier; // must be initialized.
1047+
> private string? _optionalMessage;
1048+
>
1049+
> public Container()
1050+
> {
1051+
> Helper();
1052+
> }
1053+
>
1054+
> public Container(string message)
1055+
> {
1056+
> Helper();
1057+
> _optionalMessage = message;
1058+
> }
1059+
>
1060+
> [MemberNotNull(nameof(_uniqueIdentifier))]
1061+
> private void Helper()
1062+
> {
1063+
> _uniqueIdentifier = DateTime.Now.Ticks.ToString();
1064+
> }
1065+
> }
1066+
> ```
1067+
>
1068+
> Multiple field names may be given as arguments to the attributes constructor. *end example*
1069+
1070+
#### §membernotnullwhen-attribute The MemberNotNullWhen attribute
1071+
1072+
Specifies that the listed member wont be `null` when the method returns the specified `bool` value.
1073+
1074+
> *Example*: This attribute is like `MemberNotNull` ([§22.5.7.8](attributes.md#22578-the-membernotnull-attribute)) except that `MemberNotNullWhen` takes a `bool` argument. `MemberNotNullWhen` is intended for use in situations in which a helper method returns a `bool` indicating whether it initialized fields. *end example*
1075+
10331076
#### 22.5.7.8 The NotNull attribute
10341077
10351078
Specifies that a nullable value will never be `null` if the method returns (rather than throwing).

standard/standard-library.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,22 @@ namespace System.Runtime.CompilerServices
614614
public MaybeNullWhenAttribute(bool returnValue) {}
615615
}
616616

617+
[System.AttributeUsage(System.AttributeTargets.Method |
618+
System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
619+
public sealed class MemberNotNullAttribute : Attribute
620+
{
621+
public MemberNotNullAttribute(string member) {}
622+
public MemberNotNullAttribute(params string[] members) {}
623+
}
624+
625+
[System.AttributeUsage(System.AttributeTargets.Method |
626+
System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
627+
public sealed class MemberNotNullWhenAttribute : Attribute
628+
{
629+
public MemberNotNullWhenAttribute(bool returnValue, string member) {}
630+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members) {}
631+
}
632+
617633
[System.AttributeUsage(System.AttributeTargets.Field |
618634
System.AttributeTargets.Parameter | System.AttributeTargets.Property |
619635
System.AttributeTargets.ReturnValue, Inherited=false)]

0 commit comments

Comments
 (0)