Browse Source

Fix #955: Ambiguous decompilation of anonymous delegates

pull/925/merge
Siegfried Pammer 8 years ago
parent
commit
2ca7ea87f1
  1. 7
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs
  2. 42
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.il
  3. 38
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.opt.il
  4. 61
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.opt.roslyn.il
  5. 64
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.roslyn.il
  6. 2
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  7. 21
      ICSharpCode.Decompiler/CSharp/Resolver/LambdaResolveResult.cs

7
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
@ -80,6 +81,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -80,6 +81,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public void Bar(Func<int> f)
{
}
private void Bug955()
{
new Thread((ThreadStart)delegate {
});
}
}
public static void Test(this string a)

42
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.il

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly kdaefnvq
.assembly '3jzwaefw'
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
@ -26,15 +26,15 @@ @@ -26,15 +26,15 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module kdaefnvq.dll
// MVID: {568C8EB4-6E17-4688-8B36-6BFD1BF31DEB}
.module '3jzwaefw.dll'
// MVID: {915B6C12-1305-49DC-868B-18153BA75025}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00C30000
// Image base: 0x012B0000
// =============== CLASS MEMBERS DECLARATION ===================
@ -213,6 +213,8 @@ @@ -213,6 +213,8 @@
} // end of class '<>c__DisplayClass30'
.field private static class [mscorlib]System.Threading.ThreadStart 'CS$<>9__CachedAnonymousMethodDelegate35'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.method public hidebysig instance class [mscorlib]System.Action
CaptureOfThis() cil managed
{
@ -536,6 +538,28 @@ @@ -536,6 +538,28 @@
IL_0001: ret
} // end of method InstanceTests::Bar
.method private hidebysig instance void
Bug955() cil managed
{
// Code size 39 (0x27)
.maxstack 8
IL_0000: nop
IL_0001: ldsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests::'CS$<>9__CachedAnonymousMethodDelegate35'
IL_0006: brtrue.s IL_001b
IL_0008: ldnull
IL_0009: ldftn void ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests::'<Bug955>b__34'()
IL_000f: newobj instance void [mscorlib]System.Threading.ThreadStart::.ctor(object,
native int)
IL_0014: stsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests::'CS$<>9__CachedAnonymousMethodDelegate35'
IL_0019: br.s IL_001b
IL_001b: ldsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests::'CS$<>9__CachedAnonymousMethodDelegate35'
IL_0020: newobj instance void [mscorlib]System.Threading.Thread::.ctor(class [mscorlib]System.Threading.ThreadStart)
IL_0025: pop
IL_0026: ret
} // end of method InstanceTests::Bug955
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
@ -575,6 +599,16 @@ @@ -575,6 +599,16 @@
IL_000a: ret
} // end of method InstanceTests::'<LambdaInForLoop>b__32'
.method private hidebysig static void
'<Bug955>b__34'() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method InstanceTests::'<Bug955>b__34'
} // end of class InstanceTests
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass1'

38
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.opt.il

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly c0bnf0jn
.assembly kmdztkbc
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
@ -26,15 +26,15 @@ @@ -26,15 +26,15 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module c0bnf0jn.dll
// MVID: {ABE4DCE9-5E34-4357-84BA-7850DA8457DD}
.module kmdztkbc.dll
// MVID: {901EEE35-CE14-4A96-8202-0E758AD0E8B0}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00E80000
// Image base: 0x02C00000
// =============== CLASS MEMBERS DECLARATION ===================
@ -210,6 +210,8 @@ @@ -210,6 +210,8 @@
} // end of class '<>c__DisplayClass30'
.field private static class [mscorlib]System.Threading.ThreadStart 'CS$<>9__CachedAnonymousMethodDelegate35'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.method public hidebysig instance class [mscorlib]System.Action
CaptureOfThis() cil managed
{
@ -457,6 +459,25 @@ @@ -457,6 +459,25 @@
IL_0000: ret
} // end of method InstanceTests::Bar
.method private hidebysig instance void
Bug955() cil managed
{
// Code size 36 (0x24)
.maxstack 8
IL_0000: ldsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests::'CS$<>9__CachedAnonymousMethodDelegate35'
IL_0005: brtrue.s IL_0018
IL_0007: ldnull
IL_0008: ldftn void ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests::'<Bug955>b__34'()
IL_000e: newobj instance void [mscorlib]System.Threading.ThreadStart::.ctor(object,
native int)
IL_0013: stsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests::'CS$<>9__CachedAnonymousMethodDelegate35'
IL_0018: ldsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests::'CS$<>9__CachedAnonymousMethodDelegate35'
IL_001d: newobj instance void [mscorlib]System.Threading.Thread::.ctor(class [mscorlib]System.Threading.ThreadStart)
IL_0022: pop
IL_0023: ret
} // end of method InstanceTests::Bug955
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
@ -490,6 +511,15 @@ @@ -490,6 +511,15 @@
IL_0006: ret
} // end of method InstanceTests::'<LambdaInForLoop>b__32'
.method private hidebysig static void
'<Bug955>b__34'() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method InstanceTests::'<Bug955>b__34'
} // end of class InstanceTests
.class auto ansi sealed nested private beforefieldinit '<>c__DisplayClass1'

61
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.opt.roslyn.il

