Skip to content
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

Respect [MemberNotNull] on local functions #75448

Merged
merged 14 commits into from
Oct 31, 2024
26 changes: 16 additions & 10 deletions src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ int makeSlot(BoundExpression node)
case BoundKind.ThisReference:
case BoundKind.BaseReference:
{
var method = getTopLevelMethod(_symbol as MethodSymbol);
var method = GetTopLevelMethod(_symbol as MethodSymbol);
var thisParameter = method?.ThisParameter;
return thisParameter is object ? GetOrCreateSlot(thisParameter) : -1;
}
Expand Down Expand Up @@ -2090,20 +2090,20 @@ int getPlaceholderSlot(BoundExpression expr)
}
return -1;
}
}

static MethodSymbol? getTopLevelMethod(MethodSymbol? method)
private static MethodSymbol? GetTopLevelMethod(MethodSymbol? method)
{
while (method is object)
{
while (method is object)
var container = method.ContainingSymbol;
if (container.Kind == SymbolKind.NamedType)
{
var container = method.ContainingSymbol;
if (container.Kind == SymbolKind.NamedType)
{
return method;
}
method = container as MethodSymbol;
return method;
}
return null;
method = container as MethodSymbol;
}
return null;
}

protected override int GetOrCreateSlot(Symbol symbol, int containingSlot = 0, bool forceSlotEvenIfEmpty = false, bool createIfMissing = true)
Expand Down Expand Up @@ -7012,6 +7012,12 @@ private void ApplyMemberPostConditions(BoundExpression? receiverOpt, MethodSymbo
receiverOpt is null ? -1 :
MakeSlot(receiverOpt);

if (method.MethodKind == MethodKind.LocalFunction
&& GetTopLevelMethod(method) is { ThisParameter: { } thisParameter })
{
receiverSlot = GetOrCreateSlot(thisParameter);
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
}

if (receiverSlot < 0)
{
return;
Expand Down
Loading