Skip to content

Type initializer call ordering w.r.t. static field stores does not look right #72354

@SingleAccretion

Description

@SingleAccretion

Reproduction:

using System;
using System.Runtime.CompilerServices;

Problem();

[MethodImpl(MethodImplOptions.NoInlining)]
static void Problem()
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Call() => throw new Exception("Thrown from 'Call'");

    var a = Call();
    ClassWithCctor.StaticField = a;
}

class ClassWithCctor
{
    public static int StaticField;
    static ClassWithCctor() => throw new Exception("Thrown from 'ClassWithCctor'");
}

Compile and run.

Expected result: the exception from Call is thrown.

Actual result: the type initializer throws instead:

Unhandled exception. System.TypeInitializationException: The type initializer for 'ClassWithCctor' threw an exception.
 ---> System.Exception: Thrown from 'ClassWithCctor'

Cause - the insertion point of the helper here:

if (helperNode != nullptr)
{
op1 = gtNewOperNode(GT_COMMA, op1->TypeGet(), helperNode, op1);
}

category:correctness
theme:basic-cq

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIbug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions