❔ how to supress warning CS8774

The following code throws a warning, how can i suppress it? Even an ! on the end doesn't affect that warning i want users of this function to know that they have a non-null Potato once M has completed. I know that an 'await' returns a task and that during the execution of the task the potato might be null, but that is standard reasoning for an async function - if i call an async anything, the results might not be there until the Task Completes ...
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

public class C {

Object? _potato;

[MemberNotNull(nameof(_potato))]
public async Task M() {
_potato= await FetchPotato()!; //warning CS8774: Member '_potato' must have a non-null value when exiting.
}

public async Task<Object> FetchPotato() {return new Object();}
}
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

public class C {

Object? _potato;

[MemberNotNull(nameof(_potato))]
public async Task M() {
_potato= await FetchPotato()!; //warning CS8774: Member '_potato' must have a non-null value when exiting.
}

public async Task<Object> FetchPotato() {return new Object();}
}
https://sharplab.io/#v2:EYLgtghglgdgPgAQEwEYCwAoBAGABAlAOgBEoIBzGAewGcAXKAYxsIGEqATAUwEEYIANgE8aUGgG5MOfCgCskjFIDM+JLla4A3ply7cOvQHlgAKy6M6AflwB9AA5U6EOlQV79GdwG0AslzDAXABOAHKOIQCuAgIAFPxgXFQAZjH2js5UAJSZALoGuggqCAAc+ABsuD4xmVr57rYOTi4AvPgAnLgAYlx0jAAWAArpLtUAhOK4APSTAO4QQTCw5OoAysUA7OsALCCV/oFBuADkaU1UR7hgEfS4fRAAbly4ELjUMAC0MFECuPeCEU8Zn0uDBcFwAB5QBgwciEOoAXzqdUK+FKCDKAB5jGYLAA+Lo9fpDM7VLQIdavLgzXDY8x0ariREYeFAA===
SharpLab
C#/VB/F# compiler playground.
8 Replies
JakenVeina
JakenVeina2y ago
you probably need to wrap the call
_potato = (await FetchPotato())!;
_potato = (await FetchPotato())!;
without that, the ! notation is applying to the Task being awaited, not the awaited result
Aart Bluestoke
Aart BluestokeOP2y ago
@JakenVeina adding brackets doesn't fix it 😦
jcotton42
jcotton422y ago
the issue is this is valid
var foo = M();
_potato.ToString(); // NRE
await foo;
var foo = M();
_potato.ToString(); // NRE
await foo;
JakenVeina
JakenVeina2y ago
huh? how is that the same scenario? ohhhh, right as in, that's the caller's scenario [MemberNotNull] is not interpreted async-ly
Aart Bluestoke
Aart BluestokeOP2y ago
not the pit of success 😦 eg; if i checked the Result property of an Task that is not completed, i'm in undefined behavior; of course this could be NRE - i wouldn't expect invariants of the function to be enforced while the Task is incomplete ... Question still stands then - any way to suppress the warning, or should i just #pragma around it?
JakenVeina
JakenVeina2y ago
what you would be looking for is something like [MemberNotNull] except async-aware which doesn't exist so, no if you want to suppress the warnings and risk consumers being able to fatally misuse the API, go for it
indigo
indigo2y ago
There have been some proposals brought up before https://github.com/dotnet/csharplang/discussions/5657 https://github.com/dotnet/csharplang/issues/6888 Not sure how far it's gotten since then though
GitHub
API Proposal: MemberNotNullWhenCompleteAttribute · dotnet csharplan...
Background and Motivation MemberNotNullAttribute indicate that when the method return, the given members are not null. This is perfect when your Initialisation code is fully synchronous. But when u...
GitHub
Require await to apply nullable postconditions to task-returning ca...
Related discussion: #5657 Summary Assume that nullable postcondition attributes such as MemberNotNull only apply when Task-returning calls are directly awaited. Adjust analysis of implementations t...
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server