aspnetcore
aspnetcore copied to clipboard
Request delegate generator produces CS8073 for ValueTask<T?> results
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the bug
For delegates that return `ValueTask<T?>? the compiler emits
Warning CS8073 The result of the expression is always 'false' since a value of type 'ValueTask<Person>' is never equal to 'null' of type 'ValueTask<Person>?'
This is especially cumbersome when
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
is set.
Expected Behavior
No warning is produced.
Steps To Reproduce
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
Person[] persons = [new Person("Batman"), new Person("Me")];
// No problem
Person? SearchPerson0(string name) => Array.Find(persons, p => p.Name == name);
// Here the ValueTask causes the CS8073
ValueTask<Person?> SearchPerson1(string name)
{
Person? person = Array.Find(persons, p => p.Name == name);
return ValueTask.FromResult(person);
}
// This is fine, as Task is a reference type
Task<Person?> SearchPerson2(string name)
{
Person? person = Array.Find(persons, p => p.Name == name);
return Task.FromResult(person);
}
app.MapGet("person0/{name}", SearchPerson0);
app.MapGet("person1/{name}", SearchPerson1);
app.MapGet("person2/{name}", SearchPerson2);
app.Run();
public record Person(string Name);
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup>
<IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers>
<EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.AspNetCore.Http.Generated</InterceptorsPreviewNamespaces>
</PropertyGroup>
</Project>
Exceptions (if any)
No response
.NET Version
8.0.200
Anything else?
If this got already fixed for .NET 9 is there a chance to backport it to .NET 8 as <TreatWarningsAsErrors>
needs some workarounds to make it compile and .NET 8 is a LTS?