Skip to content

Commit

Permalink
Emit BeforeFieldInit flag on interfaces (#69850)
Browse files Browse the repository at this point in the history
Fixes #69413.
  • Loading branch information
AlekseyTs authored Sep 8, 2023
1 parent 400fd5a commit cd3f12b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,6 @@ bool Cci.ITypeDefinition.IsBeforeFieldInit
{
case TypeKind.Enum:
case TypeKind.Delegate:
//C# interfaces don't have fields so the flag doesn't really matter, but Dev10 omits it
case TypeKind.Interface:
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69148,5 +69148,76 @@ void validate(ModuleSymbol module)
}
}
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/69413")]
public void IsBeforeFieldInit_01()
{
var source1 =
@"
public interface I1
{
}

public interface I2
{
public static int F2;
}

public interface I3
{
public static int F3 = 9;
}

public interface I4
{
public static int F4;

static I4()
{
F4 = 10;
}
}

public interface I5
{
static I5()
{
System.Console.WriteLine();
}
}

public interface I6
{
public static int F6 = 18;

static I6()
{
System.Console.WriteLine();
}
}
";

var compilation1 = CreateCompilation(source1, options: TestOptions.DebugDll,
targetFramework: TargetFramework.NetCoreApp,
parseOptions: TestOptions.RegularPreview);

CompileAndVerify(compilation1, symbolValidator: validate, verify: VerifyOnMonoOrCoreClr).VerifyDiagnostics();

void validate(ModuleSymbol module)
{
Assert.True(hasBeforeFieldInitFlag(module, "I1"));
Assert.True(hasBeforeFieldInitFlag(module, "I2"));
Assert.True(hasBeforeFieldInitFlag(module, "I3"));
Assert.False(hasBeforeFieldInitFlag(module, "I4"));
Assert.False(hasBeforeFieldInitFlag(module, "I5"));
Assert.False(hasBeforeFieldInitFlag(module, "I6"));
}

static bool hasBeforeFieldInitFlag(ModuleSymbol module, string name)
{
return (((PENamedTypeSymbol)module.GlobalNamespace.GetTypeMember(name)).Flags & System.Reflection.TypeAttributes.BeforeFieldInit) != 0;
}
}
}
}

0 comments on commit cd3f12b

Please sign in to comment.