Skip to content

Commit

Permalink
Add condition to bypass nameof usage since it will not invoke the mem…
Browse files Browse the repository at this point in the history
…ber and instead will be translated to load an string (#2649)

Add tests
Fix ExpectedWarnings from disabled intrinsics that were producing warnings from accessing fields in a nameof call
  • Loading branch information
tlakollo authored Feb 24, 2022
1 parent fb64018 commit 6a82f4f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ void CheckCalledMember (
ISymbol member,
ImmutableArray<ISymbol> incompatibleMembers)
{
// Do not emit diagnostics if the operation is nameof()
if (operationContext.Operation.Parent is IOperation operation && operation.Kind == OperationKind.NameOf)
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 @@ -30,6 +30,7 @@ public static void Main ()
TestRequiresOnPropertyGetterAndSetter ();
TestThatTrailingPeriodIsAddedToMessage ();
TestThatTrailingPeriodIsNotDuplicatedInWarningMessage ();
TestRequiresFromNameOf ();
OnEventMethod.Test ();
RequiresOnGenerics.Test ();
}
Expand Down Expand Up @@ -137,6 +138,11 @@ static void TestThatTrailingPeriodIsNotDuplicatedInWarningMessage ()
WarningMessageEndsWithPeriod ();
}

static void TestRequiresFromNameOf ()
{
_ = nameof (BasicRequires.RequiresWithMessageOnly);
}

class OnEventMethod
{
[ExpectedWarning ("IL2026", "--EventToTestRemove.remove--", ProducedBy = ProducedBy.Trimmer)]
Expand Down
17 changes: 12 additions & 5 deletions test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static void Main ()
TestStaticMethodOnRequiresTypeSuppressedByRequiresOnMethod ();
TestStaticConstructorCalls ();
TestOtherMemberTypesWithRequires ();
TestNameOfDoesntWarn ();
ReflectionAccessOnMethod.Test ();
ReflectionAccessOnCtor.Test ();
ReflectionAccessOnField.Test ();
Expand Down Expand Up @@ -426,6 +427,14 @@ static void TestOtherMemberTypesWithRequires ()
MemberTypesWithRequires.Event -= null;
}

static void TestNameOfDoesntWarn ()
{
_ = nameof (ClassWithRequires.StaticMethod);
_ = nameof (MemberTypesWithRequires.field);
_ = nameof (MemberTypesWithRequires.Property);
_ = nameof (MemberTypesWithRequires.Event);
}

class ReflectionAccessOnMethod
{
// Analyzer still dont understand RUC on type
Expand Down Expand Up @@ -591,11 +600,9 @@ static void TestDAMAccess ()
typeof (DerivedWithRequires).RequiresPublicFields ();
}

[ExpectedWarning ("IL2026", "WithRequires.StaticField")]
// Analyzer does not recognize the binding flags
[ExpectedWarning ("IL2026", "WithRequires.StaticField", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2026", "WithRequires.PrivateStaticField", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2026", "DerivedWithRequires.DerivedStaticField")]
[ExpectedWarning ("IL2026", "DerivedWithRequires.DerivedStaticField", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL2026", "DerivedWithRequires.DerivedStaticField", ProducedBy = ProducedBy.Trimmer)]
static void TestDirectReflectionAccess ()
{
typeof (WithRequires).GetField (nameof (WithRequires.StaticField));
Expand All @@ -606,7 +613,7 @@ static void TestDirectReflectionAccess ()
typeof (DerivedWithRequires).GetField (nameof (DerivedWithRequires.DerivedStaticField));
}

[ExpectedWarning ("IL2026", "WithRequires.StaticField")]
[ExpectedWarning ("IL2026", "WithRequires.StaticField", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2026", "WithRequires.StaticField", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2026", "WithRequires.StaticField", ProducedBy = ProducedBy.Trimmer)]
[DynamicDependency (nameof (WithRequires.StaticField), typeof (WithRequires))]
Expand Down

0 comments on commit 6a82f4f

Please sign in to comment.