Improve null case handling in MatchLegacySwitchOnStringWithDict#3422
Improve null case handling in MatchLegacySwitchOnStringWithDict#3422siegfriedpammer merged 3 commits intoicsharpcode:masterfrom
Conversation
Updated the condition for `nullValueCaseBlock` to ensure it is not null and not equal to `defaultBlock`.
|
Thank you very much for looking into this problem and providing a fix! I think a test case would be nice, but is not strictly necessary. Judging from the .NET Framework version, this is code generated by the C# 2.0 compiler and I think you could only add an IL pretty test, similar to the test in your last PR. The fix is very simple and fixing an obvious flaw, if you look at the code that handles C# 1.0 switch on string (using Hashtable), you can see that a check for Leave instructions (where the defaultBlock would be null) is present there, but not in the C# 2.0 case. |
|
I added a test, but I think I discovered a larger issue. ILSpy seems to be erasing this null check and treats it as the default case. |
- Updated `Issue3421.cs`. - Updated `MatchLegacySwitchOnStringWithDict` to check for `leaveContainer` and handle null sections accordingly. - Introduced an overload for `AddNullSection` to accept `ILInstruction` as the body, improving flexibility. - Modified existing `AddNullSection` to utilize the new overload, allowing for varied body types in `SwitchSection`.
|
I found the issue. It should be good now. |
|
Thank you for the contribution! |
Updated the condition for
nullValueCaseBlockto ensure it is not null and not equal todefaultBlock.Problem
Resolves #3421
Solution
Is a test necessary for this? If so, I'm not sure what the best way to write that test is.