[dotnet-sdk-8.0.100-preview.1.23107.4] Build with warnings after retargeting to .NET 8.0 for paint.net app
See original GitHub issueApplication Name: paint.net OS: Windows10 21h2 CPU: x64 App Source checking at: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1745300
Verify Scenarios: 1). Windows 10 21H2 AMD64 + dotnet-sdk-8.0.100-preview.1.23107.4 + app (default 7.0): Pass 2). Windows 10 21H2 AMD64 + dotnet-sdk-8.0.100-preview.1.23107.4 + app (retarget 8.0): Fail 3). Windows 10 21H2 AMD64 + dotnet-sdk-8.0.100-preview.1.23108.10-win-x64 + app (retarget 8.0): Fail
Repro steps: The machine has Visual Studio 2022 Preview installed because this app needs to build with Visual Studio and need to retarget .NET 8.0. 1.Copy app source paint.net to local machine. 2. Follow the steps in paintnet_Readme.txt file to configure the build environment. (File attached https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1745300) 3. Build the solution in Visual Studio Expected Result: Build successful. Actual Result: Build with following warnings (There are some other errors related to arm64 projects and build platform 143, please ignore them):
Minimal Repro sample: Minimal repro sample attached. paint.net demo.zip
Repro steps:
- Create a default .net 7.0 WinForm app.
- Add a new Class1.cs.
- Add following code in Class1.cs
internal class Class1: Class2
{
private void Method1(object? sender, EventArgs e)
{
Class2 c2 = (Class2)sender!;
Class3 c3 = (Class3)c2!.Tag;
Method2(c3);
}
public void Method2(Class3 c3)
{ }
}
internal class Class2: ToolStripMenuItem
{
protected override void OnParentChanged(ToolStrip oldParent, ToolStrip newParent)
{
base.OnParentChanged(oldParent, newParent);
}
}
internal class Class3
{ }
- Build the project, there is no warning in Error List windows of Visual Studio.
- Retarget the project to .net8.0-windows.
- Rebuild the project.
Expected Result: Build successful.
Actual Result:
Build with warning:
Warning CS8604 Possible null reference argument for parameter 'c3' in 'void Class1.Method2(Class3 c3)'.
Warning CS8765 Nullability of type of parameter 'oldParent' doesn't match overridden member (possibly because of nullability attributes).
Warning CS8765 Nullability of type of parameter 'newParent' doesn't match overridden member (possibly because of nullability attributes).
Warning CS8600 Converting null literal or possible null value to non-nullable type.
Findings:
-
When the custom class inherit from ToolStripMenuItem (come from System.Windows.Forms.dll), these warnings will appear in .NET 8.0, but not in .NET 7.0. The behavior changed in .NET 8.0.
-
According to the document: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings There are some ways recommended to resolve these warnings in document. We can add ? or ! to let the warnings disappear.
internal class Class1 : Class2
{
private void Method1(object? sender, EventArgs e)
{
Class2 c2 = (Class2)sender!;
Class3 c3 = (Class3)c2!.Tag!;
Method2(c3);
}
public void Method2(Class3? c3)
{ }
}
internal class Class2 : ToolStripMenuItem
{
protected override void OnParentChanged(ToolStrip? oldParent, ToolStrip? newParent)
{
base.OnParentChanged(oldParent, newParent);
}
}
Issue Analytics
- State:
- Created 7 months ago
- Comments:13 (11 by maintainers)
Top GitHub Comments
Saw this in .NET 7, glad to see more coming in .NET 8 😃
BinaryFormatter
will be my biggest blocker moving forward, that’s a lot of work to migrate away from. Nullability annotations are no sweat.This is currently by-design. There is no action item on WinForms at this point, the application has some work it could do while we’re going through the community led effort. (Thanks @elachlan!!) @rickbrew, wanted to get this on your radar early in .NET 8.