diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs index 75f65536fa..c8379b1819 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs @@ -66,6 +66,15 @@ public record PrimaryCtorWithField(int A, string B) public double C = 1.0; public string D = A + B; } +#if EXPECTED_OUTPUT + public record PrimaryCtorWithReorderedPropertyInitializers(int First, int Second); +#else + public record PrimaryCtorWithReorderedPropertyInitializers(int First, int Second) + { + public int Second { get; init; } = Second; + public int First { get; init; } = First; + } +#endif public record PrimaryCtorWithInParameter(in int A, in string B); public record PrimaryCtorWithProperty(int A, string B) { diff --git a/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs b/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs index 373c14f540..6319c86e50 100644 --- a/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs @@ -293,8 +293,6 @@ bool IsPrimaryConstructor(Block body, IMethod method, IMethod unspecializedMetho if (body.Instructions.Count < addonInst) return false; - int parameterIndex = 0; - for (int i = 0; i < body.Instructions.Count - addonInst; i++) { if (!body.Instructions[i].MatchStFld(out var target, out var field, out var valueInst)) @@ -306,16 +304,14 @@ bool IsPrimaryConstructor(Block body, IMethod method, IMethod unspecializedMetho continue; if (valueInst.MatchLdLoc(out var v)) { - if (!ValidateParameter(v, parameterIndex)) + if (!ValidateParameter(v)) return false; - parameterIndex = v.Index!.Value; } else if (valueInst.MatchLdObj(out valueInst, out _) && valueInst.MatchLdLoc(out v)) { - if (!ValidateParameter(v, parameterIndex)) + if (!ValidateParameter(v)) return false; - parameterIndex = v.Index!.Value; - if (method.Parameters[parameterIndex].ReferenceKind is ReferenceKind.None) + if (method.Parameters[v.Index!.Value].ReferenceKind is ReferenceKind.None) { return false; } @@ -324,7 +320,7 @@ bool IsPrimaryConstructor(Block body, IMethod method, IMethod unspecializedMetho { continue; } - IParameter parameter = unspecializedMethod.Parameters[parameterIndex]; + IParameter parameter = unspecializedMethod.Parameters[v.Index!.Value]; if (primaryCtorParameterToAutoProperty.ContainsKey(parameter)) { continue; @@ -343,17 +339,12 @@ bool IsPrimaryConstructor(Block body, IMethod method, IMethod unspecializedMetho var returnInst = body.Instructions.LastOrDefault(); return returnInst != null && returnInst.MatchReturn(out var retVal) && retVal.MatchNop(); - bool ValidateParameter(ILVariable v, int expectedMinimumIndex) + bool ValidateParameter(ILVariable v) { if (v.Kind != VariableKind.Parameter) return false; Debug.Assert(v.Index.HasValue); - if (v.Index < 0 || v.Index >= unspecializedMethod.Parameters.Count) - return false; - var parameter = unspecializedMethod.Parameters[v.Index.Value]; - if (primaryCtorParameterToAutoProperty.ContainsKey(parameter)) - return true; - return v.Index >= expectedMinimumIndex; + return v.Index >= 0 && v.Index < unspecializedMethod.Parameters.Count; } }