C
C#โ€ข15mo ago
Aart Bluestoke

โ” 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
JakenVeinaโ€ข15mo 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 Bluestokeโ€ข15mo ago
@JakenVeina adding brackets doesn't fix it ๐Ÿ˜ฆ
jcotton42
jcotton42โ€ข15mo ago
the issue is this is valid
var foo = M();
_potato.ToString(); // NRE
await foo;
var foo = M();
_potato.ToString(); // NRE
await foo;
JakenVeina
JakenVeinaโ€ข15mo 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 Bluestokeโ€ข15mo 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
JakenVeinaโ€ข15mo 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
indigoโ€ข15mo 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
Accordโ€ข15mo 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
More Posts
โ” How to get the main window from a custom control?I am trying to make a custom top bar instead of wpfs defualt minimize maximize and drag buttons howeโ” cron task, 1 task is delayingRecurringJob.AddOrUpdate<ReportService>(x => x.SendTimeSpecificLocationSummary(), "0 0 7,11,15,20 ? โ” MongoDB.Driver.MongoConnectionException:Hi, I have problem with finding document in MongoDB collection. Whenever method collection.find is fโ” DataGridView Rows SizeDo you how can I make the rows size to cover the whole datagridview?โ” Maximum Subarray Leetcode Help```C# public int MaxSubArray(int[] nums) { //declarations int max_ending_hโ” [SOLVED :white_check_mark:] [Linux] How could I send a keyPress/keyRelease to some application ?As title says, how I could do that ? On linux I can't use SendKeys, because library System.Windows.FCustom attribute for ValidationAttribute, am I able to get the property being validated dynamically?Looking for a way to have a custom validation attribute apply to two related properties... simple soโ” Catching an error status code in HTML agility pack when using LoadFromWebAsyncI'm using HTML agility pack to request multiple web pages simultaneously. I'm struggling to see how โœ… System.StackOverflowException```cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using SyIterating over AsyncEnumerable and writing to MemoryStreamMainly a question on how you all would go about this, I am thinking of just initializing a `Semaphor