Skip to content

InputBase<TValue> produces trim warnings IL2091 for built in types #11718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alexaka1 opened this issue Apr 8, 2025 · 8 comments · Fixed by #11856
Closed

InputBase<TValue> produces trim warnings IL2091 for built in types #11718

alexaka1 opened this issue Apr 8, 2025 · 8 comments · Fixed by #11856
Assignees
Labels
area-blazor area-compiler Umbrella for all compiler issues
Milestone

Comments

@alexaka1
Copy link

alexaka1 commented Apr 8, 2025

This code emits the exact warning like what xantari mentioned.

@page "/debug/radio-buttons"
@rendermode InteractiveWebAssembly

<div>
    <InputRadioGroup @bind-Value="value1">
        <InputRadio Value="@("false")" />
        <InputRadio Value="@("true")" />
    </InputRadioGroup>
</div>

@code {

private string value1 = "true";

}
/_/redacted.Client/obj/Release/net9.0/Microsoft.CodeAnalysis.Razor.Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Pages_DebugPages_RadioButtonPalette_razor.g.cs(285,9): warning IL2091: 'TValue' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in 'Microsoft.AspNetCore.Components.Forms.InputRadioGroup<TValue>'. The generic parameter 'TValue' of '__Blazor.redacted.Client.Pages.DebugPages.RadioButtonPalette.TypeInference.CreateInputRadioGroup_0<TValue>(RenderTreeBuilder, Int32, Int32, TValue, Int32, EventCallback<TValue>, Int32, Expression<Func<TValue>>, Int32, RenderFragment)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.  
/_/redacted.Client/obj/Release/net9.0/Microsoft.CodeAnalysis.Razor.Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Pages_DebugPages_RadioButtonPalette_razor.g.cs(302,9): warning IL2091: 'TValue' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in 'Microsoft.AspNetCore.Components.Forms.InputRadio<TValue>'. The generic parameter 'TValue' of '__Blazor.redacted.Client.Pages.DebugPages.RadioButtonPalette.TypeInference.CreateInputRadio_1<TValue>(RenderTreeBuilder, Int32, Int32, TValue, Int32, Object)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.  
...

I am at a loss what I am doing wrong.

Originally posted by @alexaka1 in #44845

I have the following enabled in my project:

<PropertyGroup Label="AOT analyzers">
    <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
    <EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
    <EnableAotAnalyzer>true</EnableAotAnalyzer>
  </PropertyGroup>
  <PropertyGroup Label="AOT">
    <RunAOTCompilation>true</RunAOTCompilation>
    <OptimizationPreference>Speed</OptimizationPreference>
    <WasmStripILAfterAOT>true</WasmStripILAfterAOT>
    <RequiredWorkloads>wasm-tools</RequiredWorkloads>
  </PropertyGroup>
@ghost ghost added the area-blazor label Apr 8, 2025
@alexaka1 alexaka1 changed the title InputBase<TValue> produces trim warnings IL2091 InputBase<TValue> produces trim warnings IL2091 for built in types Apr 8, 2025
@javiercn
Copy link
Member

javiercn commented Apr 8, 2025

@alexaka1 thanks for contacting us.

Seems like RadioGroup has an annotation, but InputBase doesn't

RG:
https://github.com/dotnet/aspnetcore/blob/8491a278ab03850dbbe3f93a6f304852751c9980/src/Components/Web/src/Forms/InputRadioGroup.cs#L13

InputBase:
https://github.com/dotnet/aspnetcore/blob/8491a278ab03850dbbe3f93a6f304852751c9980/src/Components/Web/src/Forms/InputBase.cs#L17

I'm not sure if the issue is that, or if it's something on the generated code that is not being properly annotated.

Moving this to the razor repo so that they can take a look at the generated code.

@javiercn javiercn transferred this issue from dotnet/aspnetcore Apr 8, 2025
@alexaka1
Copy link
Author

alexaka1 commented Apr 9, 2025

I have created a small repro: https://github.com/alexaka1/repro-dotnet-razor-11718

  1. Look at BlazorWasmAot/BlazorWasmAot.Client/Pages/Counter.razor. Should be standard Blazor
  2. Look at BlazorWasmAot/BlazorWasmAot.Client/BlazorWasmAot.Client.csproj:10 and below. AOT is enabled.
  3. dotnet build -c Release
  4. Observe 8 warnings of IL2091 in the console

@alexaka1
Copy link
Author

alexaka1 commented Apr 9, 2025

If I set EnableTrimAnalyzer to false, the warning goes away. Maybe I don't fully understand Blazor Wasm Aot, I've reread the docs and they say this kind of AOT compilation is not like the traditional one, because there is no trimming. Does that mean the trim analyzer is safe to turn off? (as long as only RunAOTCompilation is enabled for the client, and the server is not AOT)

@jjonescz jjonescz added the area-compiler Umbrella for all compiler issues label Apr 9, 2025
@chsienki
Copy link
Member

@jjonescz Please take a look.

@jjonescz
Copy link
Member

The generated code looks like this:

public static void CreateInputRadioGroup_0<TValue>(...)
{
    __builder.OpenComponent<global::Microsoft.AspNetCore.Components.Forms.InputRadioGroup<TValue>>(seq);

Where we get warning

warning IL2091: 'TValue' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in 'Microsoft.AspNetCore.Components.Forms.InputRadioGroup'

If I understand the trimmer warning correctly, Razor needs to look at InputRadioGroup from which it generates the helper method CreateInputRadioGroup and copy over the DynamicallyAccessedMemberTypes annotations (we already copy over any where constraints). In this case, the generated code would look like this:

public static void CreateInputRadioGroup_0<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TValue>(...)
{
    __builder.OpenComponent<global::Microsoft.AspNetCore.Components.Forms.InputRadioGroup<TValue>>(seq);

@alexaka1
Copy link
Author

Thanks @jjonescz. Any chance this lands in current LTS and STS?

@jjonescz
Copy link
Member

This fix is currently targeting .NET 10. I don't think it meets the bar for backporting to earlier releases.

@alexaka1
Copy link
Author

☹️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor area-compiler Umbrella for all compiler issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants