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

Fix "path cost" calculation when computing the shortest path to the exit of a decision dag (#47407)

in the face of multiple when clauses, for the purpose of constructing the "not exhaustive" diagnostic.
Fixes #47164
上级 a68269fe
......@@ -52,10 +52,11 @@ int distance(BoundDecisionDagNode x)
BoundEvaluationDecisionDagNode e => (distance(e.Next), e.Next),
BoundTestDecisionDagNode { Test: BoundDagNonNullTest _ } t when !nullPaths => (1 + distance(t.WhenTrue), t.WhenTrue),
BoundTestDecisionDagNode { Test: BoundDagExplicitNullTest _ } t when !nullPaths => (1 + distance(t.WhenFalse), t.WhenFalse),
BoundTestDecisionDagNode t when distance(t.WhenTrue) is var trueDist && distance(t.WhenFalse) is var falseDist =>
(trueDist <= falseDist) ? (1 + trueDist, t.WhenTrue) : (1 + falseDist, t.WhenFalse),
BoundWhenDecisionDagNode w when distance(w.WhenTrue) is var trueDist && distance(w.WhenFalse) is var falseDist =>
(trueDist <= falseDist) ? (1 + trueDist, w.WhenTrue) : (1 + falseDist + nodeCount, w.WhenFalse),
BoundTestDecisionDagNode t when distance(t.WhenTrue) is var trueDist1 && distance(t.WhenFalse) is var falseDist1 =>
(trueDist1 <= falseDist1) ? (1 + trueDist1, t.WhenTrue) : (1 + falseDist1, t.WhenFalse),
BoundWhenDecisionDagNode w when distance(w.WhenTrue) is var trueDist2 && distance(w.WhenFalse) is var falseDist2 =>
// add nodeCount to the distance if we need to flag that the path requires failure of a when clause
(trueDist2 <= falseDist2) ? (1 + trueDist2, w.WhenTrue) : (1 + (falseDist2 < nodeCount ? nodeCount : 0) + falseDist2, w.WhenFalse),
// treat the endpoint as distance 1.
// treat other nodes as not on the path to the endpoint
_ => ((n == node) ? 1 : infinity, null),
......
......@@ -3165,5 +3165,83 @@ void M()
Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "false").WithLocation(49, 16)
);
}
[Fact, WorkItem(47164, "https://github.com/dotnet/roslyn/issues/47164")]
public void MultipleWhenClausesToFailure_01()
{
var source =
@"class Sample
{
void M(int q)
{
_ = q switch
{
4 => 1,
5 => 2,
6 => 3,
int i when i % 2 == 1 => 4,
int i when i % 2 == 0 => 5,
};
}
}";
var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
compilation.VerifyEmitDiagnostics(
// (5,15): warning CS8846: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '0' is not covered. However, a pattern with a 'when' clause might successfully match this value.
// _ = q switch
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustiveWithWhen, "switch").WithArguments("0").WithLocation(5, 15)
);
}
[Fact, WorkItem(47164, "https://github.com/dotnet/roslyn/issues/47164")]
public void MultipleWhenClausesToFailure_02()
{
var source =
@"class Sample
{
void M(int q)
{
_ = q switch
{
4 => 1,
5 => 2,
6 => 3,
int i when i % 2 == 1 => 4,
};
}
}";
var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
compilation.VerifyEmitDiagnostics(
// (5,15): warning CS8846: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '0' is not covered. However, a pattern with a 'when' clause might successfully match this value.
// _ = q switch
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustiveWithWhen, "switch").WithArguments("0").WithLocation(5, 15)
);
}
[Fact, WorkItem(47164, "https://github.com/dotnet/roslyn/issues/47164")]
public void MultipleWhenClausesToFailure_03()
{
var source =
@"class Sample
{
void M(int q)
{
_ = q switch
{
4 => 1,
5 => 2,
6 => 3,
int i when i % 3 == 0 => 4,
int i when i % 3 == 1 => 5,
int i when i % 3 == 2 => 6,
};
}
}";
var compilation = CreateCompilation(source, options: TestOptions.ReleaseDll);
compilation.VerifyEmitDiagnostics(
// (5,15): warning CS8846: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '0' is not covered. However, a pattern with a 'when' clause might successfully match this value.
// _ = q switch
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustiveWithWhen, "switch").WithArguments("0").WithLocation(5, 15)
);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册