Skip to content
Draft
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 @@ -161,6 +161,11 @@ public override MultiValue VisitInstanceReference(IInstanceReferenceOperation in

public override MultiValue VisitFieldReference(IFieldReferenceOperation fieldRef, StateValue state)
{
// Visit the instance to ensure that method calls or other operations
// used as the instance are properly analyzed for diagnostics.
// For example: someMethod().Field should analyze someMethod().
Visit(fieldRef.Instance, state);

var field = fieldRef.Field;
switch (field.Name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static void Main()
{
GetInterface_Name.Test();
GetInterface_Name_IgnoreCase.Test();
GetInterfaceMap_DataFlow.Test();
}

class GetInterface_Name
Expand Down Expand Up @@ -184,6 +185,50 @@ public static void Test()
}
}

class GetInterfaceMap_DataFlow
{
[ExpectedWarning("IL2072", nameof(Type.GetInterfaceMap))]
static void TestDirectCall()
{
// This should warn: @interface doesn't have the required annotations
var @interface = typeof(TestType).GetInterfaces()[0];
_ = typeof(TestType).GetInterfaceMap(@interface);
}

[ExpectedWarning("IL2072", nameof(Type.GetInterfaceMap))]
static void TestFieldAccess()
{
// This should also warn: @interface doesn't have the required annotations
// even though we're accessing a field on the return value.
// Regression test for https://github.com/dotnet/runtime/issues/117849
var @interface = typeof(TestType).GetInterfaces()[0];
_ = typeof(TestType).GetInterfaceMap(@interface).TargetMethods;
}

[ExpectedWarning("IL2072", nameof(Type.GetInterfaceMap))]
static void TestMultipleFieldAccess()
{
// Should warn even with multiple field/property accesses
var @interface = typeof(TestType).GetInterfaces()[0];
_ = typeof(TestType).GetInterfaceMap(@interface).TargetMethods.Length;
}

static void TestWithAnnotation([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType)
{
// This should not warn: interfaceType has the required annotations
_ = typeof(TestType).GetInterfaceMap(interfaceType);
_ = typeof(TestType).GetInterfaceMap(interfaceType).TargetMethods;
}

public static void Test()
{
TestDirectCall();
TestFieldAccess();
TestMultipleFieldAccess();
TestWithAnnotation(null);
}
}

interface ITestInterface
{
}
Expand Down
Loading