Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
6d88f91c
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,发现更多精彩内容 >>
未验证
提交
6d88f91c
编写于
12月 13, 2018
作者:
C
Charles Stoner
提交者:
GitHub
12月 13, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tests for `is` declaration pattern (#31755)
上级
816a86fc
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
378 addition
and
2 deletion
+378
-2
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
...rp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
+378
-2
未找到文件。
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
浏览文件 @
6d88f91c
...
@@ -33349,8 +33349,7 @@ static void G(string s)
...
@@ -33349,8 +33349,7 @@ static void G(string s)
comp.VerifyDiagnostics();
comp.VerifyDiagnostics();
}
}
// https://github.com/dotnet/roslyn/issues/29909: Should not warn on either call to F(string).
[Fact]
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/29909")]
[WorkItem(29909, "https://github.com/dotnet/roslyn/issues/29909")]
[WorkItem(29909, "https://github.com/dotnet/roslyn/issues/29909")]
public void IsPattern_02()
public void IsPattern_02()
{
{
...
@@ -33409,6 +33408,383 @@ static void G(object? x)
...
@@ -33409,6 +33408,383 @@ static void G(object? x)
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x").WithArguments("o", "void C.F(object o)").WithLocation(18, 19));
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x").WithArguments("o", "void C.F(object o)").WithLocation(18, 19));
}
}
[Fact]
public void IsDeclarationPattern_01()
{
// https://github.com/dotnet/roslyn/issues/30952: `is` declaration does not set not nullable for declared local.
var source =
@"class Program
{
static void F1(object x1)
{
if (x1 is string y1)
{
x1/*T:object!*/.ToString();
y1/*T:string*/.ToString();
}
x1/*T:object!*/.ToString();
}
static void F2(object? x2)
{
if (x2 is string y2)
{
x2/*T:object!*/.ToString();
y2/*T:string*/.ToString();
}
x2/*T:object?*/.ToString(); // 1
}
static void F3(object x3)
{
x3 = null; // 2
if (x3 is string y3)
{
x3/*T:object!*/.ToString();
y3/*T:string*/.ToString();
}
x3/*T:object?*/.ToString(); // 3
}
static void F4(object? x4)
{
if (x4 == null) return;
if (x4 is string y4)
{
x4/*T:object!*/.ToString();
y4/*T:string*/.ToString();
}
x4/*T:object!*/.ToString();
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (19,9): warning CS8602: Possible dereference of a null reference.
// x2.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x2").WithLocation(19, 9),
// (23,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// x3 = null; // 2
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(23, 14),
// (29,9): warning CS8602: Possible dereference of a null reference.
// x3.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x3").WithLocation(29, 9));
comp.VerifyTypes();
}
[Fact]
public void IsDeclarationPattern_02()
{
var source =
@"class Program
{
static void F1<T, U>(T t1)
where T : class
where U : class
{
if (t1 is U u1)
{
t1.ToString();
u1.ToString();
}
t1.ToString();
}
static void F2<T, U>(T t2)
where T : class?
where U : class
{
if (t2 is U u2)
{
t2.ToString();
u2.ToString();
}
t2.ToString(); // 1
}
static void F3<T, U>(T t3)
where T : class
where U : class
{
t3 = null; // 2
if (t3 is U u3)
{
t3.ToString();
u3.ToString();
}
t3.ToString(); // 3
}
static void F4<T, U>(T t4)
where T : class?
where U : class
{
if (t4 == null) return;
if (t4 is U u4)
{
t4.ToString();
u4.ToString();
}
t4.ToString();
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (23,9): warning CS8602: Possible dereference of a null reference.
// t2.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t2").WithLocation(23, 9),
// (29,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// t3 = null; // 2
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(29, 14),
// (35,9): warning CS8602: Possible dereference of a null reference.
// t3.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t3").WithLocation(35, 9));
}
[Fact]
public void IsDeclarationPattern_03()
{
var source =
@"class Program
{
static void F1<T, U>(T t1)
{
if (t1 is U u1)
{
t1.ToString();
u1.ToString();
}
t1.ToString(); // 1
}
static void F2<T, U>(T t2)
{
if (t2 == null) return;
if (t2 is U u2)
{
t2.ToString();
u2.ToString();
}
t2.ToString();
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (10,9): warning CS8602: Possible dereference of a null reference.
// t1.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t1").WithLocation(10, 9));
}
[Fact]
public void IsDeclarationPattern_NeverNull_01()
{
var source =
@"class Program
{
static void F1(object x1)
{
if (x1 is string y1)
{
x1?.ToString(); // 1
y1?.ToString(); // 2
}
x1?.ToString(); // 3
}
static void F2(object? x2)
{
if (x2 is string y2)
{
x2?.ToString(); // 4
y2?.ToString(); // 5
}
x2?.ToString();
}
}";
// https://github.com/dotnet/roslyn/issues/30952: `is` declaration does not set not nullable for declared local.
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (7,13): hidden CS8607: Expression is probably never null.
// x1?.ToString(); // 1
Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "x1").WithLocation(7, 13),
// (10,9): hidden CS8607: Expression is probably never null.
// x1?.ToString(); // 3
Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "x1").WithLocation(10, 9),
// (16,13): hidden CS8607: Expression is probably never null.
// x2?.ToString(); // 4
Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "x2").WithLocation(16, 13));
}
[Fact]
public void IsDeclarationPattern_NeverNull_02()
{
var source =
@"class Program
{
static void F1<T, U>(T t1)
where T : class
where U : class
{
if (t1 is U u1)
{
t1?.ToString(); // 1
u1?.ToString(); // 2
}
t1?.ToString(); // 3
}
static void F2<T, U>(T t2)
where T : class?
where U : class
{
if (t2 is U u2)
{
t2?.ToString(); // 4
u2?.ToString(); // 5
}
t2?.ToString();
}
}";
// https://github.com/dotnet/roslyn/issues/30952: `is` declaration does not set not nullable for declared local.
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (9,13): hidden CS8607: Expression is probably never null.
// t1?.ToString(); // 1
Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "t1").WithLocation(9, 13),
// (12,9): hidden CS8607: Expression is probably never null.
// t1?.ToString(); // 3
Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "t1").WithLocation(12, 9),
// (20,13): hidden CS8607: Expression is probably never null.
// t2?.ToString(); // 4
Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "t2").WithLocation(20, 13));
}
[Fact]
public void IsDeclarationPattern_Unassigned_01()
{
var source =
@"class Program
{
static void F1(object x1)
{
if (x1 is string y1)
{
}
else
{
x1.ToString();
y1.ToString(); // 1
}
}
static void F2(object? x2)
{
if (x2 is string y2)
{
}
else
{
x2.ToString(); // 2
y2.ToString(); // 3
}
}
static void F3(object x3)
{
x3 = null; // 4
if (x3 is string y3)
{
}
else
{
x3.ToString(); // 5
y3.ToString(); // 6
}
}
static void F4(object? x4)
{
if (x4 == null) return;
if (x4 is string y4)
{
}
else
{
x4.ToString();
y4.ToString(); // 7
}
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (11,13): error CS0165: Use of unassigned local variable 'y1'
// y1.ToString(); // 1
Diagnostic(ErrorCode.ERR_UseDefViolation, "y1").WithArguments("y1").WithLocation(11, 13),
// (21,13): warning CS8602: Possible dereference of a null reference.
// x2.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x2").WithLocation(21, 13),
// (22,13): error CS0165: Use of unassigned local variable 'y2'
// y2.ToString(); // 3
Diagnostic(ErrorCode.ERR_UseDefViolation, "y2").WithArguments("y2").WithLocation(22, 13),
// (27,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// x3 = null; // 4
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(27, 14),
// (33,13): warning CS8602: Possible dereference of a null reference.
// x3.ToString(); // 5
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x3").WithLocation(33, 13),
// (34,13): error CS0165: Use of unassigned local variable 'y3'
// y3.ToString(); // 6
Diagnostic(ErrorCode.ERR_UseDefViolation, "y3").WithArguments("y3").WithLocation(34, 13),
// (46,13): error CS0165: Use of unassigned local variable 'y4'
// y4.ToString(); // 7
Diagnostic(ErrorCode.ERR_UseDefViolation, "y4").WithArguments("y4").WithLocation(46, 13));
}
[Fact]
public void IsDeclarationPattern_Unassigned_02()
{
var source =
@"class Program
{
static void F1(object x1)
{
if (x1 is string y1) { }
x1.ToString();
y1.ToString(); // 1
}
static void F2(object? x2)
{
if (x2 is string y2) { }
x2.ToString(); // 2
y2.ToString(); // 3
}
static void F3(object x3)
{
x3 = null; // 4
if (x3 is string y3) { }
x3.ToString(); // 5
y3.ToString(); // 6
}
static void F4(object? x4)
{
if (x4 == null) return;
if (x4 is string y4) { }
x4.ToString();
y4.ToString(); // 7
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (7,9): error CS0165: Use of unassigned local variable 'y1'
// y1.ToString(); // 1
Diagnostic(ErrorCode.ERR_UseDefViolation, "y1").WithArguments("y1").WithLocation(7, 9),
// (12,9): warning CS8602: Possible dereference of a null reference.
// x2.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x2").WithLocation(12, 9),
// (13,9): error CS0165: Use of unassigned local variable 'y2'
// y2.ToString(); // 3
Diagnostic(ErrorCode.ERR_UseDefViolation, "y2").WithArguments("y2").WithLocation(13, 9),
// (17,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// x3 = null; // 4
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(17, 14),
// (19,9): warning CS8602: Possible dereference of a null reference.
// x3.ToString(); // 5
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x3").WithLocation(19, 9),
// (20,9): error CS0165: Use of unassigned local variable 'y3'
// y3.ToString(); // 6
Diagnostic(ErrorCode.ERR_UseDefViolation, "y3").WithArguments("y3").WithLocation(20, 9),
// (27,9): error CS0165: Use of unassigned local variable 'y4'
// y4.ToString(); // 7
Diagnostic(ErrorCode.ERR_UseDefViolation, "y4").WithArguments("y4").WithLocation(27, 9));
}
[Fact]
[Fact]
public void Feature()
public void Feature()
{
{
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录