Skip to content

Commit cd3f12b

Browse files
authored
Emit BeforeFieldInit flag on interfaces (#69850)
Fixes #69413.
1 parent 400fd5a commit cd3f12b

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/Compilers/CSharp/Portable/Emitter/Model/NamedTypeSymbolAdapter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,6 @@ bool Cci.ITypeDefinition.IsBeforeFieldInit
507507
{
508508
case TypeKind.Enum:
509509
case TypeKind.Delegate:
510-
//C# interfaces don't have fields so the flag doesn't really matter, but Dev10 omits it
511-
case TypeKind.Interface:
512510
return false;
513511
}
514512

src/Compilers/CSharp/Test/Symbol/Symbols/DefaultInterfaceImplementationTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69148,5 +69148,76 @@ void validate(ModuleSymbol module)
6914869148
}
6914969149
}
6915069150
}
69151+
69152+
[Fact]
69153+
[WorkItem("https://github.com/dotnet/roslyn/issues/69413")]
69154+
public void IsBeforeFieldInit_01()
69155+
{
69156+
var source1 =
69157+
@"
69158+
public interface I1
69159+
{
69160+
}
69161+
69162+
public interface I2
69163+
{
69164+
public static int F2;
69165+
}
69166+
69167+
public interface I3
69168+
{
69169+
public static int F3 = 9;
69170+
}
69171+
69172+
public interface I4
69173+
{
69174+
public static int F4;
69175+
69176+
static I4()
69177+
{
69178+
F4 = 10;
69179+
}
69180+
}
69181+
69182+
public interface I5
69183+
{
69184+
static I5()
69185+
{
69186+
System.Console.WriteLine();
69187+
}
69188+
}
69189+
69190+
public interface I6
69191+
{
69192+
public static int F6 = 18;
69193+
69194+
static I6()
69195+
{
69196+
System.Console.WriteLine();
69197+
}
69198+
}
69199+
";
69200+
69201+
var compilation1 = CreateCompilation(source1, options: TestOptions.DebugDll,
69202+
targetFramework: TargetFramework.NetCoreApp,
69203+
parseOptions: TestOptions.RegularPreview);
69204+
69205+
CompileAndVerify(compilation1, symbolValidator: validate, verify: VerifyOnMonoOrCoreClr).VerifyDiagnostics();
69206+
69207+
void validate(ModuleSymbol module)
69208+
{
69209+
Assert.True(hasBeforeFieldInitFlag(module, "I1"));
69210+
Assert.True(hasBeforeFieldInitFlag(module, "I2"));
69211+
Assert.True(hasBeforeFieldInitFlag(module, "I3"));
69212+
Assert.False(hasBeforeFieldInitFlag(module, "I4"));
69213+
Assert.False(hasBeforeFieldInitFlag(module, "I5"));
69214+
Assert.False(hasBeforeFieldInitFlag(module, "I6"));
69215+
}
69216+
69217+
static bool hasBeforeFieldInitFlag(ModuleSymbol module, string name)
69218+
{
69219+
return (((PENamedTypeSymbol)module.GlobalNamespace.GetTypeMember(name)).Flags & System.Reflection.TypeAttributes.BeforeFieldInit) != 0;
69220+
}
69221+
}
6915169222
}
6915269223
}

0 commit comments

Comments
 (0)