Skip to content

Static fields in interfaces should cause the compiler to emit beforefieldinit on the interface #69413

@siegfriedpammer

Description

@siegfriedpammer

Version Used:
Microsoft Visual Studio Community 2022
Version 17.6.4
VisualStudio.17.Release/17.6.4+33815.320
Microsoft .NET Framework
Version 4.8.09037

Installed Version: Community

C# Tools 4.6.0-3.23259.8+c3cc1d0ceeab1a65da0217e403851a1e8a30086a
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Steps to Reproduce:

  1. the following C# code utilizing static interface members
internal interface I
{
	static int f = 42;
}

is compiled to the following IL (using a simple test project targeting C# 11 and net7.0):

.class interface private auto ansi abstract I
{
	// Fields
	.field public static int32 f

	// Methods
	.method private hidebysig specialname rtspecialname static 
		void .cctor () cil managed 
	{
		.maxstack 8

		IL_0000: ldc.i4.s 42
		IL_0002: stsfld int32 I::f
		IL_0007: ret
	} // end of method I::.cctor

} // end of class I

Expected Behavior:

I would expect the interface to have the beforefieldinit attribute.

Actual Behavior:

The beforefieldinit attribute is missing, this results in unnecessary runtime overhead see also https://godbolt.org/z/jxbxGKrv1

Metadata

Metadata

Assignees

Labels

4 - In ReviewA fix for the issue is submitted for review.Area-CompilersBugCode Gen QualityRoom for improvement in the quality of the compiler's generated code

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions