Skip to content

JIT: (bug) inlining an instance method that never uses this drops the receiver null check #133713

Closed

Description

@EgorBo
using System;
using System.Runtime.CompilerServices;

class Data { public int V; }
class Node { public int Get(Data d) => d.V; }   // never touches 'this'

class P
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Foo(Data d, Node n) => n.Get(d);

    static void Main()
    {
        var d = new Data { V = 123 };
        try { Console.WriteLine("result=" + Foo(d, null)); }
        catch (NullReferenceException) { Console.WriteLine("NRE (correct)"); }
    }
}

Expected: NRE (correct) — callvirt on a null receiver must throw before the callee body runs.
Actual: result=123 — no exception; the null receiver check is dropped.

Checked corerun, -optimize+: prints result=123 with tiering on and with DOTNET_TieredCompilation=0; the same program compiled -optimize- prints NRE (correct).
Cause matches the report: the receiver is never fetched, so inlArgInfo[0].argTmpNum stays 0 (memset in impInlineInitVars), and impInlineIsThis (src\coreclr\jit\importer.cpp) then matches caller local V00 (d), making the field load of d.V look like a guaranteed this dereference, so fgInlinePrependStatements omits the null check.

Activity

  1. added this to the 12.0.0 milestone on Sep 11, 2026
  2. added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Sep 11, 2026
  3. dotnet-policy-service commented on Sep 11, 2026

    @dotnet-policy-service
    Contributor

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

  4. MihuBot commented on Sep 11, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  5. self-assigned this
    on Sep 11, 2026
  6. added a commit that references this issue on Sep 13, 2026
    3c4631e
  7. added a commit that references this issue on Sep 18, 2026
    4dc0ac6
  8. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  9. locked and limited conversation to collaborators on Oct 19, 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-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions