-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add [MemberNotNull] and [MemberNotNullWhen] support #85
Conversation
@@ -94,7 +94,7 @@ Namespace Global.System.Diagnostics.CodeAnalysis | |||
|
|||
''' <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary> | |||
<AttributeUsage(AttributeTargets.Parameter, Inherited:=False)> | |||
Friend NotInheritable Class DoesNotReturnAttribute | |||
Friend NotInheritable Class DoesNotReturnIfAttribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's an actual bug, as DoesNotReturnAttribute
already appears earlier in the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice find. We have a test hole. @sharwell
@@ -110,4 +110,65 @@ Namespace Global.System.Diagnostics.CodeAnalysis | |||
Public ReadOnly Property ParameterValue As Boolean | |||
End Class | |||
|
|||
''' <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary> | |||
<AttributeUsage(AttributeTargets.Method Or AttributeTargets.Property, Inherited:=False, AllowMultiple:=True)> | |||
Friend NotInheritable Class MemberNotNullAttribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm by no means a VB.NET expert, so please double check (and forgive any rookie mistake 😅)
|
||
protected override void ImplementAttribute(ModuleDefinition module, TypeDefinition attribute, WellKnownTypes wellKnownTypes, CustomAttributeFactory attributeFactory) | ||
{ | ||
var constructor1 = MethodFactory.Constructor(wellKnownTypes.TypeSystem); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the first time I play with Cecil, so you'll likely want to triple-check I'm not doing anything stupid 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for this, we really appreciate it!
@sharwell I'm less familiar with the ProvidedAttributeType part. Does it need custom attributes to be added on the string array like NotNullIfNotNullAttributeProvidedType does, and could we follow up after this PR with a test that would fail if this is needed and missing?
Lines 18 to 27 in 05b952d
protected override void ImplementAttribute(ModuleDefinition module, TypeDefinition attribute, WellKnownTypes wellKnownTypes, CustomAttributeFactory attributeFactory) | |
{ | |
attribute.CustomAttributes.Add(attributeFactory.NullableContext(1)); | |
attribute.CustomAttributes.Add(attributeFactory.Nullable(0)); | |
attribute.CustomAttributes.Add(attributeFactory.AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, allowMultiple: true, inherited: false)); | |
var constructor = MethodFactory.Constructor(wellKnownTypes.TypeSystem); | |
constructor.Parameters.Add(new ParameterDefinition("parameterName", ParameterAttributes.None, wellKnownTypes.TypeSystem.String)); | |
attribute.Methods.Add(constructor); | |
} |
|
||
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary> | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] | ||
internal class MemberNotNullAttribute : Attribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these should be declared as sealed. Could you update the URL at the top to https://github.com/dotnet/runtime/blob/v5.0.0-rc.1.20451.14/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs and then compare against it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch! Looks like I accidentally removed it when copying the definition 😅
Fixed.
3a5c6b7
to
2bd2f90
Compare
Wooops, looks like I broke something. I'll take a look 😅 |
I tried to add I'm out of ideas. I'll wait for @sharwell to magically find the root cause in 3 seconds 🤣 |
Hmm, I can confirm that this repros locally for me on member_not_null but not on master:
|
…ibute/MemberNotNullWhenAttribute's definition
Waiting for @sharwell seems like a good plan. The only idea I have is to do a binary search of deleting half your changes repeatedly until we find the line that seems to perturb the behavior into failing. Bit slow of a process. |
|
||
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary> | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] | ||
internal sealed class MemberNotNullAttribute : Attribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 These need to be conditionally included for .NET Standard 2.1 and .NET Core 3.x builds when GenerateNullableAttributes
is set. See here for an example:
This will require conditional directives in the source file, and modifications to the handling of GenerateNullableAttributes
in the build targets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum yeah, great point. I'll give it a try when I have a moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Let me know if I got it right.
Latest commits improved things, but we're back to the |
@kevinchalet Been busy but hoping to get to this 😄 |
Haha, no worries. I closed this PR as I wasn't able to fix it on my own, but I'd be happy to revive it if we can find the root cause of the issue 😄 |
Fixes #84.