meziantou / Meziantou.Polyfill

MIT License
43 stars 1 forks source link

Generated code for System.Index/System.Range require additional polyfills #39

Closed RachelDavids closed 5 months ago

RachelDavids commented 5 months ago

Version of the Meziantou.Polyfill NuGet package

1.0.38

Description

Generated code for System.Index requires both System.Diagnostics.CodeAnalysis.NotNullWhenAttribute and System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute

Reproduction Steps

Add the following to the project file.

    <PropertyGroup>
      <!-- semicolon-separated or pipe-separated list of name prefix -->
      <!-- Tip: The name of the generated polyfills are available in the generated "Debug.g.cs" file -->
      <MeziantouPolyfill_IncludedPolyfills>T:System.Index|T:System.Range</MeziantouPolyfill_IncludedPolyfills>
      <MeziantouPolyfill_ExcludedPolyfills>M:System</MeziantouPolyfill_ExcludedPolyfills>
    </PropertyGroup>
    <ItemGroup Label="CodeGenerators">
        <PackageReference Include="Meziantou.Polyfill" Version="1.0.38">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
    </ItemGroup>

Build generates the following errors ..\obj\Debug\netstandard2.0\gen\Meziantou.Polyfill\Meziantou.Polyfill.PolyfillGenerator\T_System.Index.g.cs(119,78): error CS0122: 'NotNullWhenAttribute' is inaccessible due to its protection level [S:\Projects\Rachel\Tools\BuildAsSdk\MSBuild\Tasks\MSBuild.Tasks.csproj] ..\obj\Debug\netstandard2.0\gen\Meziantou.Polyfill\Meziantou.Polyfill.PolyfillGenerator\T_System.Index.g.cs(147,54): error CS0122: 'DoesNotReturnAttribute' is inaccessible due to its protection level [S:\Projects\Rachel\Tools\BuildAsSdk\MSBuild\Tasks\MSBuild.Tasks.csproj] ..\obj\Debug\netstandard2.0\gen\Meziantou.Polyfill\Meziantou.Polyfill.PolyfillGenerator\T_System.Range.g.cs(111,54): error CS0122: 'DoesNotReturnAttribute' is inaccessible due to its protection level [S:\Projects\Rachel\Tools\BuildAsSdk\MSBuild\Tasks\MSBuild.Tasks.csproj] ..\obj\Debug\netstandard2.0\gen\Meziantou.Polyfill\Meziantou.Polyfill.PolyfillGenerator\T_System.Range.g.cs(38,78): error CS0122: 'NotNullWhenAttribute' is inaccessible due to its protection level [S:\Projects\Rachel\Tools\BuildAsSdk\MSBuild\Tasks\MSBuild.Tasks.csproj]

Other information

Workaround is possible by adding the two additional types to the MeziantouPolyfill_IncludedPolyfills property

<MeziantouPolyfill_IncludedPolyfills>T:System.Index|T:Sysrem.Range|T:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute|T:System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute</MeziantouPolyfill_IncludedPolyfills>
meziantou commented 5 months ago

I do not reproduce the error. Here's the full csproj

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>

        <MeziantouPolyfill_IncludedPolyfills>T:System.Index|T:System.Range</MeziantouPolyfill_IncludedPolyfills>
        <MeziantouPolyfill_ExcludedPolyfills>M:System</MeziantouPolyfill_ExcludedPolyfills>
    </PropertyGroup>

    <ItemGroup Label="CodeGenerators">
        <PackageReference Include="Meziantou.Polyfill" Version="1.0.38">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
    </ItemGroup>
</Project>
meziantou commented 5 months ago

I do reproduce the issue. I was just targeting a recent TFM instead of an older one.

This is the expected behavior. When you set the MeziantouPolyfill_IncludedPolyfills property, you must set all the required types. Otherwise, they won't be generated. That's why you must include DoesNotReturnAttribute and NotNullWhenAttribute.