Skip to content

Recognize a VB exception filter's type test - #4134

Open
dualfroz wants to merge 4 commits into
icsharpcode:masterfrom
dualfroz:fix-vb-exception-filter-type-test
Open

Recognize a VB exception filter's type test#4134
dualfroz wants to merge 4 commits into
icsharpcode:masterfrom
dualfroz:fix-vb-exception-filter-type-test

Conversation

@dualfroz

Copy link
Copy Markdown

vbc emits a filter as one and expression rather than the branch chain csc emits, so VB filters decompiled to catch (object obj).
That form is now matched and becomes catch (Exception ex) when (...), but only when the whole filter is pure, since moving the type test changes evaluation order.

Closes #3659

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 icsharpcode#3659
@siegfriedpammer

Copy link
Copy Markdown
Member

Did you use AI to implement this? Please read https://github.com/icsharpcode/ILSpy/blob/master/CONTRIBUTING.md#contributing especially bullet 2.

@@ -0,0 +1,109 @@
.assembly extern System.Runtime

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this a VBPretty test?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my bad. Will change that in a sec

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be done, sorry, won't happen again

@dualfroz

Copy link
Copy Markdown
Author

Hi. I didn't use the AI to write the code itself. I did however use AI to make it easier to read. Same with the comment - i wrote it myself and then told ai to please make it look nicer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong Decompilation: catch (object obj) when (obj is System.Exception) for VB exception

2 participants