diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs index b631f9b7c..f4c0f1187 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs +++ b/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 readonly int RefReadonlyProperty => ref arr[0]; public readonly ref int ReadonlyRefProperty => ref arr[0]; diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs index fde5a7647..60da319d8 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs @@ -2035,7 +2035,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax 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; accessor2.Modifiers &= ~Modifiers.Readonly; diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs index 7fe25ef73..718c983b2 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs @@ -630,7 +630,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms } if (field == null || !NameCouldBeBackingFieldOfAutomaticProperty(field.Name, out _)) return null; - if (propertyDeclaration.Setter.HasModifier(Modifiers.Readonly)) + if (propertyDeclaration.Setter.HasModifier(Modifiers.Readonly) || (propertyDeclaration.HasModifier(Modifiers.Readonly) && !propertyDeclaration.Setter.IsNull)) return null; if (field.IsCompilerGenerated() && field.DeclaringTypeDefinition == property.DeclaringTypeDefinition) { @@ -638,6 +638,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms RemoveCompilerGeneratedAttribute(propertyDeclaration.Setter.Attributes); propertyDeclaration.Getter.Body = null; propertyDeclaration.Setter.Body = null; + propertyDeclaration.Modifiers &= ~Modifiers.Readonly; propertyDeclaration.Getter.Modifiers &= ~Modifiers.Readonly; // Add C# 7.3 attributes on backing field: