Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for warning behavior of Requires on type with a const field #84452

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ();
}
}
}
}