Skip to content

[BUG] NativeAOT Trim warnings #2447

Closed

Description

@trampster

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

The following Trim warnings are geneated when trying to use the CommunityToolkit with NativeAOT in .net 9 Maui on iOS:

 ILC : Trim analysis warning IL2092: CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Microsoft.Maui.Controls.IValueConverter.Convert(Object,Type,Object,CultureInfo): 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter '' of method 'CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Microsoft.Maui.Controls.IValueConverter.Convert(Object,Type,Object,CultureInfo)' don't match overridden parameter '' of method 'Microsoft.Maui.Controls.IValueConverter.Convert(Object,Type,Object,CultureInfo)'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
    ILC : Trim analysis warning IL2092: CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Microsoft.Maui.Controls.IValueConverter.ConvertBack(Object,Type,Object,CultureInfo): 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter '' of method 'CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Microsoft.Maui.Controls.IValueConverter.ConvertBack(Object,Type,Object,CultureInfo)' don't match overridden parameter '' of method 'Microsoft.Maui.Controls.IValueConverter.ConvertBack(Object,Type,Object,CultureInfo)'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
    ILC : Trim analysis warning IL2092: CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Microsoft.Maui.Controls.IValueConverter.Convert(Object,Type,Object,CultureInfo): 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter '' of method 'CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Microsoft.Maui.Controls.IValueConverter.Convert(Object,Type,Object,CultureInfo)' don't match overridden parameter '' of method 'Microsoft.Maui.Controls.IValueConverter.Convert(Object,Type,Object,CultureInfo)'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
    ILC : Trim analysis warning IL2092: CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Microsoft.Maui.Controls.IValueConverter.ConvertBack(Object,Type,Object,CultureInfo): 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter '' of method 'CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Microsoft.Maui.Controls.IValueConverter.ConvertBack(Object,Type,Object,CultureInfo)' don't match overridden parameter '' of method 'Microsoft.Maui.Controls.IValueConverter.ConvertBack(Object,Type,Object,CultureInfo)'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
    /_/src/CommunityToolkit.Maui/Extensions/ValueConverterExtension.shared.cs(31): Trim analysis warning IL2062: CommunityToolkit.Maui.Extensions.ValueConverterExtension.IsValidTargetType<TTarget>(Type&,Boolean): Value passed to parameter 'type' of method 'System.Activator.CreateInstance(Type)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements.

Expected Behavior

No Trim warnings

Reproduction Sample

https://github.com/trampster/MauiCommunityToolkitTrimIssue

Steps To Reproduce

  1. Create a MAUI project that targets iOS with .net 9 MAUI
  2. Add reference to CommCommunityToolkit.Maui <PackageReference Include="CommunityToolkit.Maui" Version="10.0.0" />
  3. Use the InvertedBoolConverter in xaml
  4. Add bits required to turn on NativeAOT:
<IsAotCompatible>true</IsAotCompatible>
<PublishAot Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">true</PublishAot>
  1. Build using dotnet publish -f net9.0-ios -r ios-arm64
    -> trim warnings from description

Link to public reproduction project repository

MauiCommunityToolkitTrimIssue

Environment

- .NET MAUI CommunityToolkit: 10.0.0
- OS: iOS
- .NET MAUI: 9.0.21

Anything else?

No response

Activity

  1. dotnet-policy-service commented on Jan 16, 2025

    @dotnet-policy-service
    Contributor

    Hi @trampster! We have added the needs reproduction label to this issue, which indicates that we are unable to take further action until you provide a reproduction sample that we can use to reproduce + fix your bug. In accordance with the Reporting a bug section of our Contributing.md that you confirmed reading before you submitted this Issue, we ask that every Issue author provide a link to an open-source repository containing the reproduction sample app. This issue will be closed automatically in 5 days if we do not receive a reproduction sample from you. Please feel free to re-open this Issue once you have provided a reproduction sample.

  2. trampster commented on Jan 16, 2025

    @trampster
    Author

    I have added a link to a reproduction sample. https://github.com/trampster/MauiCommunityToolkitTrimIssue

  3. trampster commented on Jan 16, 2025

    @trampster
    Author

    As a work arround we have created our own InvertBoolConverter that does not produce trim warnings:

    
    [AcceptEmptyServiceProvider]
    public class InvertBoolConverter : IValueConverter
    {
       public object Convert(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture)
       {
          return !((bool)(value ?? false));
       }
    
       public object ConvertBack(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture)
       {
          return value ?? false;
       }
    }
    

    Feel free to use this until the Maui CommunityToolkit is fixed.

  4. jfversluis commented on Jan 20, 2025

    @jfversluis
    Member

    @simonrozsival is there any way to prevent this other than to use #pragma disable?

  5. simonrozsival commented on Jan 20, 2025

    @simonrozsival
    Contributor

    @jfversluis The reason the warnings are shown is that
    the ICommunityToolkitValueConverter adds [DynamicallyAccessedMembers] attribute to parameters that don't have it in the base IValueConverter definition:

    #pragma warning disable IL2092
    object? IValueConverter.Convert(object? value, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type targetType, object? parameter, CultureInfo culture) =>
    Convert(value, targetType, parameter, culture);
    /// <inheritdoc />
    object? IValueConverter.ConvertBack(object? value, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type targetType, object? parameter, CultureInfo culture) =>
    ConvertBack(value, targetType, parameter, culture);
    #pragma warning restore IL2092
    vs. https://github.com/dotnet/maui/blob/1807a6c10fb5ebeb14c0f24883acb3cbddd97a52/src/Controls/src/Core/IValueConverter.cs

    The #pragma warning disable IL2092 silences the roslyn analyzer, but when the NativeAOT compiler (ILC) later compiles the IL, it won't see that #pragma. Instead, it is necessary to use [UnconditionalSuppressMessage] to completely silence those warnings. Something like this should do the trick:

    object? IValueConverter.Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
    	return _Convert();
    
            [UnconditionalSuppressMessage("TrimAnalysis", "IL2092", Justification = "The ToType and FromType properties are attributed with the  appropriate DynamicallyAccessedMembers attributes.")]
    	object? _Convert() => Convert(value, targetType, parameter, culture);
    }
  6. jfversluis commented on Jan 20, 2025

    @jfversluis
    Member

    @trampster would you be able to test the resulting NuGets from this PR build to verify if that fixes this scenario for you in your project?

  7. locked and limited conversation to collaborators on Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions