Skip to content

Commit 3fd9981

Browse files
Fix #3621: Fix multiple uses of primary ctor parameter in initializer of record.
1 parent 193a463 commit 3fd9981

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ public UnexpectedCodeInCtor(int A)
232232
this.A = A;
233233
}
234234
}
235+
236+
private record RecordWithMultipleInitializerAssignmentsInPrimaryCtor(string name, string? value, in object encode)
237+
{
238+
public string? Value { get; } = name;
239+
240+
public string Name { get; } = value;
241+
242+
private string? WebValue { get; } = (name != null) ? "111" : value;
243+
244+
private string? WebValue2;
245+
}
235246
}
236247

237248
#if CS100

ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,13 @@ bool IsPrimaryConstructor(Block body, IMethod method, IMethod unspecializedMetho
303303
continue;
304304
if (valueInst.MatchLdLoc(out var v))
305305
{
306-
if (v.Kind != VariableKind.Parameter || v.Index < parameterIndex)
306+
if (!ValidateParameter(v, parameterIndex))
307307
return false;
308308
parameterIndex = v.Index!.Value;
309309
}
310310
else if (valueInst.MatchLdObj(out valueInst, out _) && valueInst.MatchLdLoc(out v))
311311
{
312-
if (v.Kind != VariableKind.Parameter || v.Index < parameterIndex)
312+
if (!ValidateParameter(v, parameterIndex))
313313
return false;
314314
parameterIndex = v.Index!.Value;
315315
if (method.Parameters[parameterIndex].ReferenceKind is ReferenceKind.None)
@@ -339,6 +339,19 @@ bool IsPrimaryConstructor(Block body, IMethod method, IMethod unspecializedMetho
339339

340340
var returnInst = body.Instructions.LastOrDefault();
341341
return returnInst != null && returnInst.MatchReturn(out var retVal) && retVal.MatchNop();
342+
343+
bool ValidateParameter(ILVariable v, int expectedMinimumIndex)
344+
{
345+
if (v.Kind != VariableKind.Parameter)
346+
return false;
347+
Debug.Assert(v.Index.HasValue);
348+
if (v.Index < 0 || v.Index >= unspecializedMethod.Parameters.Count)
349+
return false;
350+
var parameter = unspecializedMethod.Parameters[v.Index.Value];
351+
if (primaryCtorParameterToAutoProperty.ContainsKey(parameter))
352+
return true;
353+
return v.Index >= expectedMinimumIndex;
354+
}
342355
}
343356

344357
IMethod? FindChainedCtor(Block body)

0 commit comments

Comments
 (0)