Files
icsharpcode-ILSpy/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3659.cs
T
dualfroz 594bb08fca Recognize a VB exception filter's type test
vbc builds an exception filter as a single non-short-circuiting
expression: `isinst`, then the user's `when` conditions, combined with
`and`. DetectCatchWhenConditionBlocks only knew the branch chain csc
emits, so the type test stayed inside the filter and the handler kept the
`object` variable it has in IL:

    catch (object obj) when ((obj is Exception) & (num2 != 0) & (num == 0))
    {
        ProjectData.SetProjectError((Exception)obj);

A catch type has to derive from Exception, so that does not compile.

The conjunction form is matched too now, lifting the test to the catch
type as the block form already does. The other conjuncts stop running for
a non-matching exception once the test moves, so the filter has to be
pure for this to be invisible; PropagateExceptionVariable then drops the
castclass in the handler:

    catch (Exception ex) when ((num2 != 0) & (num == 0))
    {
        ProjectData.SetProjectError(ex);

Closes #3659
2026-09-13 15:38:45 +02:00

66 lines
1.3 KiB
C#

using System;
using Microsoft.VisualBasic.CompilerServices;
namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty
{
public class Issue3659
{
public static void Func(ref Issue3659 obj, object value)
{
}
public static int ShowMessage(string a, int b, string c, object d, int e)
{
return 0;
}
internal void VBFunction(object value)
{
int try0000_dispatch = -1;
int num2 = default;
int num = default;
while (true)
{
try
{
/*Note: ILSpy has introduced the following switch to emulate a goto from catch-block to try-block*/;
switch (try0000_dispatch)
{
default:
{
ProjectData.ClearProjectError();
num2 = 2;
Issue3659 obj = this;
Func(ref obj, value);
goto end_IL_0000;
}
case 40:
num = -1;
switch (num2)
{
case 2:
ShowMessage("VBFunction", 0, "Exception", null, 0);
goto end_IL_0000;
}
break;
}
}
catch (Exception ex) when ((num2 != 0) & (num == 0))
{
ProjectData.SetProjectError(ex);
try0000_dispatch = 40;
continue;
}
throw ProjectData.CreateProjectError(-214628237);
continue;
end_IL_0000:
break;
}
if (num != 0)
{
ProjectData.ClearProjectError();
}
}
}
}