Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mono] Perform the class init at runtime when static fields of BEFORE_FIELD_INIT type are accessed #86787

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/mono/mono/mini/method-to-ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -4196,11 +4196,6 @@ mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoCla
return FALSE;
}

if (mono_class_is_before_field_init (klass)) {
if (cfg->method == method)
return FALSE;
}

if (!mono_class_needs_cctor_run (klass, method))
return FALSE;

Expand Down
42 changes: 42 additions & 0 deletions src/tests/JIT/Methodical/beforefieldinit/StaticFieldInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;

static class C
{
public static int retVal = 100;
[MethodImpl(MethodImplOptions.NoInlining)]
public static int TestMethod(bool cond)
{
if (cond)
{
// never invoked in this program
return ClassB.X;
}
// always taken
return 0;
}
}

public class ClassB
{
public static readonly int X = GetX();

static int GetX()
{
Console.WriteLine("GetX call!..."); // not expected to be invoked in this program
C.retVal = 42;
return 42;
}
}

public class StaticFieldInit
{
public static int Main () {
C.TestMethod(false);

return C.retVal;
}
}
13 changes: 13 additions & 0 deletions src/tests/JIT/Methodical/beforefieldinit/StaticFieldInit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RequiresProcessIsolation>true</RequiresProcessIsolation>
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>
<PropertyGroup>
<DebugType>PdbOnly</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
<ItemGroup>
<Compile Include="StaticFieldInit.cs" />
</ItemGroup>
</Project>