未验证 提交 e8fe1af8 编写于 作者: N Neal Gafter 提交者: GitHub

An input requires analysis using a different slot when suppressed (#39697)

Fixes #38586
上级 0b8a8c0c
......@@ -142,7 +142,7 @@ public override BoundNode VisitITuplePattern(BoundITuplePattern node)
protected override LocalState VisitSwitchStatementDispatch(BoundSwitchStatement node)
{
// first, learn from any null tests in the patterns
int slot = MakeSlot(node.Expression);
int slot = node.Expression.IsSuppressed ? GetOrCreatePlaceholderSlot(node.Expression) : MakeSlot(node.Expression);
if (slot > 0)
{
var originalInputType = node.Expression.Type;
......@@ -493,7 +493,7 @@ public override BoundNode VisitUnconvertedSwitchExpression(BoundUnconvertedSwitc
private void VisitSwitchExpressionCore(BoundSwitchExpression node, bool inferType)
{
// first, learn from any null tests in the patterns
int slot = MakeSlot(node.Expression);
int slot = node.Expression.IsSuppressed ? GetOrCreatePlaceholderSlot(node.Expression) : MakeSlot(node.Expression);
if (slot > 0)
{
var originalInputType = node.Expression.Type;
......
......@@ -120001,5 +120001,63 @@ public static void M()
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s4.field").WithLocation(21, 9)
);
}
[Fact]
[WorkItem(38586, "https://github.com/dotnet/roslyn/issues/38586")]
public void SuppressSwitchExpressionInput()
{
var source =
@"#nullable enable
public class C {
public int M0(C a) => a switch { C _ => 0 }; // ok
public int M1(C? a) => a switch { C _ => 0 }; // warns
public int M2(C? a) => a! switch { C _ => 0 }; // ok
public int M3(C a) => (1, a) switch { (_, C _) => 0 }; // ok
public int M4(C? a) => (1, a) switch { (_, C _) => 0 }; // warns
public int M5(C? a) => (1, a!) switch { (_, C _) => 0 }; // ok
public void M6(C? a, bool b)
{
if (a == null) return;
switch (a!)
{
case C _:
break;
case null: // does not affect knowledge of 'a'
break;
}
a.ToString();
}
public void M7(C? a, bool b)
{
if (a == null) return;
switch (a)
{
case C _:
break;
case null: // affects knowledge of a
break;
}
a.ToString(); // warns
}
}
";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (4,30): warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive).
// public int M1(C? a) => a switch { C _ => 0 }; // warns
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull, "switch").WithLocation(4, 30),
// (8,35): warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive).
// public int M4(C? a) => (1, a) switch { (_, C _) => 0 }; // warns
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull, "switch").WithLocation(8, 35),
// (36,9): warning CS8602: Dereference of a possibly null reference.
// a.ToString(); // warns
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a").WithLocation(36, 9)
);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册