- Notifications
You must be signed in to change notification settings - Fork215
Closed
Description
Here's a small project that reproduces the issue:
<ProjectSdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net5.0</TargetFramework> </PropertyGroup> <PropertyGroup> <Optimize>true</Optimize> <RootAllApplicationAssemblies>false</RootAllApplicationAssemblies> <IlcInvariantGlobalization>true</IlcInvariantGlobalization> <IlcGenerateCompleteTypeMetadata>false</IlcGenerateCompleteTypeMetadata> <IlcDisableReflection>true</IlcDisableReflection> <IlcOptimizationPreference>Speed</IlcOptimizationPreference> </PropertyGroup> <ItemGroup> <PackageReferenceInclude="Microsoft.DotNet.ILCompiler"Version="6.0.0-*" /> </ItemGroup></Project>
usingSystem;namespaceBugNativeAOT{internalclassProgram{privatereadonlystructSize{publicintWidth{get;}publicintHeight{get;}publiclongArea=>Width*(long)Height;publicSize(intwidth,intheight){Width=width;Height=height;}}privatesealedclassBug{publicBug(Sizes){S=s;}publicSizeS{get;}}privatestaticvoidMain(string[]args){vart=newBug(newSize(0,0));if(t.S.Area==0L){Console.WriteLine("Expected: == operator");}else{Console.WriteLine("UNEXPECTED: == operator");Console.WriteLine($"Area value ={t.S.Area}");}if(0L.Equals(t.S.Area)){Console.WriteLine("Expected: long.Equals");}else{Console.WriteLine("UNEXPECTED: == long.Equals");Console.WriteLine($"Area value ={t.S.Area}");}if(((object)t.S.Area).Equals(0L)){Console.WriteLine("Expected: object.Equals boxed");}else{Console.WriteLine("UNEXPECTED: == object.Equals boxed");Console.WriteLine($"Area value ={t.S.Area}");}}}}
The output is
UNEXPECTED: == operatorArea value = 0UNEXPECTED: == long.EqualsArea value = 0Expected: object.Equals boxed
I haven't tested other types and I'm not sure the example is exactly minimal, but it reproduces the bug I found when using something similar.
NOTE: if I disable the optimizations in the .csproj, the program outputs the expected results.