Handle cctor dependency on RVA static access (#86081)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Thu, 11 May 2023 10:01:37 +0000 (19:01 +0900)
committerGitHub <noreply@github.com>
Thu, 11 May 2023 10:01:37 +0000 (19:01 +0900)
Contributes to #68278.

src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs
src/tests/Regressions/coreclr/GitHub_68278/runtime68278.il [new file with mode: 0644]
src/tests/Regressions/coreclr/GitHub_68278/runtime68278.ilproj [new file with mode: 0644]

index 9a8b6c2..fc8a429 100644 (file)
@@ -1045,7 +1045,10 @@ namespace Internal.IL
                     // magic fields the compiler synthetized, the data blob might bring more dependencies
                     // and we need to scan those.
                     _dependencies.Add(_compilation.GetFieldRvaData(field), reason);
-                    // TODO: lazy cctor dependency
+                    // RVA static fields in generic types not implemented
+                    Debug.Assert(!field.OwningType.HasInstantiation);
+                    if (_compilation.HasLazyStaticConstructor(field.OwningType))
+                        _dependencies.Add(_factory.TypeNonGCStaticsSymbol((MetadataType)field.OwningType), "Cctor context");
                     return;
                 }
 
diff --git a/src/tests/Regressions/coreclr/GitHub_68278/runtime68278.il b/src/tests/Regressions/coreclr/GitHub_68278/runtime68278.il
new file mode 100644 (file)
index 0000000..3d30102
--- /dev/null
@@ -0,0 +1,30 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+.assembly runtime68278 { }
+
+.class interface abstract IHasCctor
+{
+    .data cil FortyTwo = int8(42)
+    .field public static int8 s_fortyTwo at FortyTwo
+    .method specialname rtspecialname static void .cctor()
+    {
+        ldc.i4 58
+        stsfld int8 IUnrelated::s_fiftyEight
+        ret
+    }
+}
+
+.class interface abstract IUnrelated
+{
+    .field public static int8 s_fiftyEight
+}
+
+.method static int32 main()
+{
+    .entrypoint
+    ldsfld int8 IHasCctor::s_fortyTwo
+    ldsfld int8 IUnrelated::s_fiftyEight
+    add
+    ret
+}
diff --git a/src/tests/Regressions/coreclr/GitHub_68278/runtime68278.ilproj b/src/tests/Regressions/coreclr/GitHub_68278/runtime68278.ilproj
new file mode 100644 (file)
index 0000000..bc34a24
--- /dev/null
@@ -0,0 +1,8 @@
+<Project Sdk="Microsoft.NET.Sdk.IL">
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="runtime68278.il" />
+  </ItemGroup>
+</Project>