@ -31,14 +31,14 @@ @@ -31,14 +31,14 @@
.ver 0:0:0:0
}
.module DelegateConstruction.dll
// MVID: {534F3E90-3206-4E6A-821F-26BFD75A6959}
// MVID: {732578B7-A105-4FCC-BD88-23E06857F316}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x01460000
// Image base: 0x013F0000
// =============== CLASS MEMBERS DECLARATION ===================
@ -216,6 +216,42 @@ @@ -216,6 +216,42 @@
} // end of class '<>c__DisplayClass3_2'
.class auto ansi serializable sealed nested private beforefieldinit '<>c'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static initonly class ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c' '<>9'
.field public static class [mscorlib]System.Threading.ThreadStart '<>9__7_0'
.method private hidebysig specialname rtspecialname static
void .cctor() cil managed
{
// Code size 11 (0xb)
.maxstack 8
IL_0000: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::.ctor()
IL_0005: stsfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c' ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<>9'
IL_000a: ret
} // end of method '<>c'::.cctor
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method '<>c'::.ctor
.method assembly hidebysig instance void
'<Bug955>b__7_0'() cil managed
{
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method '<>c'::'<Bug955>b__7_0'
} // end of class '<>c'
.method public hidebysig instance class [mscorlib]System.Action
CaptureOfThis() cil managed
{
@ -438,6 +474,27 @@ @@ -438,6 +474,27 @@
IL_0000: ret
} // end of method InstanceTests::Bar
.method private hidebysig instance void
Bug955() cil managed
{
// Code size 38 (0x26)
.maxstack 8
IL_0000: ldsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<>9__7_0'
IL_0005: dup
IL_0006: brtrue.s IL_001f
IL_0008: pop
IL_0009: ldsfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c' ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<>9'
IL_000e: ldftn instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<Bug955>b__7_0'()
IL_0014: newobj instance void [mscorlib]System.Threading.ThreadStart::.ctor(object,
native int)
IL_0019: dup
IL_001a: stsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<>9__7_0'
IL_001f: newobj instance void [mscorlib]System.Threading.Thread::.ctor(class [mscorlib]System.Threading.ThreadStart)
IL_0024: pop
IL_0025: ret
} // end of method InstanceTests::Bug955
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

64
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.roslyn.il

@ -31,14 +31,14 @@ @@ -31,14 +31,14 @@
.ver 0:0:0:0
}
.module DelegateConstruction.dll
// MVID: {8AC9B4E6-77A7-45DC-BBF4-76DB9EA29AFF}
// MVID: {CAEDF2ED-ABA8-46FA-95A9-59299B362410}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x01820000
// Image base: 0x01390000
// =============== CLASS MEMBERS DECLARATION ===================
@ -225,6 +225,44 @@ @@ -225,6 +225,44 @@
} // end of class '<>c__DisplayClass3_2'
.class auto ansi serializable sealed nested private beforefieldinit '<>c'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static initonly class ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c' '<>9'
.field public static class [mscorlib]System.Threading.ThreadStart '<>9__7_0'
.method private hidebysig specialname rtspecialname static
void .cctor() cil managed
{
// Code size 11 (0xb)
.maxstack 8
IL_0000: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::.ctor()
IL_0005: stsfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c' ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<>9'
IL_000a: ret
} // end of method '<>c'::.cctor
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method '<>c'::.ctor
.method assembly hidebysig instance void
'<Bug955>b__7_0'() cil managed
{
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method '<>c'::'<Bug955>b__7_0'
} // end of class '<>c'
.method public hidebysig instance class [mscorlib]System.Action
CaptureOfThis() cil managed
{
@ -499,6 +537,28 @@ @@ -499,6 +537,28 @@
IL_0001: ret
} // end of method InstanceTests::Bar
.method private hidebysig instance void
Bug955() cil managed
{
// Code size 39 (0x27)
.maxstack 8
IL_0000: nop
IL_0001: ldsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<>9__7_0'
IL_0006: dup
IL_0007: brtrue.s IL_0020
IL_0009: pop
IL_000a: ldsfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c' ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<>9'
IL_000f: ldftn instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<Bug955>b__7_0'()
IL_0015: newobj instance void [mscorlib]System.Threading.ThreadStart::.ctor(object,
native int)
IL_001a: dup
IL_001b: stsfld class [mscorlib]System.Threading.ThreadStart ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction/InstanceTests/'<>c'::'<>9__7_0'
IL_0020: newobj instance void [mscorlib]System.Threading.Thread::.ctor(class [mscorlib]System.Threading.ThreadStart)
IL_0025: pop
IL_0026: ret
} // end of method InstanceTests::Bug955
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{

2
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -1363,7 +1363,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1363,7 +1363,7 @@ namespace ICSharpCode.Decompiler.CSharp
var rr = new DecompiledLambdaResolveResult(
function, delegateType, inferredReturnType,
hasParameterList: ame.HasParameterList,
hasParameterList: isLambda || ame.HasParameterList,
isAnonymousMethod: !isLambda,
isImplicitlyTyped: ame.Parameters.Any(p => p.Type.IsNull));

21
ICSharpCode.Decompiler/CSharp/Resolver/LambdaResolveResult.cs

@ -147,15 +147,18 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -147,15 +147,18 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
public override Conversion IsValid(IType[] parameterTypes, IType returnType, CSharpConversions conversions)
{
if (this.Parameters.Count != parameterTypes.Length)
return Conversion.None;
for (int i = 0; i < parameterTypes.Length; ++i) {
if (!parameterTypes[i].Equals(this.Parameters[i].Type)) {
if (IsImplicitlyTyped) {
// it's possible that different parameter types also lead to a valid conversion
return LambdaConversion.Instance;
} else {
return Conversion.None;
// Anonymous method expressions without parameter lists are applicable to any parameter list.
if (HasParameterList) {
if (this.Parameters.Count != parameterTypes.Length)
return Conversion.None;
for (int i = 0; i < parameterTypes.Length; ++i) {
if (!parameterTypes[i].Equals(this.Parameters[i].Type)) {
if (IsImplicitlyTyped) {
// it's possible that different parameter types also lead to a valid conversion
return LambdaConversion.Instance;
} else {
return Conversion.None;
}
}
}
}

Loading…
Cancel
Save