Skip to content

Conversation

@Sergio0694
Copy link
Member

Closes #1860, #1342

This PR adds a new analyzer to warn when doing trim-unsafe casts to projected runtime classes.

Copy link

@Lamparter Lamparter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!!!

@Sergio0694 Sergio0694 force-pushed the dev/runtime-class-cast-analyzer branch from ef36376 to a92f6a8 Compare March 27, 2025 18:39
<data name="RuntimeClassCast_Brief" xml:space="preserve">
<value>Casts to WinRT runtime class types are not trim-safe</value>
</data>
<data name="RuntimeClassCast_Text" xml:space="preserve">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like both error messages, but especially this one, is missing the context on when it is not trim-safe / might be trimmed which is when they are declared as object / lacking static type information.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the problem is that it's more complicated than that. It's not just when there's no static type information. Casting to a derived class is also not trim-safe, even if the source type is not just object. I can update the messages and include this info too, was just not sure how verbose and long we wanted the messages to be? 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I would still consider the latter to be lack of static type information but with respect to not being aware of derived type. We can probably generalize it with something along the lines of can't be determined with static type information or so?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

Casting to type '{0}' where the type being cast to is never instantiated in the C# implementation is not trim-safe as the type can be trimmed.

or

Casting to type '{0}' where the type being cast to was statically not known about when the C# wrapper was created is not trim-safe as the type can be trimmed.

<value>Casts to WinRT 'IReference`1&lt;T&gt;' unboxed values are not trim-safe</value>
</data>
<data name="IReferenceTypeCast_Text" xml:space="preserve">
<value>Casting to type '{0}' is not trim-safe, as it is unboxing a WinRT 'IReference`1&lt;T&gt;' value type. Consider using the 'WinRT.CastExtensions.Cast&lt;T&gt;' extension method instead, which will ensure the necessary metadata is kept. Alternatively, you can use '[DynamicDependency]' to manually root the necessary metadata, and suppress the warning via '[UnconditionalSuppressMessage]'.</value>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<value>Casting to type '{0}' is not trim-safe, as it is unboxing a WinRT 'IReference`1&lt;T&gt;' value type. Consider using the 'WinRT.CastExtensions.Cast&lt;T&gt;' extension method instead, which will ensure the necessary metadata is kept. Alternatively, you can use '[DynamicDependency]' to manually root the necessary metadata, and suppress the warning via '[UnconditionalSuppressMessage]'.</value>
<value>Casting to type '{0}' is not trim-safe, as it is unboxing a WinRT 'IReference`1&lt;T&gt;' value type that was statically not known. Consider using the 'WinRT.CastExtensions.Cast&lt;T&gt;' extension method instead, which will ensure the necessary metadata is kept. Alternatively, you can use '[DynamicDependency]' to manually root the necessary metadata, and suppress the warning via '[UnconditionalSuppressMessage]'.</value>

@Sergio0694 Sergio0694 merged commit b108210 into staging/2.3 Apr 1, 2025
10 checks passed
@Sergio0694 Sergio0694 deleted the dev/runtime-class-cast-analyzer branch April 1, 2025 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants