Browse Source

Fix #2406: for readonly property with only getter, move readonly to property instead of getter

pull/2408/head
SilverFox 4 years ago
parent
commit
65ea2e7af6
  1. 7
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs
  2. 7
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs
  3. 3
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

7
ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs

@ -98,6 +98,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
} }
} }
public readonly int ReadOnlyPropertyWithOnlyGetter {
get {
Console.WriteLine("No inlining");
return 1;
}
}
public ref int RefProperty => ref arr[0]; public ref int RefProperty => ref arr[0];
public ref readonly int RefReadonlyProperty => ref arr[0]; public ref readonly int RefReadonlyProperty => ref arr[0];
public readonly ref int ReadonlyRefProperty => ref arr[0]; public readonly ref int ReadonlyRefProperty => ref arr[0];

7
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -2035,7 +2035,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
static void MergeReadOnlyModifiers(EntityDeclaration decl, Accessor accessor1, Accessor accessor2) static void MergeReadOnlyModifiers(EntityDeclaration decl, Accessor accessor1, Accessor accessor2)
{ {
if (accessor1.HasModifier(Modifiers.Readonly) && accessor2.HasModifier(Modifiers.Readonly)) if (accessor1.HasModifier(Modifiers.Readonly) && accessor1.Role == PropertyDeclaration.GetterRole && accessor2.IsNull)
{
accessor1.Modifiers &= ~Modifiers.Readonly;
decl.Modifiers |= Modifiers.Readonly;
}
else if (accessor1.HasModifier(Modifiers.Readonly) && accessor2.HasModifier(Modifiers.Readonly))
{ {
accessor1.Modifiers &= ~Modifiers.Readonly; accessor1.Modifiers &= ~Modifiers.Readonly;
accessor2.Modifiers &= ~Modifiers.Readonly; accessor2.Modifiers &= ~Modifiers.Readonly;

3
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -630,7 +630,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
} }
if (field == null || !NameCouldBeBackingFieldOfAutomaticProperty(field.Name, out _)) if (field == null || !NameCouldBeBackingFieldOfAutomaticProperty(field.Name, out _))
return null; return null;
if (propertyDeclaration.Setter.HasModifier(Modifiers.Readonly)) if (propertyDeclaration.Setter.HasModifier(Modifiers.Readonly) || (propertyDeclaration.HasModifier(Modifiers.Readonly) && !propertyDeclaration.Setter.IsNull))
return null; return null;
if (field.IsCompilerGenerated() && field.DeclaringTypeDefinition == property.DeclaringTypeDefinition) if (field.IsCompilerGenerated() && field.DeclaringTypeDefinition == property.DeclaringTypeDefinition)
{ {
@ -638,6 +638,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
RemoveCompilerGeneratedAttribute(propertyDeclaration.Setter.Attributes); RemoveCompilerGeneratedAttribute(propertyDeclaration.Setter.Attributes);
propertyDeclaration.Getter.Body = null; propertyDeclaration.Getter.Body = null;
propertyDeclaration.Setter.Body = null; propertyDeclaration.Setter.Body = null;
propertyDeclaration.Modifiers &= ~Modifiers.Readonly;
propertyDeclaration.Getter.Modifiers &= ~Modifiers.Readonly; propertyDeclaration.Getter.Modifiers &= ~Modifiers.Readonly;
// Add C# 7.3 attributes on backing field: // Add C# 7.3 attributes on backing field:

Loading…
Cancel
Save