Skip to content

Commit e4000c8

Browse files
committed
Enhance null handling in switch transformations
- 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`.
1 parent a1b3b14 commit e4000c8

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3421.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ internal class Issue3421
55

66
public virtual void SetValue(object value)
77
{
8-
if (name == null)
9-
{
10-
return;
11-
}
128
switch (name)
139
{
1410
case "##Name##":
@@ -19,6 +15,8 @@ public virtual void SetValue(object value)
1915
case "##InnerText##":
2016
this.value = value.ToString();
2117
return;
18+
case null:
19+
return;
2220
}
2321
if (this.value == null)
2422
{

ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,13 @@ bool MatchLegacySwitchOnStringWithDict(InstructionCollection<ILInstruction> inst
675675
return false;
676676
}
677677
}
678+
else if (leaveContainer != null && !defaultBlockJump.MatchLeave(leaveContainer))
679+
{
680+
if (!AddNullSection(sections, stringValues, (Leave)exitBlockJump))
681+
{
682+
return false;
683+
}
684+
}
678685
context.Step(nameof(MatchLegacySwitchOnStringWithDict), instructions[i]);
679686
bool keepAssignmentBefore = false;
680687
if (switchValueVar.LoadCount > 2 || switchValue == null)
@@ -741,6 +748,11 @@ bool HasLabel(SwitchSection section)
741748
}
742749

743750
bool AddNullSection(List<SwitchSection> sections, List<(string Value, int Index)> stringValues, Block nullValueCaseBlock)
751+
{
752+
return AddNullSection(sections, stringValues, new Branch(nullValueCaseBlock));
753+
}
754+
755+
bool AddNullSection(List<SwitchSection> sections, List<(string Value, int Index)> stringValues, ILInstruction body)
744756
{
745757
var label = new LongSet(stringValues.Max(item => item.Index) + 1);
746758
var possibleConflicts = sections.Where(sec => sec.Labels.Overlaps(label)).ToArray();
@@ -753,7 +765,7 @@ bool AddNullSection(List<SwitchSection> sections, List<(string Value, int Index)
753765
possibleConflicts[0].Labels = possibleConflicts[0].Labels.ExceptWith(label);
754766
}
755767
stringValues.Add((null, (int)label.Values.First()));
756-
sections.Add(new SwitchSection() { Labels = label, Body = new Branch(nullValueCaseBlock) });
768+
sections.Add(new SwitchSection() { Labels = label, Body = body });
757769
return true;
758770
}
759771

0 commit comments

Comments
 (0)