Skip to content

Configuration binding source generator fails with CS8598 error #89010

@martincostello

Description

@martincostello

Description

Attempting to enable the Configuration binding source generator with a struct used as a custom configuration type causes the source generator to emit code that fails to compile with a CS8598 error.

Repro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\GeneratedConfigurationBinder.g.cs(122,17): error CS8598: The suppression operator is not allowed in this context [Repro\Repro.csproj]

The method generated is below. The failing line is element! = temp4;.

public static void BindCore(IConfiguration configuration, ref IDictionary<string, Geolocation> obj, BinderOptions? binderOptions)
{
    if (obj is null)
    {
        throw new ArgumentNullException(nameof(obj));
    }

    foreach (IConfigurationSection section in configuration.GetChildren())
    {
        if (!(obj.TryGetValue(section.Key!, out Geolocation element)))
        {
            element = InitializeGeolocation(section, binderOptions);;
        }
        var temp4 = InitializeGeolocation(configuration, binderOptions);;
        BindCore(section, ref temp4, binderOptions);
        element! = temp4; // CS8598 occurs here
        obj[section.Key!] = element;
    }
}

Reproduction Steps

  1. Clone martincostello/config-binding-source-generator-cs8598-repro
  2. Run dotnet build

Expected behavior

The application compiles and configuration binding works equivalently to runtime binding.

Actual behavior

The application fails to compile with a CS8598 error.

Regression?

No.

Known Workarounds

None.

Configuration

.NET SDK 8.0.100-preview.6.23330.14

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions