Skip to content

Commit

Permalink
Respect [MemberNotNull] on local function
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Oct 10, 2024
1 parent a656d1a commit f49a448
Show file tree
Hide file tree
Showing 2 changed files with 360 additions and 25 deletions.
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)
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);
}

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

0 comments on commit f49a448

Please sign in to comment.