Skip to content

Commit f0f95ef

Browse files
Fix StateRangeAnalysis to handle changes in yield return codegen in Roslyn 4.13.
1 parent 3e5e81e commit f0f95ef

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

ICSharpCode.Decompiler/IL/ControlFlow/StateRangeAnalysis.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
using System;
2222
using System.Collections.Generic;
2323
using System.Diagnostics;
24-
using System.Diagnostics.CodeAnalysis;
2524
using System.Linq;
26-
using System.Text;
2725
using System.Threading;
28-
using System.Threading.Tasks;
2926

3027
using ICSharpCode.Decompiler.TypeSystem;
3128
using ICSharpCode.Decompiler.Util;
@@ -225,6 +222,32 @@ public LongSet AssignStateRanges(ILInstruction inst, LongSet stateRange)
225222
goto default;
226223
}
227224
}
225+
case StObj stobj when mode == StateRangeAnalysisMode.IteratorDispose:
226+
{
227+
if (stobj.MatchStFld(out var target, out var field, out var value) && target.MatchLdThis())
228+
{
229+
if (field.MemberDefinition == stateField && value.MatchLdcI4(-2))
230+
{
231+
// Roslyn 4.13 sets the state field in Dispose() to mark the iterator as disposed,
232+
// don't consider this user code.
233+
return stateRange;
234+
}
235+
else if (value.MatchDefaultOrNullOrZero())
236+
{
237+
// Roslyn 4.13 clears any local hoisted local variables in Dispose(),
238+
// don't consider this user code.
239+
return stateRange;
240+
}
241+
else
242+
{
243+
goto default;
244+
}
245+
}
246+
else
247+
{
248+
goto default;
249+
}
250+
}
228251
default:
229252
// User code - abort analysis
230253
if (mode == StateRangeAnalysisMode.IteratorDispose && !(inst is Leave l && l.IsLeavingFunction))

0 commit comments

Comments
 (0)