Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
e8fe1af8
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
e8fe1af8
编写于
12月 16, 2019
作者:
N
Neal Gafter
提交者:
GitHub
12月 16, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
An input requires analysis using a different slot when suppressed (#39697)
Fixes #38586
上级
0b8a8c0c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
60 addition
and
2 deletion
+60
-2
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker_Patterns.cs
...s/CSharp/Portable/FlowAnalysis/NullableWalker_Patterns.cs
+2
-2
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
...rp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
+58
-0
未找到文件。
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker_Patterns.cs
浏览文件 @
e8fe1af8
...
...
@@ -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
;
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
浏览文件 @
e8fe1af8
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录