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

Add MemberNotNull/When attributes #33567

Merged
merged 8 commits into from
Mar 19, 2020
Merged

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented Mar 13, 2020

Fixes #31877
Relates to dotnet/roslyn#41438 (add support for MemberNotNull in C# 9)

@jcouv
Copy link
Member Author

jcouv commented Mar 13, 2020

@stephentoub Do we care about CLS compliance? If not, I'll suppress the diagnostic.

@stephentoub
Copy link
Member

What is the compiler complaining about here? i.e. what here isn't CLS compliant?

@jcouv
Copy link
Member Author

jcouv commented Mar 13, 2020

The compiler is complaining because of the string[] (sharplab).
I'll double-check the code and spec for rationale, but I'm assuming that's by-design.

@stephentoub
Copy link
Member

I see. I could not have told you arrays as attribute arguments aren't CLS compliant.
https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs3016

@stephentoub
Copy link
Member

I assume attributing it as [CLSCompliant(false)] makes it go away? If so, let's do that.

@stephentoub
Copy link
Member

We've been having another conversation (cc: @jkotas, @ericstj, @terrajobst) about whether we care about CLS compliance at all moving forward, but here I wouldn't worry about it at all, regardless.

@@ -125,4 +125,41 @@ sealed class DoesNotReturnIfAttribute : Attribute
/// <summary>Gets the condition parameter value.</summary>
public bool ParameterValue { get; }
}

/// <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, AllowMultiple = false, Inherited = false)]
Copy link
Member

Choose a reason for hiding this comment

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

AllowMultiple = false is the default and so doesn't need to be specified. Same below.

@jcouv jcouv marked this pull request as ready for review March 13, 2020 20:48
sealed class MemberNotNullAttribute : Attribute
{
/// <summary>Gets field or property member names.</summary>
public string[] Members { get; }
Copy link
Member

Choose a reason for hiding this comment

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

@terrajobst, concerns?

Copy link
Member Author

Choose a reason for hiding this comment

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

@terrajobst Just confirming that it's okay to add the properties to store the arguments to the attribute. This is following what we did for other nullability attributes.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. NullableAttribute also exposes a raw byte array. /cc @bartonjs

@@ -5616,6 +5616,21 @@ 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)]
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Add the "System." namespace to these (see existing examples). The tool that generates these files would do so, so without them this will just result in churn the next time the files are auto-gen'd.

{
/// <summary>Gets field or property member names.</summary>
public string[] Members { get; }

Copy link
Member

Choose a reason for hiding this comment

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

Nit: please move the properties to below the ctor (here and in the other attribute as well)

/// The list of field and property members that are promised to be not-null.
/// </param>
public MemberNotNullAttribute(params string[] members)
=> this.Members = members;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: remove the "this."

public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
{
this.ReturnValue = returnValue;
this.Members = members;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: remove the "this."

@jcouv
Copy link
Member Author

jcouv commented Mar 19, 2020

@stephentoub I think this is ready to merge/squash.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

Thanks.

@stephentoub stephentoub merged commit 90ab57c into dotnet:master Mar 19, 2020
@stephentoub
Copy link
Member

@jcouv, do you know when dotnet/runtime will get a compiler that respects this?

@jcouv jcouv deleted the member-notnull branch March 19, 2020 18:47
@jcouv
Copy link
Member Author

jcouv commented Mar 19, 2020

do you know when dotnet/runtime will get a compiler that respects this?

@stephentoub I checked that version 3.6.0-3.20168.4 of the compiler package has MemberNotNull support. This means any 3.6.0-3.* version would have it, and possibly some 3.6.0-2.* as well.
I'm not sure when the runtime repo picks up a new compiler. But in this case, using MemberNotNull also requires updating the language version to "preview", which may be more problematic...

@stephentoub
Copy link
Member

But in this case, using MemberNotNull also requires updating the language version to "preview", which may be more problematic...

Shouldn't be 😉

<LangVersion Condition="'$(Language)' == 'C#'">preview</LangVersion>

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New API: MemberNotNullAttribute and MemberNotNullWhenAttribute
5 participants