Skip to content
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
15 changes: 13 additions & 2 deletions src/ILLink.RoslynAnalyzer/DataFlow/LocalDataFlowVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ public override TValue VisitSimpleAssignment (ISimpleAssignmentOperation operati
// The remaining cases don't have a dataflow value that represents LValues, so we need
// to handle the LHS specially.
case IPropertyReferenceOperation propertyRef: {
var setMethod = propertyRef.Property.SetMethod;
IPropertySymbol? property = propertyRef.Property;
IMethodSymbol? setMethod;
while ((setMethod = property.SetMethod) == null) {
if ((property = property.OverriddenProperty) == null)
break;
}
if (setMethod == null) {
// This can happen in a constructor - there it is possible to assign to a property
// without a setter. This turns into an assignment to the compiler-generated backing field.
Expand Down Expand Up @@ -249,8 +254,14 @@ public override TValue VisitPropertyReference (IPropertyReferenceOperation opera
// Accessing property for reading is really a call to the getter
// The setter case is handled in assignment operation since here we don't have access to the value to pass to the setter
TValue instanceValue = Visit (operation.Instance, state);
IPropertySymbol? property = operation.Property;
IMethodSymbol? getMethod;
while ((getMethod = property.GetMethod) == null) {
if ((property = property.OverriddenProperty) == null)
break;
}
return HandleMethodCall (
operation.Property.GetMethod!,
getMethod!,
instanceValue,
ImmutableArray<TValue>.Empty,
operation);
Expand Down
41 changes: 41 additions & 0 deletions test/Mono.Linker.Tests.Cases/DataFlow/PropertyDataFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static void Main ()

PropertyWithAttributeMarkingItself.Test ();
WriteToGetOnlyProperty.Test ();

BasePropertyAccess.Test ();
}

[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)]
Expand Down Expand Up @@ -547,6 +549,45 @@ public static void Test ()
}
}

class BasePropertyAccess
{
class Base
{
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)]
public virtual Type DerivedGetOnly { get; set; }

[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
public virtual Type DerivedSetOnly { get; set; }
}

class Derived : Base
{
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)]
public override Type DerivedGetOnly { get => null; }

[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
public override Type DerivedSetOnly { set => throw null; }
}

[ExpectedWarning ("IL2072", nameof (Derived.DerivedGetOnly) + ".set", nameof (GetTypeWithNonPublicConstructors))]
static void TestWriteToDerivedGetOnly ()
{
new Derived ().DerivedGetOnly = GetTypeWithNonPublicConstructors ();
}

[ExpectedWarning ("IL2072", nameof (Derived.DerivedSetOnly) + ".get", nameof (DataFlowTypeExtensions.RequiresAll))]
static void TestReadFromDerivedSetOnly ()
{
new Derived ().DerivedSetOnly.RequiresAll ();
}

public static void Test ()
{
TestWriteToDerivedGetOnly ();
TestReadFromDerivedSetOnly ();
}
}

[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
private static Type GetTypeWithPublicParameterlessConstructor ()
{
Expand Down