Skip to content

Commit

Permalink
NullableWalker.VisitSwitchStatementDispatch - visit local declaration…
Browse files Browse the repository at this point in the history
…s before the switch expression, which can cause us to "split" the state. (#56031)

Fixes #55227.
  • Loading branch information
AlekseyTs authored Aug 31, 2021
1 parent 2be2c34 commit 9ad67ca
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,17 @@ protected override LocalState VisitSwitchStatementDispatch(BoundSwitchStatement
}
}

// visit switch header
Visit(node.Expression);
var expressionState = ResultType;

DeclareLocals(node.InnerLocals);
foreach (var section in node.SwitchSections)
{
// locals can be alive across jumps in the switch sections, so we declare them early.
DeclareLocals(section.Locals);
}

// visit switch header
Visit(node.Expression);
var expressionState = ResultType;

var labelStateMap = LearnFromDecisionDag(node.Syntax, node.DecisionDag, node.Expression, expressionState, stateWhenNotNullOpt: null);
foreach (var section in node.SwitchSections)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152149,5 +152149,63 @@ ref T Passthrough(ref T value)
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "value2").WithArguments("T?", "T").WithLocation(9, 20)
);
}

[Fact, WorkItem(55227, "https://github.com/dotnet/roslyn/issues/55227")]
public void Issue55227_01()
{
var source = @"
#nullable enable

static class Program
{
public static void Main()
{
}

public static object Test(object a)
{
var isNullCheck = new global::System.Func<object, object>((p) =>
{
switch (p == null)
{
case var isnull:
return isnull;
}
});
return isNullCheck(a);
}
}
";

CreateCompilation(source).VerifyDiagnostics();
}

[Fact, WorkItem(55227, "https://github.com/dotnet/roslyn/issues/55227")]
public void Issue55227_02()
{
var source = @"
#nullable enable

static class Program
{
public static void Main()
{
}

public static object Test(object a)
{
var isNullCheck = new global::System.Func<object, object>((p) =>
(p == null) switch
{
var isnull => isnull
}
);
return isNullCheck(a);
}
}
";

CreateCompilation(source).VerifyDiagnostics();
}
}
}

0 comments on commit 9ad67ca

Please sign in to comment.