Skip to content

Commit

Permalink
Do not keep cctor for instance fields (dotnet#96656)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalStrehovsky authored and tmds committed Jan 23, 2024
1 parent 6774f9e commit 80b366c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ void MarkField (FieldDefinition field, in DependencyInfo reason, in MessageOrigi
}

var parent = field.DeclaringType;
if (!Annotations.HasPreservedStaticCtor (parent)) {
if (field.IsStatic && !Annotations.HasPreservedStaticCtor (parent)) {
var cctorReason = reason.Kind switch {
// Report an edge directly from the method accessing the field to the static ctor it triggers
DependencyKind.FieldAccess => new DependencyInfo (DependencyKind.TriggersCctorThroughFieldAccess, reason.Source),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class UnusedStaticFieldInitializer
public static void Main ()
{
C.Foo ();

new C2 (123);
}

static class C
Expand All @@ -18,5 +20,16 @@ public static void Foo ()
{
}
}

class C2
{
public static object o = new object ();

[Kept]
public int Field;

[Kept]
public C2 (int val) => Field = val;
}
}
}

0 comments on commit 80b366c

Please sign in to comment.