Skip to content

.NET 11.0.0: Enum.Equals with a null Nullable<TEnum> argument throws NullReferenceException (fix #133826 not in release/11.0) #137270

Description

@thomhurst

Description

On .NET 11.0.0, enumValue.Equals(nullableEnum) throws NullReferenceException when the nullable is null, in fully optimized code. It should return false, and does on .NET 10.

This is #133817. It was fixed in main by #133826 (milestone 12.0.0), but the fix is not in release/11.0, so the 11.0.0 GA runtime ships with the bug. gtFoldExprCall on release/11.0 still discards the non-null result for the argument (bool isNonNull; // Unused here.).

Could #133826 be backported to release/11.0 for servicing? #134898 (shared-generic enum folding, #134890) touches the same check and is also missing there.

Reproduction Steps

using System.Runtime.CompilerServices;

Console.WriteLine(Test(E.A, null));

[MethodImpl(MethodImplOptions.NoInlining)]
static bool Test(E x, E? y) => x.Equals(y);

enum E { A, B }

Either of:

  • dotnet build -c Release, then DOTNET_TieredCompilation=0 dotnet bin/Release/net11.0/repro.dll
  • dotnet publish -c Release -r <rid> -p:PublishAot=true, then run the native executable

Expected behavior

False

Actual behavior

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Program.<<Main>$>g__Test|0_0(E x, Nullable`1 y)

Tier-0 code and NativeAOT Debug builds return False.

Regression?

Yes. .NET 10 returns False with the same project, both NativeAOT and JIT with DOTNET_TieredCompilation=0. #133817 attributes it to #122779.

Known Workarounds

Avoid passing a Nullable<TEnum> to Enum.Equals(object); compare with == or object.Equals(a, b) instead. That only helps in code you own.

Configuration

  • .NET SDK 11.0.100, runtime 11.0.0 (11.0.26.53018)
  • macOS arm64 (JIT and NativeAOT); also seen on linux-x64 NativeAOT

Other information

A real-world hit: Testcontainers.Kafka 4.16.0 KafkaBuilder.Build() evaluates v.Vendor.Equals(DockerResourceConfiguration.Vendor) with a null KafkaVendor?, so every Kafka container build throws under .NET 11 NativeAOT:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Testcontainers.Kafka.KafkaBuilder.<>c__DisplayClass22_0.<Build>b__0(IKafkaVendorConfiguration v)
   at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1, Func`2, Boolean&)
   at Testcontainers.Kafka.KafkaBuilder.Build()

The same code can fail under the JIT once the method is optimized.

Found while moving Dekaf's NativeAOT tests to .NET 11.

Activity

  1. added
    area-CodeGen-coreclr
    and removed on Nov 10, 2026
  2. added
    untriaged
    on Nov 10, 2026
  3. dotnet-policy-service commented on Nov 10, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
    See info in area-owners.md if you want to be subscribed.

  4. dotnet-policy-service commented on Nov 10, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  5. JOnysko commented on Nov 11, 2026

    @JOnysko

    Adding reach data in case it helps with the servicing decision: this also breaks PDF generation with iText and one path in the Open XML SDK, so it is not limited to Testcontainers.

    Same shape as the issue (box Nullable<TEnum> → constrained. TEnum → callvirt Object::Equals(Object) with a null nullable), hit inside widely used libraries. Standalone console harness, Release, Windows x64, same binaries on both runtimes:

    Library call Package 10.0.15 11.0.0, DOTNET_TieredCompilation=0 11.0.0, default tiering
    Paragraph.SetFixedLeading / SetMultipliedLeading itext 9.6.0 0 throws NRE on every call, from call 1 NRE from about call 10,000
    HtmlConverter.ConvertToPdf with a <ul> (top frame UlOlTagCssApplier.Apply) itext.pdfhtml 6.3.2 0 throws NRE on every call, from call 1 NRE from about call 325
    Laying out a Document with a Paragraph and a Table (top frame LineRenderer.Layout) itext 9.6.0 0 throws NRE on every call, from call 1 NRE from about call 1,000
    Canvas.ShowTextAligned(Paragraph, ...) on an existing PDF itext 9.6.0 0 throws NRE on every call, from call 1 NRE from about call 1,000
    SpreadsheetDocument.ChangeDocumentType on a freshly opened package (DocumentType not read yet) DocumentFormat.OpenXml 3.1.1 and 3.5.1 0 throws NRE on every call, from call 1 NRE from about call 7,000
    System.NullReferenceException: Object reference not set to an instance of an object.
       at iText.Layout.Element.Paragraph.SetFixedLeading(Single leading)
    

    What makes this hard to live with in production: with default tiering the calls succeed until the method is promoted to tier 1, so a short test run passes and a long-running server starts failing later. An IL scan of itext 9.6.0 + itext.pdfhtml 6.3.2 finds 32 call sites with this shape, so we cannot work around it call by call, and we are holding our .NET 11 upgrade for it.

    A backport of #133826 to release/11.0 would unblock us. Happy to run the harness against a candidate build if that is useful.

  6. AndyAyersMS commented on Nov 11, 2026

    @AndyAyersMS
    Member

    We have a second report of this in #137382.

    I have started a backport.

  7. self-assigned this
    on Nov 11, 2026
  8. removed
    untriaged
    on Nov 11, 2026
  9. added this to the 11.0.0 milestone on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions