-
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
Remove CapturedVariablesByLambda #20878
Changes from all commits
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 |
---|---|---|
|
@@ -328,22 +328,20 @@ protected override NamedTypeSymbol ContainingType | |
/// </summary> | ||
private void MakeFrames(ArrayBuilder<ClosureDebugInfo> closureDebugInfo) | ||
{ | ||
var closures = _analysis.CapturedVariablesByLambda.Keys; | ||
|
||
foreach (var closure in closures) | ||
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. Is this why the names changed? We are traversing closures in different order? I think this is ok. 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. Agreed |
||
Analysis.VisitClosures(_analysis.ScopeTree, (scope, closure) => | ||
{ | ||
var capturedVars = _analysis.CapturedVariablesByLambda[closure]; | ||
bool canTakeRefParams = _analysis.CanTakeRefParameters(closure); | ||
var capturedVars = closure.CapturedVariables; | ||
MethodSymbol closureSymbol = closure.OriginalMethodSymbol; | ||
bool canTakeRefParams = _analysis.CanTakeRefParameters(closureSymbol); | ||
|
||
if (canTakeRefParams && OnlyCapturesThis((LocalFunctionSymbol)closure, capturedVars)) | ||
if (canTakeRefParams && OnlyCapturesThis(closure, scope)) | ||
{ | ||
continue; | ||
return; | ||
} | ||
|
||
foreach (var captured in capturedVars) | ||
{ | ||
BoundNode scope; | ||
if (!_analysis.VariableScope.TryGetValue(captured, out scope)) | ||
if (!_analysis.VariableScope.TryGetValue(captured, out BoundNode captureScope)) | ||
{ | ||
continue; | ||
} | ||
|
@@ -359,7 +357,7 @@ private void MakeFrames(ArrayBuilder<ClosureDebugInfo> closureDebugInfo) | |
continue; | ||
} | ||
|
||
LambdaFrame frame = GetFrameForScope(scope, closureDebugInfo); | ||
LambdaFrame frame = GetFrameForScope(captureScope, closureDebugInfo); | ||
|
||
if (captured.Kind != SymbolKind.Method && !proxies.ContainsKey(captured)) | ||
{ | ||
|
@@ -377,30 +375,31 @@ private void MakeFrames(ArrayBuilder<ClosureDebugInfo> closureDebugInfo) | |
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
||
private SmallDictionary<LocalFunctionSymbol, bool> _onlyCapturesThisMemoTable; | ||
private SmallDictionary<Analysis.Closure, bool> _onlyCapturesThisMemoTable; | ||
/// <summary> | ||
/// Helper for determining whether a local function transitively | ||
/// only captures this (only captures this or other local functions | ||
/// which only capture this). | ||
/// </summary> | ||
private bool OnlyCapturesThis<T>( | ||
LocalFunctionSymbol closure, | ||
T capturedVars, | ||
private bool OnlyCapturesThis( | ||
Analysis.Closure closure, | ||
Analysis.Scope scope, | ||
PooledHashSet<LocalFunctionSymbol> localFuncsInProgress = null) | ||
where T : IEnumerable<Symbol> | ||
{ | ||
Debug.Assert(closure != null); | ||
Debug.Assert(scope != null); | ||
|
||
bool result = false; | ||
if (_onlyCapturesThisMemoTable?.TryGetValue(closure, out result) == true) | ||
{ | ||
return result; | ||
} | ||
|
||
result = true; | ||
foreach (var captured in capturedVars) | ||
foreach (var captured in closure.CapturedVariables) | ||
{ | ||
var param = captured as ParameterSymbol; | ||
if (param != null && param.IsThis) | ||
|
@@ -423,10 +422,8 @@ private bool OnlyCapturesThis<T>( | |
} | ||
|
||
localFuncsInProgress.Add(localFunc); | ||
bool transitivelyTrue = OnlyCapturesThis( | ||
localFunc, | ||
_analysis.CapturedVariablesByLambda[localFunc], | ||
localFuncsInProgress); | ||
var (found, foundScope) = Analysis.GetVisibleClosure(scope, localFunc); | ||
bool transitivelyTrue = OnlyCapturesThis(found, foundScope, localFuncsInProgress); | ||
|
||
if (freePool) | ||
{ | ||
|
@@ -446,7 +443,7 @@ private bool OnlyCapturesThis<T>( | |
|
||
if (_onlyCapturesThisMemoTable == null) | ||
{ | ||
_onlyCapturesThisMemoTable = new SmallDictionary<LocalFunctionSymbol, bool>(); | ||
_onlyCapturesThisMemoTable = new SmallDictionary<Analysis.Closure, bool>(); | ||
} | ||
|
||
_onlyCapturesThisMemoTable[closure] = result; | ||
|
@@ -474,7 +471,7 @@ private LambdaFrame GetFrameForScope(BoundNode scope, ArrayBuilder<ClosureDebugI | |
frame = new LambdaFrame(_topLevelMethod, containingMethod, canBeStruct, syntax, methodId, closureId); | ||
_frames.Add(scope, frame); | ||
|
||
CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(this.ContainingType, frame); | ||
CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(ContainingType, frame); | ||
if (frame.Constructor != null) | ||
{ | ||
AddSynthesizedMethod( | ||
|
@@ -1335,7 +1332,7 @@ private SynthesizedLambdaMethod RewriteLambdaOrLocalFunction( | |
closureOrdinal = containerAsFrame.ClosureOrdinal; | ||
} | ||
} | ||
else if (_analysis.CapturedVariablesByLambda[node.Symbol].Count == 0) | ||
else if (Analysis.GetClosureInTree(_analysis.ScopeTree, node.Symbol).CapturedVariables.Count == 0) | ||
{ | ||
if (_analysis.MethodsConvertedToDelegates.Contains(node.Symbol)) | ||
{ | ||
|
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.
Delete the
private set
. It's unnecessary since you only modify the value in the ctor. Makes it clear this is areadonly
property.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.
? It's set in the visitor, not in the constructor. I could set it in the constructor but that's a dangerous dance as I would be handing out
this
before fully constructed.