Skip to content

Commit 5391a78

Browse files
[Version 9.0] Feature support for nullable attributes carried over from V8 (#1221)
* 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. * fix link * add missing type names --------- Co-authored-by: Rex Jaeschke <rex@RexJaeschke.com>
1 parent 131d9d0 commit 5391a78

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

standard/attributes.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,8 @@ The code-analysis attributes are declared in namespace `System.Diagnostics.CodeA
853853
`MaybeNullWhen` ([§23.5.7.7](attributes.md#23577-the-maybenullwhen-attribute)) | A non-nullable argument may be null when the method returns the specified `bool` value.
854854
`NotNullWhen` ([§23.5.7.10](attributes.md#235710-the-notnullwhen-attribute)) | A nullable argument won’t be null when the method returns the specified `bool` value.
855855
`NotNullIfNotNull` ([§23.5.7.9](attributes.md#23579-the-notnullifnotnull-attribute)) | A return value isn’t null if the argument for the specified parameter isn’t null.
856+
`MemberNotNull` (§membernotnull-attribute) | The listed member won’t be null when the method returns.
857+
`MemberNotNullWhen` (§membernotnullwhen-attribute) | The listed member won’t be null when the method returns the specified `bool` value.
856858
`DoesNotReturn` ([§23.5.7.4](attributes.md#23574-the-doesnotreturn-attribute)) | This method never returns.
857859
`DoesNotReturnIf` ([§23.5.7.5](attributes.md#23575-the-doesnotreturnif-attribute)) | This method never returns if the associated `bool` parameter has the specified value.
858860
@@ -999,6 +1001,47 @@ Specifies that a non-nullable return value may be null.
9991001
10001002
Specifies that a non-nullable argument may be `null` when the method returns the specified `bool` value. This is similar to the `MaybeNull` attribute ([§23.5.7.6](attributes.md#23576-the-maybenull-attribute)), but includes a parameter for the specified return value.
10011003
1004+
#### §membernotnull-attribute The MemberNotNull attribute
1005+
1006+
Specifies that the given member wont be `null` when the method returns.
1007+
1008+
> *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:
1009+
>
1010+
> <!-- Example: {template:"standalone-lib", name:"MemberNotNullAttribute"} -->
1011+
> ```csharp
1012+
> #nullable enable
1013+
> public class Container
1014+
> {
1015+
> private string _uniqueIdentifier; // must be initialized.
1016+
> private string? _optionalMessage;
1017+
>
1018+
> public Container()
1019+
> {
1020+
> Helper();
1021+
> }
1022+
>
1023+
> public Container(string message)
1024+
> {
1025+
> Helper();
1026+
> _optionalMessage = message;
1027+
> }
1028+
>
1029+
> [MemberNotNull(nameof(_uniqueIdentifier))]
1030+
> private void Helper()
1031+
> {
1032+
> _uniqueIdentifier = DateTime.Now.Ticks.ToString();
1033+
> }
1034+
> }
1035+
> ```
1036+
>
1037+
> Multiple field names may be given as arguments to the attributes constructor. *end example*
1038+
1039+
#### §membernotnullwhen-attribute The MemberNotNullWhen attribute
1040+
1041+
Specifies that the listed member wont be `null` when the method returns the specified `bool` value.
1042+
1043+
> *Example*: This attribute is like `MemberNotNull` (§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*
1044+
10021045
#### 23.5.7.8 The NotNull attribute
10031046
10041047
Specifies that a nullable value will never be `null` if the method returns (rather than throwing).

standard/standard-library.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,22 @@ namespace System.Diagnostics.CodeAnalysis
735735
public MaybeNullWhenAttribute(bool returnValue);
736736
}
737737

738+
[System.AttributeUsage(System.AttributeTargets.Method |
739+
System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
740+
public sealed class MemberNotNullAttribute : Attribute
741+
{
742+
public MemberNotNullAttribute(string member) {}
743+
public MemberNotNullAttribute(params string[] members) {}
744+
}
745+
746+
[System.AttributeUsage(System.AttributeTargets.Method |
747+
System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
748+
public sealed class MemberNotNullWhenAttribute : Attribute
749+
{
750+
public MemberNotNullWhenAttribute(bool returnValue, string member) {}
751+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members) {}
752+
}
753+
738754
[System.AttributeUsage(System.AttributeTargets.Field |
739755
System.AttributeTargets.Parameter | System.AttributeTargets.Property |
740756
System.AttributeTargets.ReturnValue, Inherited=false)]
@@ -1356,6 +1372,8 @@ The following library types are referenced in this specification. The full names
13561372
- `global::System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute`
13571373
- `global::System.Diagnostics.CodeAnalysis.MaybeNullAttribute`
13581374
- `global::System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute`
1375+
- `global::System.Diagnostics.CodeAnalysis.MemberNotNullAttribute`
1376+
- `global::System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute`
13591377
- `global::System.Diagnostics.CodeAnalysis.NotNullAttribute`
13601378
- `global::System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute`
13611379
- `global::System.Diagnostics.CodeAnalysis.NotNullWhenAttribute`

0 commit comments

Comments
 (0)