Skip to content

Commit c0a9afc

Browse files
Fix #3616: local variable naming conflict with primary constructor parameters.
1 parent 6755d27 commit c0a9afc

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,32 @@ public class UnusedPrimaryCtorParameter(int unused)
264264
{
265265
}
266266
#endif
267+
#if OPT && EXPECTED_OUTPUT
268+
public class C8(object obj)
269+
{
270+
public int Test()
271+
{
272+
object obj2 = obj;
273+
if (obj2 is int)
274+
{
275+
return (int)obj2;
276+
}
277+
return 0;
278+
}
279+
}
280+
#else
281+
public class C8(object obj)
282+
{
283+
public int Test()
284+
{
285+
if (obj is int result)
286+
{
287+
return result;
288+
}
289+
return 0;
290+
}
291+
}
292+
#endif
267293
#endif
268294
}
269295
}

ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
using ICSharpCode.Decompiler.CSharp;
3030
using ICSharpCode.Decompiler.CSharp.OutputVisitor;
31+
using ICSharpCode.Decompiler.CSharp.Transforms;
3132
using ICSharpCode.Decompiler.CSharp.TypeSystem;
3233
using ICSharpCode.Decompiler.TypeSystem;
3334
using ICSharpCode.Decompiler.TypeSystem.Implementation;
@@ -106,6 +107,15 @@ public VariableScope(ILFunction function, ILTransformContext context, VariableSc
106107
var currentLowerCaseTypeOrMemberNames = new HashSet<string>(StringComparer.Ordinal);
107108
foreach (var name in CollectAllLowerCaseMemberNames(function.Method.DeclaringTypeDefinition))
108109
currentLowerCaseTypeOrMemberNames.Add(name);
110+
foreach (IField item in function.Method.DeclaringTypeDefinition.Fields)
111+
{
112+
if (TransformFieldAndConstructorInitializers.IsGeneratedPrimaryConstructorBackingField(item))
113+
{
114+
string name = item.Name.Substring(1, item.Name.Length - 3);
115+
currentLowerCaseTypeOrMemberNames.Add(name);
116+
AddExistingName(reservedVariableNames, name);
117+
}
118+
}
109119
foreach (var name in CollectAllLowerCaseTypeNames(function.Method.DeclaringTypeDefinition))
110120
{
111121
currentLowerCaseTypeOrMemberNames.Add(name);

0 commit comments

Comments
 (0)