Skip to content

Commit d1fdf7e

Browse files
committed
Fixed the issue where IsPrimaryConstructor's judgment was too conservative, which resulted in the inability to generate correct code in many scenarios.
1 parent df49abd commit d1fdf7e

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,25 +198,12 @@ bool IsPrimaryConstructor(IMethod method, IMethod unspecializedMethod)
198198
if (method.Parameters.Count == 0)
199199
return false;
200200

201-
var addonInst = isStruct ? 1 : 2;
202-
if (body.Instructions.Count < method.Parameters.Count + addonInst)
203-
return false;
204-
205201
for (int i = 0; i < method.Parameters.Count; i++)
206202
{
207203
if (!body.Instructions[i].MatchStFld(out var target, out var field, out var valueInst))
208-
return false;
204+
break;
209205
if (!target.MatchLdThis())
210206
return false;
211-
if (method.Parameters[i].ReferenceKind is ReferenceKind.In or ReferenceKind.RefReadOnly)
212-
{
213-
if (!valueInst.MatchLdObj(out valueInst, out _))
214-
return false;
215-
}
216-
if (!valueInst.MatchLdLoc(out var value))
217-
return false;
218-
if (!(value.Kind == VariableKind.Parameter && value.Index == i))
219-
return false;
220207
IMember backingMember;
221208
if (backingFieldToAutoProperty.TryGetValue(field, out var property))
222209
{
@@ -379,7 +366,10 @@ public bool IsPropertyDeclaredByPrimaryConstructor(IProperty property)
379366

380367
internal (IProperty prop, IField field) GetPropertyInfoByPrimaryConstructorParameter(IParameter parameter)
381368
{
382-
var member = primaryCtorParameterToAutoPropertyOrBackingField[parameter];
369+
if (!primaryCtorParameterToAutoPropertyOrBackingField.TryGetValue(parameter, out var member))
370+
{
371+
return (null, null);
372+
}
383373
if (member is IField field)
384374
return (null, field);
385375
return ((IProperty)member, autoPropertyToBackingField[(IProperty)member]);

0 commit comments

Comments
 (0)