Skip to content

Commit c447338

Browse files
authored
Fix RunClassConstructor trim warning for Nullable<T> (#106975)
The trimming tools (ILLink, native AOT, and the trim analyzer) report warnings when RunClassConstructor is called for nullable value types. This is not problematic for trimming and should not warn.
1 parent ed21115 commit c447338

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCases/TestSuites.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public void Reflection (string t)
7474
case "ParametersUsedViaReflection":
7575
case "UnsafeAccessor":
7676
case "TypeUsedViaReflection":
77+
case "RunClassConstructor":
7778
Run (t);
7879
break;
7980
default:

src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers
293293
} else if (typeHandleValue is RuntimeTypeHandleForValueWithDynamicallyAccessedMembers damAnnotatedHandle
294294
&& (damAnnotatedHandle.UnderlyingTypeValue.DynamicallyAccessedMemberTypes & DynamicallyAccessedMemberTypes.NonPublicConstructors) != 0) {
295295
// No action needed, NonPublicConstructors keeps the static constructor on the type
296+
} else if (typeHandleValue is RuntimeTypeHandleForNullableSystemTypeValue) {
297+
// No action needed, RunClassConstructor does not run the class constructor of the underlying type of Nullable<T>
296298
} else {
297299
_diagnosticContext.AddDiagnostic (DiagnosticId.UnrecognizedTypeInRuntimeHelpersRunClassConstructor, calledMethod.GetDisplayName ());
298300
}

src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/ReflectionTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ public Task PropertiesUsedViaReflection ()
166166
return RunTest ();
167167
}
168168

169+
[Fact]
170+
public Task RunClassConstructor ()
171+
{
172+
return RunTest ();
173+
}
174+
169175
[Fact]
170176
public Task RuntimeReflectionExtensionsCalls ()
171177
{

src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/generated/ILLink.RoslynAnalyzer.Tests.Generator/ILLink.RoslynAnalyzer.Tests.TestCaseGenerator/ReflectionTests.g.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ public Task ParametersUsedViaReflection ()
4949
return RunTest (allowMissingWarnings: true);
5050
}
5151

52-
[Fact]
53-
public Task RunClassConstructorUsedViaReflection ()
54-
{
55-
return RunTest (allowMissingWarnings: true);
56-
}
57-
5852
[Fact]
5953
public Task UnderlyingSystemType ()
6054
{
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Mono.Linker.Tests.Cases.Reflection
88
{
99
[SetupCSharpCompilerToUse ("csc")]
1010
[ExpectedNoWarnings]
11-
public class RunClassConstructorUsedViaReflection
11+
public class RunClassConstructor
1212
{
1313
public static void Main ()
1414
{
@@ -21,6 +21,7 @@ public static void Main ()
2121
TestPublicConstructorDataFlowType ();
2222
TestIfElseUsingRuntimeTypeHandle (1);
2323
TestIfElseUsingType (1);
24+
TestNullableValueType ();
2425
}
2526

2627
[Kept]
@@ -125,6 +126,12 @@ static void TestIfElseUsingType (int i)
125126
RuntimeHelpers.RunClassConstructor (myType.TypeHandle);
126127
}
127128

129+
[Kept]
130+
static void TestNullableValueType ()
131+
{
132+
RuntimeHelpers.RunClassConstructor (typeof (int?).TypeHandle);
133+
}
134+
128135
[Kept]
129136
[KeptMember (".cctor()")]
130137
class OnlyUsedViaReflection

0 commit comments

Comments
 (0)