Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -592,41 +592,22 @@ protected override TypeAnnotations CreateValueFromKey(TypeDesc key)
}
}

if (IsAutoProperty(property))
FieldDesc backingField = backingFieldFromSetter ?? backingFieldFromGetter!;
Comment thread
jtschuster marked this conversation as resolved.
Outdated
if (backingField is not null)
{
FieldDesc? backingField = null;
if ((property.SetMethod is not null
&& property.SetMethod.HasCustomAttribute("System.Runtime.CompilerServices", "CompilerGeneratedAttribute")
&& backingFieldFromSetter is null)
|| (property.GetMethod is not null
&& property.GetMethod.HasCustomAttribute("System.Runtime.CompilerServices", "CompilerGeneratedAttribute")
&& backingFieldFromGetter is null))
if (annotatedFields.Any(a => a.Field == backingField))
Comment thread
jtschuster marked this conversation as resolved.
Outdated
Comment thread
sbomer marked this conversation as resolved.
Outdated
{
// We failed to find the backing field of an auto-property accessor
_logger.LogWarning(property, DiagnosticId.DynamicallyAccessedMembersCouldNotFindBackingField, property.GetDisplayName());
_logger.LogWarning(backingField, DiagnosticId.DynamicallyAccessedMembersOnPropertyConflictsWithBackingField, property.GetDisplayName(), backingField.GetDisplayName());
}
else if (backingFieldFromGetter is not null && backingFieldFromSetter is not null
&& backingFieldFromSetter != backingFieldFromGetter)
else if (backingFieldFromGetter is not null
&& backingFieldFromSetter is not null
&& backingFieldFromGetter != backingFieldFromSetter)
{
// We found two different backing fields for the getter and the setter
_logger.LogWarning(property, DiagnosticId.DynamicallyAccessedMembersCouldNotFindBackingField, property.GetDisplayName());
Comment thread
jtschuster marked this conversation as resolved.
Outdated
}
else
{
// We either have a single auto-property accessor or both accessors point to the same backing field
backingField = backingFieldFromSetter ?? backingFieldFromGetter;
}

if (backingField != null)
{
if (annotatedFields.Any(a => a.Field == backingField))
{
_logger.LogWarning(backingField, DiagnosticId.DynamicallyAccessedMembersOnPropertyConflictsWithBackingField, property.GetDisplayName(), backingField.GetDisplayName());
}
else
{
annotatedFields.Add(new FieldAnnotation(backingField, annotation));
}
annotatedFields.Add(new FieldAnnotation(backingField, annotation));
}
Comment thread
jtschuster marked this conversation as resolved.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ internal static DynamicallyAccessedMemberTypes GetFieldAnnotation(IFieldSymbol f
if (!field.OriginalDefinition.Type.IsTypeInterestingForDataflow(isByRef: field.RefKind is not RefKind.None))
return DynamicallyAccessedMemberTypes.None;

if (field.AssociatedSymbol is IPropertySymbol property)
if (field.AssociatedSymbol is IPropertySymbol property
&& property.IsAutoProperty())
{
// If this is an auto property, we get the property annotation
if (property.IsAutoProperty())
return property.GetDynamicallyAccessedMemberTypes();
return property.GetDynamicallyAccessedMemberTypes();
}

return field.GetDynamicallyAccessedMemberTypes();
Expand Down
36 changes: 9 additions & 27 deletions src/tools/illink/src/linker/Linker.Dataflow/FlowAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,41 +433,23 @@ TypeAnnotations BuildTypeAnnotations(TypeDefinition type)
}
}

if (IsAutoProperty(property))

