Browse Source

Fix #1319: Property name collides with variable name

pull/1324/head
Siegfried Pammer 7 years ago
parent
commit
b18653ba82
  1. 9
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/AutoProperties.cs
  2. 26
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/AutoProperties.opt.roslyn.il
  3. 28
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/AutoProperties.roslyn.il
  4. 21
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  5. 13
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

9
ICSharpCode.Decompiler.Tests/TestCases/Pretty/AutoProperties.cs

@ -29,5 +29,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -29,5 +29,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
get;
set;
}
public int issue1319 {
get;
}
public AutoProperties(int issue1319)
{
this.issue1319 = issue1319;
}
}
}

26
ICSharpCode.Decompiler.Tests/TestCases/Pretty/AutoProperties.opt.roslyn.il

@ -47,6 +47,8 @@ @@ -47,6 +47,8 @@
.field private int32 '<PropertyWithAttributeOnBackingField>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.ObsoleteAttribute::.ctor(string) = ( 01 00 05 46 69 65 6C 64 00 00 ) // ...Field..
.field private initonly int32 '<issue1319>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.method public hidebysig specialname instance int32
get_A() cil managed
{
@ -135,10 +137,21 @@ @@ -135,10 +137,21 @@
IL_0007: ret
} // end of method AutoProperties::set_PropertyWithAttributeOnBackingField
.method public hidebysig specialname instance int32
get_issue1319() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::'<issue1319>k__BackingField'
IL_0006: ret
} // end of method AutoProperties::get_issue1319
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
instance void .ctor(int32 issue1319) cil managed
{
// Code size 21 (0x15)
// Code size 28 (0x1c)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
@ -148,7 +161,10 @@ @@ -148,7 +161,10 @@
IL_0009: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::'<B>k__BackingField'
IL_000e: ldarg.0
IL_000f: call instance void [mscorlib]System.Object::.ctor()
IL_0014: ret
IL_0014: ldarg.0
IL_0015: ldarg.1
IL_0016: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::'<issue1319>k__BackingField'
IL_001b: ret
} // end of method AutoProperties::.ctor
.method private hidebysig specialname rtspecialname static
@ -187,6 +203,10 @@ @@ -187,6 +203,10 @@
.get instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::get_PropertyWithAttributeOnBackingField()
.set instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::set_PropertyWithAttributeOnBackingField(int32)
} // end of property AutoProperties::PropertyWithAttributeOnBackingField
.property instance int32 issue1319()
{
.get instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::get_issue1319()
} // end of property AutoProperties::issue1319
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties

28
ICSharpCode.Decompiler.Tests/TestCases/Pretty/AutoProperties.roslyn.il

@ -52,6 +52,9 @@ @@ -52,6 +52,9 @@
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = ( 01 00 00 00 00 00 00 00 )
.custom instance void [mscorlib]System.ObsoleteAttribute::.ctor(string) = ( 01 00 05 46 69 65 6C 64 00 00 ) // ...Field..
.field private initonly int32 '<issue1319>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = ( 01 00 00 00 00 00 00 00 )
.method public hidebysig specialname instance int32
get_A() cil managed
{
@ -140,10 +143,21 @@ @@ -140,10 +143,21 @@
IL_0007: ret
} // end of method AutoProperties::set_PropertyWithAttributeOnBackingField
.method public hidebysig specialname instance int32
get_issue1319() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::'<issue1319>k__BackingField'
IL_0006: ret
} // end of method AutoProperties::get_issue1319
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
instance void .ctor(int32 issue1319) cil managed
{
// Code size 22 (0x16)
// Code size 30 (0x1e)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.1
@ -154,7 +168,11 @@ @@ -154,7 +168,11 @@
IL_000e: ldarg.0
IL_000f: call instance void [mscorlib]System.Object::.ctor()
IL_0014: nop
IL_0015: ret
IL_0015: nop
IL_0016: ldarg.0
IL_0017: ldarg.1
IL_0018: stfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::'<issue1319>k__BackingField'
IL_001d: ret
} // end of method AutoProperties::.ctor
.method private hidebysig specialname rtspecialname static
@ -193,6 +211,10 @@ @@ -193,6 +211,10 @@
.get instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::get_PropertyWithAttributeOnBackingField()
.set instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::set_PropertyWithAttributeOnBackingField(int32)
} // end of property AutoProperties::PropertyWithAttributeOnBackingField
.property instance int32 issue1319()
{
.get instance int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties::get_issue1319()
} // end of property AutoProperties::issue1319
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.AutoProperties

21
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -192,14 +192,31 @@ namespace ICSharpCode.Decompiler.CSharp @@ -192,14 +192,31 @@ namespace ICSharpCode.Decompiler.CSharp
return currentFunction.Ancestors.OfType<ILFunction>().SelectMany(f => f.Variables).Any(v => v.Name == name);
}
bool RequiresQualifier(IMember member, TranslatedExpression target)
{
if (HidesVariableWithName(member.Name))
return true;
if (member.IsStatic)
return !IsCurrentOrContainingType(member.DeclaringTypeDefinition);
return !(target.Expression is ThisReferenceExpression || target.Expression is BaseReferenceExpression);
}
ExpressionWithResolveResult ConvertField(IField field, ILInstruction targetInstruction = null)
{
var target = TranslateTarget(targetInstruction,
nonVirtualInvocation: true,
memberStatic: field.IsStatic,
memberDeclaringType: field.DeclaringType);
bool requireTarget = HidesVariableWithName(field.Name)
|| (field.IsStatic ? !IsCurrentOrContainingType(field.DeclaringTypeDefinition) : !(target.Expression is ThisReferenceExpression || target.Expression is BaseReferenceExpression));
bool requireTarget;
// If this is a reference to the backing field of an automatic property and we're going to transform automatic properties
// in PatternStatementTransform, then we have to do the "requires qualifier"-check based on the property instead of the field.
// It is easier to solve this special case here than in PatternStatementTransform, because here we perform all resolver checks.
// It feels a bit hacky, though.
if (settings.AutomaticProperties && PatternStatementTransform.IsBackingFieldOfAutomaticProperty(field, out var property)) {
requireTarget = RequiresQualifier(property, target);
} else {
requireTarget = RequiresQualifier(field, target);
}
bool targetCasted = false;
var targetResolveResult = requireTarget ? target.ResolveResult : null;

13
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -565,6 +565,19 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -565,6 +565,19 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
return base.VisitIdentifier(identifier);
}
internal static bool IsBackingFieldOfAutomaticProperty(IField field, out IProperty property)
{
property = null;
if (!(field.Name.StartsWith("<") && field.Name.EndsWith(">k__BackingField")))
return false;
if (!field.IsCompilerGenerated())
return false;
var propertyName = field.Name.Substring(1, field.Name.Length - 1 - ">k__BackingField".Length);
property = field.DeclaringTypeDefinition
.GetProperties(p => p.Name == propertyName, GetMemberOptions.IgnoreInheritedMembers).FirstOrDefault();
return property != null;
}
Identifier ReplaceBackingFieldUsage(Identifier identifier)
{
if (identifier.Name.StartsWith("<") && identifier.Name.EndsWith(">k__BackingField")) {

Loading…
Cancel
Save