Browse Source

Fix #2143: Missing `this.` qualification in lambda

pull/2145/head
Siegfried Pammer 6 years ago
parent
commit
549f7fb44d
  1. 7
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs
  2. 27
      ICSharpCode.Decompiler/IL/Instructions/ILVariableCollection.cs

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

@ -32,6 +32,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public string Value; public string Value;
} }
private int x;
public Action CaptureOfThis() public Action CaptureOfThis()
{ {
return delegate { return delegate {
@ -134,6 +136,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}); });
} }
public Func<int, int> Issue2143()
{
return (int x) => this.x;
}
public Action<object> Bug971_DelegateWithoutParameterList() public Action<object> Bug971_DelegateWithoutParameterList()
{ {
return delegate { return delegate {

27
ICSharpCode.Decompiler/IL/Instructions/ILVariableCollection.cs

@ -104,11 +104,7 @@ namespace ICSharpCode.Decompiler.IL
{ {
for (int i = 0; i < list.Count;) for (int i = 0; i < list.Count;)
{ {
var v = list[i]; if (ShouldRemoveVariable(list[i]))
// Note: we cannot remove display-class locals from the collection,
// even if they are unused - which is always the case, if TDCU succeeds,
// because they are necessary for PDB generation to produce correct results.
if (v.IsDead && v.Kind != VariableKind.DisplayClassLocal)
{ {
RemoveAt(i); RemoveAt(i);
} }
@ -117,6 +113,27 @@ namespace ICSharpCode.Decompiler.IL
i++; i++;
} }
} }
static bool ShouldRemoveVariable(ILVariable v)
{
if (!v.IsDead)
return false;
// Note: we cannot remove display-class locals from the collection,
// even if they are unused - which is always the case, if TDCU succeeds,
// because they are necessary for PDB generation to produce correct results.
if (v.Kind == VariableKind.DisplayClassLocal)
return false;
// Do not remove parameter variables, as these are defined even if unused.
if (v.Kind == VariableKind.Parameter)
{
// However, remove unused this-parameters of delegates, expression trees, etc.
// These will be replaced with the top-level function's this-parameter.
if (v.Index == -1 && v.Function.Kind != ILFunctionKind.TopLevelFunction)
return true;
return false;
}
return true;
}
} }
public int Count { public int Count {

Loading…
Cancel
Save