-
Notifications
You must be signed in to change notification settings - Fork 122
Add analyzer for trim-unsafe casts to projected runtime class types #1956
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
Conversation
Lamparter
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!!!
ef36376 to
a92f6a8
Compare
src/Authoring/WinRT.SourceGenerator/Analyzers/RuntimeClassCastAnalyzer.cs
Outdated
Show resolved
Hide resolved
| <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"> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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? 😅
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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<T>' 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<T>' value type. Consider using the 'WinRT.CastExtensions.Cast<T>' 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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| <value>Casting to type '{0}' is not trim-safe, as it is unboxing a WinRT 'IReference`1<T>' value type. Consider using the 'WinRT.CastExtensions.Cast<T>' 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<T>' value type that was statically not known. Consider using the 'WinRT.CastExtensions.Cast<T>' 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> |
Closes #1860, #1342
This PR adds a new analyzer to warn when doing trim-unsafe casts to projected runtime classes.