-
Notifications
You must be signed in to change notification settings - Fork 983
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
Get TrimTest to 0 trim warnings #11407
Conversation
src/System.Windows.Forms/tests/IntegrationTests/TrimTest/TrimTest.csproj
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/tests/IntegrationTests/TrimTest/TrimTest.csproj
Show resolved
Hide resolved
src/System.Windows.Forms.Primitives/src/System/ComponentModel/TypeConverterHelper.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Controls/ToolStrips/OpacityConverter.cs
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListControl/ListControl.cs
Show resolved
Hide resolved
@@ -18,7 +18,7 @@ internal unsafe partial class ComposedDataObject | |||
{ | |||
[FeatureSwitchDefinition("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization")] | |||
#pragma warning disable IDE0075 // Simplify conditional expression - the simpler expression is hard to read | |||
private static bool EnableUnsafeBinaryFormatterInNativeObjectSerialization { get; } = AppContext.TryGetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", out bool isEnabled) ? isEnabled : true; | |||
internal static bool EnableUnsafeBinaryFormatterInNativeObjectSerialization { get; } = AppContext.TryGetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", out bool isEnabled) ? isEnabled : true; |
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.
Maybe we need a new helper class that would store "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization" switch value.
...System.Windows.Forms/src/System/Windows/Forms/Panels/TableLayoutPanel/TableLayoutSettings.cs
Show resolved
Hide resolved
...System.Windows.Forms/src/System/Windows/Forms/Panels/TableLayoutPanel/TableLayoutSettings.cs
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Drawing/Design/UITypeEditor.cs
Outdated
Show resolved
Hide resolved
{ | ||
throw new NotSupportedException(SR.BinaryFormatterNotSupported); | ||
} |
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.
It feels weird to have a feature switch to control use of binary formatter which is also under a feature switch.
We annotated BinaryFormatter as RequiresUnreferencedCode, but at the same time we introduced a feature switch to disable it and defaulted it to "disabled" when trimming. Should we actually drop the RUC on BinaryFormatter? The rationale is that there is no behavioral difference between trimmed and untrimmed app (both throw) and we'll probably generate trimming warnings from BinaryFormatter internals should someone dare to enable the BinaryFormatter feature switch.
It doesn't feel right that people need to create feature switch for a feature switch.
(In a normal PublishTrimmed app, new BinaryFormatter().Deserialize(stream)
below is going to throw a NotSupportedException anyway.)
Cc @eerhardt
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.
It feels weird to have a feature switch to control use of binary formatter which is also under a feature switch.
DataObject.ComposedDataObject.EnableUnsafeBinaryFormatterInNativeObjectSerialization
is just a wrapper over the existing System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization
feature switch. It isn't a new feature switch itself.
Should we actually drop the RUC on BinaryFormatter?
Not until BinaryFormatter's logic is actually removed - dotnet/runtime#98245. Once it always throws everywhere - then yes I think we can drop the annotations. No use in giving trim/AOT warnings when it will always throw.
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.
DataObject.ComposedDataObject.EnableUnsafeBinaryFormatterInNativeObjectSerialization
is just a wrapper over the existingSystem.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization
feature switch. It isn't a new feature switch itself.
There's a comment on this saying it's not the right property to use, so I assume it will change: https://github.com/dotnet/winforms/pull/11407/files#r1607641253
Should we actually drop the RUC on BinaryFormatter?
Not until BinaryFormatter's logic is actually removed - dotnet/runtime#98245. Once it always throws everywhere - then yes I think we can drop the annotations. No use in giving trim/AOT warnings when it will always throw.
The point I'm trying to make is that we generate warnings when there's a difference between F5 Debug time and publish time. In the case of BinaryFormatter in default trimmed configuration, there's no behavior difference. It always throws. The way we've done this elsewhere is that we don't generate a warning and structure things so that if someone changes the feature switch default, then they get a warning (and the warning would be somewhere in the implementation details of it).
But the more I'm thinking about it the more it feels like that's not the right approach either - if someone is a library developer like in this case, they won't know the API could throw until someone uses the library in a trimmed app (it will not be caught in unit tests, etc.). To give a similar example, someone could be using DI with open generics in a library (another spot where we use feature switches to erase differences and make trim unsafe API "safe"), never get any warnings from an analyzer, and only get bug reports from users later that it doesn't work.
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.
The feature switch, EnableUnsafeBinaryFormatterInNativeObjectSerialization
, using the binary formatter feature switch, is used to control trim warnings related to using BinaryFormatter
in WinForms trimming. The trimmer will generate a warning when set to true and will not generate a warning when set to false to work around the issue of the RUC on the BinaryFormatter
as mentioned above.
src/System.Windows.Forms.Primitives/src/System/ComponentModel/TypeConverterHelper.cs
Outdated
Show resolved
Hide resolved
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #11407 +/- ##
===================================================
+ Coverage 74.28601% 74.39904% +0.11302%
===================================================
Files 3026 3030 +4
Lines 627075 628039 +964
Branches 46755 46803 +48
===================================================
+ Hits 465829 467255 +1426
+ Misses 157900 157435 -465
- Partials 3346 3349 +3
Flags with carried forward coverage won't be shown. Click here to find out more. |
Use the new
ComponentModel
type registration APIs to make theTrimTest
project trim compatible.ComponentModel
APIs (non-trimmed applications will continue to use the legacyComponentModel
APIs)ComponentModel
since these are assumed to be supported by default byComponentModel
Microsoft Reviewers: Open in CodeFlow