Repository navigation
Closed
Description
Description
When configuration binding generation is enabled by means of:
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<Features>$(Features);InterceptorsPreview</Features>…a project can fail to compile with this error message:
/app/repro/Microsoft.Extensions.Configuration.Binder.SourceGeneration/Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator/BindingExtensions.g.cs(87,25): e
rror CS0664: Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type [/app/repro/repro.csproj]
Reproduction Steps
Given the following project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<Features>$(Features);InterceptorsPreview</Features>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0-rtm.23477.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-rtm.23477.9" />
</ItemGroup>
</Project>…and the following Program.cs:
using Microsoft.Extensions.Configuration;
var c = new ConfigurationBuilder().Build();
var x = c.GetSection("Anything").Get<Values>();
Console.WriteLine(x);
readonly record struct Values(int A, string B, decimal C = 2.30m);…run dotnet build via SDK version "8.0.100-rtm.23477.37".
Expected behavior
The compilation succeeds, and the value is bound from configuration.
Actual behavior
Compilation fails with the following error:
/app/repro/Microsoft.Extensions.Configuration.Binder.SourceGeneration/Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator/BindingExtensions.g.cs(87,25): e
rror CS0664: Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type [/app/repro/repro.csproj]
The line in question in the generated code appears here in a method named InitializeValues (but really Initialize{typename}):
decimal C = 2.30;
if (configuration["C"] is string value2)
{
C = ParseDecimal(value2, () => configuration.GetSection("C").Path);
}Regression?
I'm afraid I don't know.
Known Workarounds
No response
Configuration
.NET Version: 8.0.100-rtm.23477.37
OS and version: macOS 13.5.1 (22G90)
Architecture: x64
Specific?: No, I don't think so.
Blazor: ❌
Other information
You know what, I just checked initializing float C = 2.30f as well, and it behaves the same.
float C = 2.3;
if (configuration["C"] is string value2)
{
C = ParseFloat(value2, () => configuration.GetSection("C").Path);
}
Activity
[-]Configuration Binding Generator Initializes `decimal` Default Value Incorrectly[/-][+]Configuration Binding Generator Initializes Suffixed Default Values Incorrectly[/+]ericstj commentedon Oct 3, 2023
I confirmed this repro in the latest codebase. We'll need to see what's missing from how we emit the default value. Thanks for the report @chrisoverzero
ericstj commentedon Oct 6, 2023
So it would seem that JSON has a similar bug to this. It doesn't produce code that doesn't compile, but it does produce code that loses precision. Then using that code at runtime produces an exception.
Here's the JSON repro:
This happens because JSON produces this:
DefaultValuehere isruntime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonParameterInfoValues.cs
Line 43 in 7b08680
That syntax results in initializing the
objectvalue to a boxeddoublewhich will throw at runtime:This is related to this issue, which was missing this case: dotnet/roslyn#58705
I believe the fix here, needed for both Configuration and Json is refine and share our literal formatting helper:
runtime/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
Lines 1354 to 1403 in 7204fbe
CC @dotnet/area-system-text-json
ericstj commentedon Oct 7, 2023
I have a fix for this, but in testing it I hit #93159. I'll try to get the fix done for just this case and move on to that next.
ericstj commentedon Oct 10, 2023
@chrisoverzero is this issue blocking your adoption of the source generator in 8.0? I'm trying to determine if this is worth backporting to our release branch.
chrisoverzero commentedon Oct 10, 2023
It is not blocking me at this time, no. It’s something I discovered when doing reproduction for another issue.
ericstj commentedon Oct 10, 2023
Ok, closing this out for now, the fix will remain in the 9.0 codebase.
ericstj commentedon Oct 26, 2023
While the
decimalcase isn't common, this also fixed the formatting of Enums with default values with is much more common. We should consider this for servicing.ericstj commentedon Jul 22, 2024
This was serviced in 213f1a2