From e9c7558c283b93b0c7390443c60a91263f7ef0a9 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Fri, 13 Mar 2020 10:56:54 -0700 Subject: [PATCH 1/8] Add MemberNotNull/When attributes --- .../CodeAnalysis/NullableAttributes.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs index 0b24c01a76438..07f2d36a6eab5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs @@ -125,4 +125,39 @@ sealed class DoesNotReturnIfAttribute : Attribute /// Gets the condition parameter value. public bool ParameterValue { get; } } + + /// Sepcifies that the method or property will ensure that the listed field and property members have not-null values. + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] +#if INTERNAL_NULLABLE_ATTRIBUTES + internal +#else + public +#endif + sealed class MemberNotNullAttribute : Attribute + { + /// Initializes the attribute with the list of field and property members. + /// + /// The list of field and property members that are promised to be not-null. + /// + public MemberNotNullAttribute(params string[] members) { } + } + + /// Sepcifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] +#if INTERNAL_NULLABLE_ATTRIBUTES + internal +#else + public +#endif + sealed class MemberNotNullWhenAttribute : Attribute + { + /// Initializes the attribute with the specified return value condition and list of field and property members. + /// + /// The return value condition. If the method returns this value, the associated parameter will not be null. + /// + /// + /// The list of field and property members that are promised to be not-null. + /// + public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { } + } } From 0890db6cf7f4cb1142e6cb06d88a8df8e702f775 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Fri, 13 Mar 2020 12:19:25 -0700 Subject: [PATCH 2/8] Apply [CLSCompliant(false)] --- .../src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs index 07f2d36a6eab5..6f91f4c07a2c5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs @@ -128,6 +128,7 @@ sealed class DoesNotReturnIfAttribute : Attribute /// Sepcifies that the method or property will ensure that the listed field and property members have not-null values. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] + [CLSCompliant(false)] #if INTERNAL_NULLABLE_ATTRIBUTES internal #else @@ -144,6 +145,7 @@ public MemberNotNullAttribute(params string[] members) { } /// Sepcifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] + [CLSCompliant(false)] #if INTERNAL_NULLABLE_ATTRIBUTES internal #else From bcad926752e59b0a954f71ebe06b360a15e5dad0 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Fri, 13 Mar 2020 12:32:22 -0700 Subject: [PATCH 3/8] Fix typos and indent --- .../System/Diagnostics/CodeAnalysis/NullableAttributes.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs index 6f91f4c07a2c5..cd14540412e5e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs @@ -126,7 +126,7 @@ sealed class DoesNotReturnIfAttribute : Attribute public bool ParameterValue { get; } } - /// Sepcifies that the method or property will ensure that the listed field and property members have not-null values. + /// Specifies that the method or property will ensure that the listed field and property members have not-null values. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] [CLSCompliant(false)] #if INTERNAL_NULLABLE_ATTRIBUTES @@ -134,7 +134,7 @@ sealed class DoesNotReturnIfAttribute : Attribute #else public #endif - sealed class MemberNotNullAttribute : Attribute + sealed class MemberNotNullAttribute : Attribute { /// Initializes the attribute with the list of field and property members. /// @@ -143,7 +143,7 @@ sealed class MemberNotNullAttribute : Attribute public MemberNotNullAttribute(params string[] members) { } } - /// Sepcifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. + /// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] [CLSCompliant(false)] #if INTERNAL_NULLABLE_ATTRIBUTES @@ -151,7 +151,7 @@ public MemberNotNullAttribute(params string[] members) { } #else public #endif - sealed class MemberNotNullWhenAttribute : Attribute + sealed class MemberNotNullWhenAttribute : Attribute { /// Initializes the attribute with the specified return value condition and list of field and property members. /// From 9c783a21d9f8a5fbbb8a51c482eabfc81b6b8da0 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Fri, 13 Mar 2020 13:08:54 -0700 Subject: [PATCH 4/8] Move [CLSCompliant(false)] and remove AllowMultiple --- .../System/Diagnostics/CodeAnalysis/NullableAttributes.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs index cd14540412e5e..43fa0355c34e7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs @@ -127,11 +127,11 @@ sealed class DoesNotReturnIfAttribute : Attribute } /// Specifies that the method or property will ensure that the listed field and property members have not-null values. - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] - [CLSCompliant(false)] + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)] #if INTERNAL_NULLABLE_ATTRIBUTES internal #else + [CLSCompliant(false)] public #endif sealed class MemberNotNullAttribute : Attribute @@ -144,11 +144,11 @@ public MemberNotNullAttribute(params string[] members) { } } /// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] - [CLSCompliant(false)] + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)] #if INTERNAL_NULLABLE_ATTRIBUTES internal #else + [CLSCompliant(false)] public #endif sealed class MemberNotNullWhenAttribute : Attribute From c12885d1897831f8546a1609341b6f5806c1b0c7 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Fri, 13 Mar 2020 15:28:44 -0700 Subject: [PATCH 5/8] Declare attributes in ref file --- src/libraries/System.Runtime/ref/System.Runtime.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index da3de89123492..5ca49cfbfe85e 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -5616,6 +5616,18 @@ public sealed partial class NotNullWhenAttribute : System.Attribute public NotNullWhenAttribute(bool returnValue) { } public bool ReturnValue { get { throw null; } } } + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)] + [CLSCompliant(false)] + public sealed class MemberNotNullAttribute : Attribute + { + public MemberNotNullAttribute(params string[] members) { } + } + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)] + [CLSCompliant(false)] + public sealed class MemberNotNullWhenAttribute : Attribute + { + public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { } + } [System.AttributeUsageAttribute(System.AttributeTargets.All, Inherited=false, AllowMultiple=true)] [System.Diagnostics.ConditionalAttribute("CODE_ANALYSIS")] public sealed partial class SuppressMessageAttribute : System.Attribute From e3fd12e901c28fdf5459ae4ea24bcbf6289def9e Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Fri, 13 Mar 2020 15:34:04 -0700 Subject: [PATCH 6/8] Store parameters into readonly properties --- .../CodeAnalysis/NullableAttributes.cs | 15 +++++++++++++-- .../System.Runtime/ref/System.Runtime.cs | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs index 43fa0355c34e7..4fa274bfe5872 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs @@ -136,11 +136,15 @@ sealed class DoesNotReturnIfAttribute : Attribute #endif sealed class MemberNotNullAttribute : Attribute { + /// Gets field or property member names. + public string[] Members { get; } + /// Initializes the attribute with the list of field and property members. /// /// The list of field and property members that are promised to be not-null. /// - public MemberNotNullAttribute(params string[] members) { } + public MemberNotNullAttribute(params string[] members) + => this.Members = members; } /// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. @@ -153,6 +157,12 @@ public MemberNotNullAttribute(params string[] members) { } #endif sealed class MemberNotNullWhenAttribute : Attribute { + /// Gets the return value condition. + public bool ReturnValue { get; } + + /// Gets field or property member names. + public string[] Members { get; } + /// Initializes the attribute with the specified return value condition and list of field and property members. /// /// The return value condition. If the method returns this value, the associated parameter will not be null. @@ -160,6 +170,7 @@ sealed class MemberNotNullWhenAttribute : Attribute /// /// The list of field and property members that are promised to be not-null. /// - public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { } + public MemberNotNullWhenAttribute(bool returnValue, params string[] members) + => (this.ReturnValue, this.Members) = (returnValue, members); } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 5ca49cfbfe85e..13b3a97a46f6c 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -5621,12 +5621,15 @@ public NotNullWhenAttribute(bool returnValue) { } public sealed class MemberNotNullAttribute : Attribute { public MemberNotNullAttribute(params string[] members) { } + public string[] Members { get { throw null; } } } [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)] [CLSCompliant(false)] public sealed class MemberNotNullWhenAttribute : Attribute { public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { } + public bool ReturnValue { get { throw null; } } + public string[] Members { get { throw null; } } } [System.AttributeUsageAttribute(System.AttributeTargets.All, Inherited=false, AllowMultiple=true)] [System.Diagnostics.ConditionalAttribute("CODE_ANALYSIS")] From e644186135bbdb06c767e485671bc33f7b290092 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Fri, 13 Mar 2020 16:34:56 -0700 Subject: [PATCH 7/8] Don't use deconstruction --- .../System/Diagnostics/CodeAnalysis/NullableAttributes.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs index 4fa274bfe5872..a500d6b8f4c1b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs @@ -171,6 +171,9 @@ sealed class MemberNotNullWhenAttribute : Attribute /// The list of field and property members that are promised to be not-null. /// public MemberNotNullWhenAttribute(bool returnValue, params string[] members) - => (this.ReturnValue, this.Members) = (returnValue, members); + { + this.ReturnValue = returnValue; + this.Members = members; + } } } From 7e4a7258079173d6c8bb9fe29fe5e12f58539351 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Sun, 15 Mar 2020 16:53:27 -0700 Subject: [PATCH 8/8] Qualify, move properties, remove this --- .../CodeAnalysis/NullableAttributes.cs | 24 +++++++++---------- .../System.Runtime/ref/System.Runtime.cs | 12 +++++----- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs index a500d6b8f4c1b..35a59488cffed 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs @@ -136,15 +136,15 @@ sealed class DoesNotReturnIfAttribute : Attribute #endif sealed class MemberNotNullAttribute : Attribute { - /// Gets field or property member names. - public string[] Members { get; } - /// Initializes the attribute with the list of field and property members. /// /// The list of field and property members that are promised to be not-null. /// public MemberNotNullAttribute(params string[] members) - => this.Members = members; + => Members = members; + + /// Gets field or property member names. + public string[] Members { get; } } /// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. @@ -157,12 +157,6 @@ public MemberNotNullAttribute(params string[] members) #endif sealed class MemberNotNullWhenAttribute : Attribute { - /// Gets the return value condition. - public bool ReturnValue { get; } - - /// Gets field or property member names. - public string[] Members { get; } - /// Initializes the attribute with the specified return value condition and list of field and property members. /// /// The return value condition. If the method returns this value, the associated parameter will not be null. @@ -172,8 +166,14 @@ sealed class MemberNotNullWhenAttribute : Attribute /// public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { - this.ReturnValue = returnValue; - this.Members = members; + ReturnValue = returnValue; + Members = members; } + + /// Gets the return value condition. + public bool ReturnValue { get; } + + /// Gets field or property member names. + public string[] Members { get; } } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 13b3a97a46f6c..edfdcb80776c1 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -5616,16 +5616,16 @@ public sealed partial class NotNullWhenAttribute : System.Attribute public NotNullWhenAttribute(bool returnValue) { } public bool ReturnValue { get { throw null; } } } - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)] - [CLSCompliant(false)] - public sealed class MemberNotNullAttribute : Attribute + [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)] + [System.CLSCompliant(false)] + public sealed class MemberNotNullAttribute : System.Attribute { public MemberNotNullAttribute(params string[] members) { } public string[] Members { get { throw null; } } } - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)] - [CLSCompliant(false)] - public sealed class MemberNotNullWhenAttribute : Attribute + [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited = false)] + [System.CLSCompliant(false)] + public sealed class MemberNotNullWhenAttribute : System.Attribute { public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { } public bool ReturnValue { get { throw null; } }