Skip to content

Commit

Permalink
Add a test for warning behavior of Requires on type with a const field (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
vitek-karas authored Apr 7, 2023
1 parent dcc0d25 commit 0760f77
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static void Main ()
KeepFieldOnAttribute ();
AttributeParametersAndProperties.Test ();
MembersOnClassWithRequires<int>.Test ();
ConstFieldsOnClassWithRequires.Test ();
}

[RequiresUnreferencedCode ("Message for --ClassWithRequires--")]
Expand Down Expand Up @@ -1229,5 +1230,60 @@ public static void Test (ClassWithRequires inst = null)
var j = new GenericAnnotatedWithWarningWithRequires<int> ();
}
}

class ConstFieldsOnClassWithRequires
{
[RequiresUnreferencedCode ("--ConstClassWithRequires--")]
[RequiresDynamicCode ("--ConstClassWithRequires--")]
class ConstClassWithRequires
{
public const string Message = "Message";
public const int Number = 42;

public static void Method () { }
}

// https://github.com/dotnet/runtime/issues/84433
[ExpectedWarning ("IL2026", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Message), ProducedBy = Tool.Analyzer)]
[ExpectedWarning ("IL3050", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Message), ProducedBy = Tool.Analyzer)]
[ExpectedWarning ("IL2026", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Number), ProducedBy = Tool.Analyzer)]
[ExpectedWarning ("IL3050", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Number), ProducedBy = Tool.Analyzer)]
[ExpectedWarning ("IL2026", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Method))]
[ExpectedWarning ("IL3050", "--ConstClassWithRequires--", nameof (ConstClassWithRequires.Method), ProducedBy = Tool.Analyzer | Tool.NativeAot)]
static void TestClassWithRequires ()
{
var a = ConstClassWithRequires.Message;
var b = ConstClassWithRequires.Number;

ConstClassWithRequires.Method ();
}

// https://github.com/dotnet/runtime/issues/84433
[ExpectedWarning ("IL2026", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
[ExpectedWarning ("IL3050", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
[RequiresUnreferencedCode (ConstClassWithRequiresUsingField.Message)]
[ExpectedWarning ("IL2026", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
[ExpectedWarning ("IL3050", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Message), ProducedBy = Tool.Analyzer)]
[RequiresDynamicCode (ConstClassWithRequiresUsingField.Message)]
class ConstClassWithRequiresUsingField
{
public const string Message = "--ConstClassWithRequiresUsingField--";

public static void Method () { }
}

[ExpectedWarning ("IL2026", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Method))]
[ExpectedWarning ("IL3050", "--ConstClassWithRequiresUsingField--", nameof (ConstClassWithRequiresUsingField.Method), ProducedBy = Tool.Analyzer | Tool.NativeAot)]
static void TestClassUsingFieldInAttribute ()
{
ConstClassWithRequiresUsingField.Method ();
}

public static void Test ()
{
TestClassWithRequires ();
TestClassUsingFieldInAttribute ();
}
}
}
}

0 comments on commit 0760f77

Please sign in to comment.