Skip to content
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

[Trimming] Fix trimming unfriendly usage of implicit operators through reflection #19922

Closed
simonrozsival opened this issue Jan 16, 2024 · 2 comments
Assignees
Labels
area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) fixed-in-9.0.0-preview.3.10457 t/app-size Application Size / Trimming (sub: perf)

Comments

@simonrozsival
Copy link
Member

MAUI uses implicit operators invoked via reflection in several places, for example:

Unfortunately, this pattern isn't trimming/AOT compatible and we cannot fix it with [DynamicallyAccessedMembers] (in some cases the type isn't known at compile time, such as in value.GetType().GetImplicitConversionOperator(...)). When app code is trimmed, some of the required implicit conversion operators might not be available at runtime leading to unexpected behavior.

Related to #18658, /cc @jonathanpeppers
Trimming warnings related to this issue are tracked in #19397

Possible solutions

  1. Make the use of implicit operators obsolete and replace them with a different mechanism, such as IValueConverter
    • we could still keep the existing code with implicit operators, but it would be the responsibility of the developers to ensure the implicit operators are preserved by the trimmer using [Preserve] or [DynamicDependency]
    • this would be a breaking change to some apps
  2. Globally disable trimming of implicit operators
@jonathanpeppers
Copy link
Member

@StephaneDelcroix might want to comment, but I wouldn't think that implicit operators are commonly used.

For example, if the example is something like:

class MyType
{
    public string Text { get; set; }

    public static implicit operator string(MyType type) => type.Text;

    // NOTE: they could just use ToString()
}

If we document they have to use IValueConverter, that seems like an OK solution.

The only problem would be if type conversion of a BCL type is using an implicit operator by chance, and we didn't know it. In that case, we can probably add a strongly-typed code path for that conversion.

@simonrozsival
Copy link
Member Author

Implementation was merged into the net9.0 branch

@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2024
@Eilon Eilon added the area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) label May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) fixed-in-9.0.0-preview.3.10457 t/app-size Application Size / Trimming (sub: perf)
Projects
None yet
Development

No branches or pull requests

4 participants