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 all 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 @@ -4207,11 +4207,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
44 changes: 44 additions & 0 deletions src/tests/JIT/Methodical/beforefieldinit/StaticFieldInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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;
using Xunit;

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
{
[Fact]
public static int TestEntryPoint () {
C.TestMethod(false);

return C.retVal;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<Compile Include="StaticFieldInit.cs" />
</ItemGroup>
</Project>