Skip to content

Commit

Permalink
Avoid producing Requires* warnings when accessing constant fields (do…
Browse files Browse the repository at this point in the history
…tnet#84622)

Fields can only be annotated with `Requires*` attributes through their type (so RUC on type and so on). But that annotation is actually not guarding access to the field itself, but it's actually guarding access to static members, including static constructor.

Constant fields don't trigger static constructor. In fact at runtime constant fields are never accessed by code. The compiler is required to inline their values instead.

So IL based scanners will never produce the above warning, only analyzer would.

This change modifies the analyzer to stop producing warings for accessing constant fields.

Fixes dotnet#84433
  • Loading branch information
vitek-karas authored Apr 11, 2023
1 parent b014ab8 commit 981445c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
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

0 comments on commit 981445c

Please sign in to comment.