Browse Source

Fix #3703: Do not transform to a primary constructor if it's not public (or protected in abstract classes)

pull/3713/head
Siegfried Pammer 3 weeks ago
parent
commit
79b0cbb719
  1. 12
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstructorInitializers.cs
  2. 3
      ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs
  3. 5
      ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

12
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ConstructorInitializers.cs

@ -134,7 +134,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{ {
public MethodCallInCtorInit(ConsoleKey key) public MethodCallInCtorInit(ConsoleKey key)
#if MCS5 #if MCS5
: this(((int)key/*cast due to .constrained prefix*/).ToString()) : this(((int)key/*cast due to constrained. prefix*/).ToString())
#else #else
: this(((int)key).ToString()) : this(((int)key).ToString())
#endif #endif
@ -286,6 +286,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
} }
} }
#endif #endif
public struct My
{
private ClassWithConstantAndStaticCtor test;
internal My(ClassWithConstantAndStaticCtor a)
{
test = a;
}
}
#endif #endif
} }
} }

3
ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs

@ -179,12 +179,15 @@ namespace ICSharpCode.Decompiler.CSharp
} }
IMethod? guessedPrimaryCtor = null; IMethod? guessedPrimaryCtor = null;
Accessibility expectedCtorAccessibility = recordTypeDef.IsAbstract ? Accessibility.Protected : Accessibility.Public;
foreach (var method in recordTypeDef.Methods) foreach (var method in recordTypeDef.Methods)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
if (method.IsStatic || !method.IsConstructor) if (method.IsStatic || !method.IsConstructor)
continue; continue;
if (method.Accessibility != expectedCtorAccessibility)
continue;
if (IsCopyConstructor(method)) if (IsCopyConstructor(method))
{ {
continue; continue;

5
ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

@ -340,6 +340,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
} }
} }
Accessibility expectedCtorAccessibility = TypeDefinition.IsAbstract ? Accessibility.Protected : Accessibility.Public;
if (context.Settings.UsePrimaryConstructorSyntaxForNonRecordTypes if (context.Settings.UsePrimaryConstructorSyntaxForNonRecordTypes
&& RecordDecompiler == null && constructorsNotChainedWithThis.Count == 1) && RecordDecompiler == null && constructorsNotChainedWithThis.Count == 1)
{ {
@ -351,7 +353,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
var initializer = InitializerSequence.Analyze(this, ctor, ctorMethod); var initializer = InitializerSequence.Analyze(this, ctor, ctorMethod);
if (initializer is { CoversFullBody: true, HasDuplicateAssignments: false, Statements.Count: > 0 }) if (initializer is { CoversFullBody: true, HasDuplicateAssignments: false, Statements.Count: > 0 }
&& ctorMethod.Accessibility == expectedCtorAccessibility)
{ {
bool transformToPrimaryConstructor = MetadataTokens.GetRowNumber(ctorMethod.MetadataToken) == firstMethodRowNumber; bool transformToPrimaryConstructor = MetadataTokens.GetRowNumber(ctorMethod.MetadataToken) == firstMethodRowNumber;

Loading…
Cancel
Save