FieldDefinition backingField = backingFieldFromSetter ?? backingFieldFromGetter!;
Comment thread
jtschuster marked this conversation as resolved.
Outdated
if (backingField is not null)
{
FieldDefinition? backingField = null;
// If it's an annotated auto-prop, we should be able to find the compiler generated field
if ((property.SetMethod is not null
&& property.SetMethod.IsCompilerGenerated()
&& backingFieldFromSetter is null)
|| (property.GetMethod is not null
&& property.GetMethod.IsCompilerGenerated()
&& backingFieldFromGetter is null))
if (annotatedFields.Any(a => a.Field == backingField))
{
// We failed to find the backing field of an auto-property accessor
_context.LogWarning(property, DiagnosticId.DynamicallyAccessedMembersCouldNotFindBackingField, property.GetDisplayName());
_context.LogWarning(backingField, DiagnosticId.DynamicallyAccessedMembersOnPropertyConflictsWithBackingField, property.GetDisplayName(), backingField.GetDisplayName());
}
else if (backingFieldFromGetter is not null && backingFieldFromSetter is not null
&& backingFieldFromSetter != backingFieldFromGetter)
else if (backingFieldFromGetter is not null
&& backingFieldFromSetter is not null
&& backingFieldFromGetter != backingFieldFromSetter)
{
// We found two different backing fields for the getter and the setter
_context.LogWarning(property, DiagnosticId.DynamicallyAccessedMembersCouldNotFindBackingField, property.GetDisplayName());
}
else
{
// We either have a single auto-property accessor or both accessors point to the same backing field
backingField = backingFieldFromSetter ?? backingFieldFromGetter;
}
if (backingField != null)
{
if (annotatedFields.Any(a => a.Field == backingField))
{
_context.LogWarning(backingField, DiagnosticId.DynamicallyAccessedMembersOnPropertyConflictsWithBackingField, property.GetDisplayName(), backingField.GetDisplayName());
}
else
{
annotatedFields.Add(new FieldAnnotation(backingField, annotation));
}
annotatedFields.Add(new FieldAnnotation(backingField, annotation));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,6 @@ class AutoPropertyUnrecognizedField
private Type Property_BackingField;

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[ExpectedWarning("IL2042", Tool.NativeAot | Tool.Trimmer, "Requires IL")] // Can't find backing field
public Type Property
{
[CompilerGenerated]
Expand All @@ -980,7 +979,6 @@ public Type Property
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[ExpectedWarning("IL2042", Tool.NativeAot | Tool.Trimmer, "Requires IL")] // Can't find backing field
public Type PropertyAutoSet
{
[CompilerGenerated]
Expand All @@ -995,10 +993,8 @@ public Type PropertyAutoSet
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[ExpectedWarning("IL2042", Tool.NativeAot | Tool.Trimmer, "Requires IL")] // Can't find backing field
public Type PropertyAutoGet
{
[ExpectedWarning("IL2078", ["return value", nameof(PropertyAutoGet), "BackingField"], producedBy: Tool.NativeAot | Tool.Trimmer, "Requires IL")]
get;
[CompilerGenerated]
set
Expand All @@ -1010,7 +1006,6 @@ public Type PropertyAutoGet
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[ExpectedWarning("IL2042", Tool.NativeAot | Tool.Trimmer, "Requires IL")] // Can't find backing field
public Type PropertyManualSet
{
[CompilerGenerated]
Expand All @@ -1025,7 +1020,6 @@ public Type PropertyManualSet
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[ExpectedWarning("IL2042", Tool.NativeAot | Tool.Trimmer, "Requires IL")] // Can't find backing field
public Type PropertyManualGet
{
[ExpectedWarning("IL2078", "return value", nameof(Property_BackingField))]
Expand All @@ -1040,7 +1034,6 @@ public Type PropertyManualGet
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[ExpectedWarning("IL2042", Tool.NativeAot | Tool.Trimmer, "Requires IL")] // Can't find backing field
public Type PropertyOnlyGet
{
[CompilerGenerated]
Expand All @@ -1054,7 +1047,6 @@ public Type PropertyOnlyGet
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[ExpectedWarning("IL2042", nameof(PropertyOnlySet), Tool.NativeAot | Tool.Trimmer, "Requires IL")] // Can't find backing field
public Type PropertyOnlySet
{
[CompilerGenerated]
Expand Down
Loading