You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: (bug) Enum.Equals folding throws NullReferenceException instead of returning false for a null argument #133817
Compiler::gtFoldExprCall folds NI_System_Enum_Equals into a direct comparison of the two boxed payloads without checking that the argument is non-null, so a null argument is unconditionally dereferenced. ValueType.Equals(object) must return false for null, never throw.
Minimal Repro
usingSystem;usingSystem.Runtime.CompilerServices;enumE:int{A=0,B=1}classProgram{[MethodImpl(MethodImplOptions.NoInlining)]staticboolTest(Ex,E?y){objecta=x;objectb=y;// null when y is null, but the JIT still sees exact class Ereturna.Equals(b);}staticvoidMain()=>Console.WriteLine(Test(E.A,null));}
Expected
False
Actual
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Program.Test(E x, Nullable`1 y)
The generated code has the null path (xor rax, rax) flowing straight into cmp ecx, dword ptr [rax+0x08].
Notes
The fold requires only an exact class handle for both operands and explicitly discards the isNonNull result (bool isNonNull; // Unused here.). Boxing a Nullable<TEnum> yields either a boxed TEnum (exact handle) or null, so exactness does not imply non-null. FullOpts only; MinOpts/tier-0 are correct. Does not repro on .NET 10 — regression from d3f4519 ("Intrinsify Enum.Equals to avoid boxing", #122779).
changed the title [-]JIT (bug): `Enum.Equals` folding throws `NullReferenceException` instead of returning `false` for a `null` argument[/-][+]JIT: (bug) `Enum.Equals` folding throws `NullReferenceException` instead of returning `false` for a `null` argument[/+]on Sep 16, 2026
Activity
dotnet-policy-service commentedon Sep 13, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
[-]JIT (bug): `Enum.Equals` folding throws `NullReferenceException` instead of returning `false` for a `null` argument[/-][+]JIT: (bug) `Enum.Equals` folding throws `NullReferenceException` instead of returning `false` for a `null` argument[/+]EgorBo commentedon Sep 18, 2026
Affected SDK versions:
Fix Enum.Equals folding for nullable boxed arguments (#133826)