Skip to content

InvalidProgramException: Common Language Runtime detected an invalid program (Debug-only) #124425

Closed

Description

@Cheatoid

Description

As the title says.

Reproduction Steps

#nullable enable
Console.WriteLine("Begin.");
Console.WriteLine(Foo(v: null));
Console.WriteLine("Finished.");

static string? Foo(System.Numerics.Vector2? v = null)
{
  return Bar(nameof(v), v);
}

static string? Bar<T>(string name, T? value)
{
  if (value == null) { return "(null)"; }
  string? valueStr;
  if (typeof(T) == typeof(System.Numerics.Vector2?))
  {
    var v = (System.Numerics.Vector2?)(object?)value;
    valueStr = v.HasValue ? $"[{v}]" : "(null)";
  }
  else if (typeof(T) == typeof(System.Numerics.Vector2))
  {
    var v = (System.Numerics.Vector2)(object)value!;
    valueStr = $"[{v}]";
  }
  else
  {
    valueStr = value?.ToString() ?? "(null)";
  }
  return valueStr;
}

dotnet run Program.cs -c Debug

Begin.
Unhandled exception. System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at Program.<<Main>$>g__Bar|0_1[T](String name, T value)
   at Program.<<Main>$>g__Foo|0_0(Nullable`1 v) in Program.cs:line 8
   at Program.<Main>$(String[] args) in Program.cs:line 3

dotnet run Program.cs -c Release

Begin.
(null)
Finished.

🤔 😕 💻 💥

Expected behavior

I expected a valid CIL and no CLR crash. The program should produce the same output regardless of configuration, whether the program is compiled with <Optimize>false</Optimize> (which is the case for Debug configuration). In other words, it should work and print out:

Begin.
(null)
Finished.

Actual behavior

As the title says.
Common Language Runtime detected an invalid program. But this only happens when program is compiled in Debug configuration (<Optimize>false</Optimize> a.k.a. /p:Optimize=false).

Regression?

No response

Known Workarounds

My current workaround is to use pattern matching:

#nullable enable
Console.WriteLine("Begin.");
Console.WriteLine(Foo(v: null));
Console.WriteLine("Finished.");

static string? Foo(System.Numerics.Vector2? v = null)
{
  return Bar(nameof(v), v);
}

static string? Bar<T>(string name, T? value)
{
  if (value == null) { return "(null)"; }
  string? valueStr;
  if (value is System.Numerics.Vector2 vector2)
  {
    valueStr = $"[{vector2}]";
  }
  else
  {
    valueStr = value?.ToString() ?? "(null)";
  }
  return valueStr;
}

Configuration

dotnet --info

.NET SDK:
 Version:           10.0.103
 Commit:            c2435c3e0f
 Workload version:  10.0.103
 MSBuild version:   18.0.11+c2435c3e0

(On Windows 10 x64, but I think that's irrelevant.)

Other information

No response

Activity

  1. added
    untriaged
    on Feb 14, 2026
  2. dotnet-policy-service commented on Feb 14, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-numerics
    See info in area-owners.md if you want to be subscribed.

  3. EgorBo commented on Feb 14, 2026

    @EgorBo
    Member

    Smells like a JIT issue. smaller repro:

    using System.Numerics;
    
    Vector2? v = null;
    Bar(nameof(v), v);
    
    static void Bar<T>(string name, T? value)
    {
        var v = (Vector2)(object)value!;
    }
  4. added
    area-CodeGen-coreclr
    and removed
    untriaged
    on Feb 14, 2026
  5. self-assigned this
    on Feb 14, 2026
  6. added this to the 11.0.0 milestone on Feb 14, 2026
  7. dotnet-policy-service commented on Feb 14, 2026

    @dotnet-policy-service
    Contributor

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

  8. reopened this on Feb 16, 2026
  9. EgorBo commented on Feb 16, 2026

    @EgorBo
    Member

    Fixed by #124432, thanks for repro! Will keep this issue open for backport to 10.0

  10. Cheatoid commented on Feb 18, 2026

    @Cheatoid
    Author

    Amazing! I was wondering if it could be backported.

  11. tannergooding commented on Jul 5, 2026

    @tannergooding
    Member

    /backport to release/10.0

  12. tannergooding commented on Jul 16, 2026

    @tannergooding
    Member

    It would've helped if I had run the command on the PR -.-

  13. JulieLeeMSFT commented on Jul 21, 2026

    @JulieLeeMSFT
    Member

    @Cheatoid, we backported the fix to 10.0.12 which will be released in September 8th.
    Please report back if it does not fix the issue.
    I am closing this now.

  14. locked and limited conversation to collaborators on Aug 23, 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

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions