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

Avoid producing Requires* warnings when accessing constant fields #84622

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
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 @@ -197,6 +197,13 @@ void CheckCalledMember (
if (operationContext.Operation.Parent is IOperation operation && operation.Kind == OperationKind.NameOf)
return;

// Do not emit any diagnostics for constant fields - they can only have Requires attributes applied to them
// via the type, and in that case the attribute is guarding the access to the static ctor.
// But constant fields are never accessed at runtime, and thus they don't cause static ctor to run.
// Constant fields are always inlined by the compiler (required by the ECMA spec).
if (member is IFieldSymbol field && field.HasConstantValue)
return;

ISymbol containingSymbol = FindContainingSymbol (operationContext, AnalyzerDiagnosticTargets);

// Do not emit any diagnostic if caller is annotated with the attribute too.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,11 +1243,6 @@ class ConstClassWithRequires
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 ()
Expand All @@ -1258,12 +1253,7 @@ static void TestClassWithRequires ()
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
{
Expand Down