Repository navigation
[Proposal]: NotNullWhenMember attribute
#8838
Replies: 19 comments
|
The existing The suggested That still leaves a gap where a method returns Boolean which determines if another method returns null. For that reason extending |
|
I'm okay with any of the approaches. The case where a method return determines another method return nullability wasn't much of a concern to me, but is definitely something to consider for this proposal. |
|
Could you please share the original scenario from roslyn? |
|
@RikkiGibson It's the |
|
@RikkiGibson FWIW, i would very much like this to be supported. This pattern, while not that common in Roslyn, is a super PITA when it does arise, leading to annoying superfluous suppressions. Thanks! |
|
It looks like the reason that extending |
|
@RikkiGibson what i would prefer it some way of saying, for a method: if you call this, you will always get something non-null back (regardless of parametrs) as long as this other call returned |
|
Some thoughts:
|
|
@333fred
The member name passed to the attribute won't just be the method name, but also parameter types. There can be multiple ways to specify which overload is referred to, but aligning with If overload resolution wasn't your concern for I'll update the proposal and either swap the alternative with the core proposal, or only keep the alternative as the core proposal. Edit: I see the generic case in the infoof article, I commented too fast 😄 But it seems it's also doable: |
It feels like the attribute can be placed on either one of the two symbols involved. We control the internal symbol model, so we can always set things up appropriately so that when the flow analysis visits 'IsA', it knows it needs to do something about 'AsA()'. |
I don't believe this is true, particularly once you start considering inheritance. We've also talked about versions of |
|
MemberNotNull doesn't work across inheritance. I don't think there's a need to support inheritance as a part of solving this issue. Regarding changing states of nested members: that's interesting. I'd be curious to know more about the scenarios where people wished they had it. It seems relevant for when the user doesn't control the definition of the type of their field but would like to check/ensure/assert on the state within the nested fields. In that case it feels like it could be good enough to just support MemberNotNull on parameters without trying to devise a "member path" encoding that could be used to refer to nested members in a MemberNotNull. |
|
All that said, I'm not a strong supporter of introducing NotNullWhenMember, at least until I better understand what it would take to make MemberNotNull able to refer to methods unambiguously (need to re-read and make sure I understand the info-of discussion). |
|
Wouldn't this only be correct on immutable objects? The current |
|
Just ran into this today. Our use case: private class ResourceAndErrors
{
public object? Resource { get; init; }
public IList<string>? Errors { get; init; }
public bool IsValid => Resource != null || Errors != null;
}
var thing = new ResourceAndErrors();
if(!thing.IsValid)
return null;
// Warning: `thing.Resource` might be null
thing.Resource.ToString(); |
|
That looks like a case where MemberNotNullWhen (already existing) would work. |
|
I would suggest like to suggest expanding existing NotNullWhen to the Enum value namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true)]
internal sealed class NotNullWhenAttribute<TEnum> : Attribute
where TEnum: enum
{
public NotNullWhenAttribute(TEnum returnValue);
public TEnum ReturnValue { get; }
}
} |
|
@YIShikunov I agree strongly. But can you open another discussion on this. Having good examples would also be valuable. To me, cases like Json.net would really benefit here. |
|
@CyrusNajmabadi #6170 We can try to continue here |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
NotNullWhenMemberattributeSummary
A nullable attribute specifies that a method return value is not null if some field/property is true/false.
Motivation
There is no way to properly track null states for the following (example is simplified from roslyn repo):
Detailed design
A new attribute will be added to the runtime:
Then the compiler should track the nullability states properly
Drawbacks
Alternatives
Allow the existing
MemberNotNullWhenAttributeto work with methods, ie, the following would work:Unresolved questions
Design meetings
All reactions