Skip to content

Commit 0760f77

Browse files
authored
Add a test for warning behavior of Requires on type with a const field (#84452)
The IL tools (illink, ilc) don't see const field references in code, since the compiler inlines the values. But analyzer does see them. If the const field is on a type with Requires attribute, that access is reported as a warning, which is only produced by the analyzer. This adds tests for these cases, no product changes. Tests for #84433
1 parent dcc0d25 commit 0760f77

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static void Main ()
3939
KeepFieldOnAttribute ();
4040
AttributeParametersAndProperties.Test ();
4141
MembersOnClassWithRequires<int>.Test ();
42+
ConstFieldsOnClassWithRequires.Test ();
4243
}
4344

4445
[RequiresUnreferencedCode ("Message for --ClassWithRequires--")]
@@ -1229,5 +1230,60 @@ public static void Test (ClassWithRequires inst = null)
12291230
var j = new GenericAnnotatedWithWarningWithRequires<int> ();
12301231
}
12311232
}
1233+
1234+
class ConstFieldsOnClassWithRequires
1235+
{
1236+
[RequiresUnreferencedCode ("--ConstClassWithRequires--")]
1237+
[RequiresDynamicCode ("--ConstClassWithRequires--")]
1238+
class ConstClassWithRequires
1239+
{
1240+
public const string Message = "Message";
1241+
public const int Number = 42;
1242+
1243+
public static void Method () { }
1244+
}
1245+
1246+
// https://github.com/dotnet/runtime/issues/84433
1247+
[ExpectedWarning ("IL2026", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Message), ProducedBy = Tool.Analyzer)]
1248+
[ExpectedWarning ("IL3050", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Message), ProducedBy = Tool.Analyzer)]
1249+
[ExpectedWarning ("IL2026", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Number), ProducedBy = Tool.Analyzer)]
1250+
[ExpectedWarning ("IL3050", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Number), ProducedBy = Tool.Analyzer)]
1251+
[ExpectedWarning ("IL2026", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Method))]
1252+
[ExpectedWarning ("IL3050", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Method), ProducedBy = Tool.Analyzer | Tool.NativeAot)]
1253+
static void TestClassWithRequires ()
1254+
{
1255+
var a = ConstClassWithRequires.Message;
1256+
var b = ConstClassWithRequires.Number;
1257+
1258+
ConstClassWithRequires.Method ();
1259+
}
1260+
1261+
// https://github.com/dotnet/runtime/issues/84433
1262+
[ExpectedWarning ("IL2026", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
1263+
[ExpectedWarning ("IL3050", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
1264+
[RequiresUnreferencedCode (ConstClassWithRequiresUsingField.Message)]
1265+
[ExpectedWarning ("IL2026", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
1266+
[ExpectedWarning ("IL3050", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
1267+
[RequiresDynamicCode (ConstClassWithRequiresUsingField.Message)]
1268+
class ConstClassWithRequiresUsingField
1269+
{
1270+
public const string Message = "--ConstClassWithRequiresUsingField--";
1271+
1272+
public static void Method () { }
1273+
}
1274+
1275+
[ExpectedWarning ("IL2026", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Method))]
1276+
[ExpectedWarning ("IL3050", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Method), ProducedBy = Tool.Analyzer | Tool.NativeAot)]
1277+
static void TestClassUsingFieldInAttribute ()
1278+
{
1279+
ConstClassWithRequiresUsingField.Method ();
1280+
}
1281+
1282+
public static void Test ()
1283+
{
1284+
TestClassWithRequires ();
1285+
TestClassUsingFieldInAttribute ();
1286+
}
1287+
}
12321288
}
12331289
}

0 commit comments

Comments
 (0)