-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clear hoisted locals when disposing iterator and async-iterator state machines #75908
Changes from 5 commits
accbe6c
04902e1
61ca295
e19d7ab
386a056
cdcadd0
e170504
0d40220
b52733e
3ac0137
37d3b3d
e948e85
b1a5ebb
fdd444c
3eb020f
58ab13e
0df2e4b
0f63ecd
5529fd6
57cafd7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,7 +160,11 @@ internal void GenerateMoveNextAndDispose(BoundStatement body, SynthesizedImpleme | |
if (rootFrame.knownStates == null) | ||
{ | ||
// nothing to finalize | ||
F.CloseMethod(F.Return()); | ||
var disposeBody = F.Block( | ||
GenerateAllHoistedLocalsCleanup(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, see the diff between commits 1 and 2 for VerifyIL differences. |
||
F.Return()); | ||
|
||
F.CloseMethod(disposeBody); | ||
} | ||
else | ||
{ | ||
|
@@ -171,6 +175,7 @@ internal void GenerateMoveNextAndDispose(BoundStatement body, SynthesizedImpleme | |
ImmutableArray.Create<LocalSymbol>(stateLocal), | ||
F.Assignment(F.Local(stateLocal), F.Field(F.This(), stateField)), | ||
EmitFinallyFrame(rootFrame, state), | ||
GenerateAllHoistedLocalsCleanup(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. See |
||
F.Return()); | ||
|
||
F.CloseMethod(disposeBody); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ internal abstract class MethodToStateMachineRewriter : MethodToClassRewriter | |
/// Note that there is a dispatch occurring at every try-finally statement, so this | ||
/// variable takes on a new set of values inside each try block. | ||
/// </summary> | ||
private Dictionary<LabelSymbol, List<StateMachineState>> _dispatches = new(); | ||
private Dictionary<LabelSymbol, List<StateMachineState>> _dispatches = new Dictionary<LabelSymbol, List<StateMachineState>>(); | ||
|
||
/// <summary> | ||
/// A pool of fields used to hoist locals. They appear in this set when not in scope, | ||
|
@@ -75,11 +75,6 @@ internal abstract class MethodToStateMachineRewriter : MethodToClassRewriter | |
/// </summary> | ||
private int _nextHoistedFieldId = 1; | ||
|
||
/// <summary> | ||
/// Used to enumerate the instance fields of a struct. | ||
/// </summary> | ||
private readonly EmptyStructTypeCache _emptyStructTypeCache = EmptyStructTypeCache.CreateNeverEmpty(); | ||
|
||
/// <summary> | ||
/// The set of local variables and parameters that were hoisted and need a proxy. | ||
/// </summary> | ||
|
@@ -434,6 +429,50 @@ private void AddVariableCleanup(ArrayBuilder<BoundExpression> cleanup, FieldSymb | |
} | ||
} | ||
|
||
#nullable enable | ||
protected BoundBlock GenerateAllHoistedLocalsCleanup() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code looks very different from original implementation of |
||
{ | ||
var variableCleanup = ArrayBuilder<BoundExpression>.GetInstance(); | ||
var alreadyCleaned = PooledHashSet<string>.GetInstance(); | ||
foreach (var symbol in HoistedVariables) | ||
jcouv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (symbol is LocalSymbol | ||
&& proxies.TryGetValue(symbol, out CapturedSymbolReplacement? replacement)) | ||
{ | ||
Debug.Assert(replacement is CapturedToStateMachineFieldReplacement or CapturedToExpressionSymbolReplacement); | ||
|
||
if (replacement is CapturedToStateMachineFieldReplacement fieldReplacement) | ||
{ | ||
addVariableCleanupOnce(variableCleanup, fieldReplacement.HoistedField, alreadyCleaned); | ||
} | ||
else if (replacement is CapturedToExpressionSymbolReplacement expressionReplacement) | ||
{ | ||
foreach (var field in expressionReplacement.HoistedFields) | ||
jcouv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
addVariableCleanupOnce(variableCleanup, field, alreadyCleaned); | ||
} | ||
} | ||
} | ||
} | ||
|
||
var result = F.Block(variableCleanup.SelectAsArray((e, f) => (BoundStatement)f.ExpressionStatement(e), F)); | ||
|
||
variableCleanup.Free(); | ||
alreadyCleaned.Free(); | ||
|
||
return result; | ||
|
||
void addVariableCleanupOnce(ArrayBuilder<BoundExpression> variableCleanup, FieldSymbol field, PooledHashSet<string> alreadyCleaned) | ||
{ | ||
// Hoisted fields may be re-used, so we can skip those that were cleared already | ||
if (alreadyCleaned.Add(field.Name)) | ||
{ | ||
AddVariableCleanup(variableCleanup, field); | ||
} | ||
} | ||
} | ||
#nullable disable | ||
|
||
private StateMachineFieldSymbol GetOrAllocateReusableHoistedField(TypeSymbol type, out bool reused, LocalSymbol local = null) | ||
{ | ||
ArrayBuilder<StateMachineFieldSymbol> fields; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't obvious. It looks like there are two call sites of this method and it is not clear why we want to have this behavior for all of them. Instead of completely ignoring the passed argument, consider adjusting the call sites to construct the right set of locals to clear. We can discuss offline in more details. #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behavior could be a source of bugs in the future, since the method doesn't do what a reasonable dev would expect it to do.