Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
51917b53
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,发现更多精彩内容 >>
未验证
提交
51917b53
编写于
9月 18, 2020
作者:
R
Rikki Gibson
提交者:
GitHub
9月 18, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix MemberNotNull attributes on substituted methods (#47818)
上级
7dad16ec
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
274 addition
and
0 deletion
+274
-0
src/Compilers/CSharp/Portable/Symbols/Wrapped/WrappedMethodSymbol.cs
...rs/CSharp/Portable/Symbols/Wrapped/WrappedMethodSymbol.cs
+6
-0
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
...rp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
+268
-0
未找到文件。
src/Compilers/CSharp/Portable/Symbols/Wrapped/WrappedMethodSymbol.cs
浏览文件 @
51917b53
...
...
@@ -323,6 +323,12 @@ internal override bool HasRuntimeSpecialName
public
sealed
override
FlowAnalysisAnnotations
FlowAnalysisAnnotations
=>
UnderlyingMethod
.
FlowAnalysisAnnotations
;
internal
sealed
override
ImmutableArray
<
string
>
NotNullMembers
=>
UnderlyingMethod
.
NotNullMembers
;
internal
sealed
override
ImmutableArray
<
string
>
NotNullWhenTrueMembers
=>
UnderlyingMethod
.
NotNullWhenTrueMembers
;
internal
sealed
override
ImmutableArray
<
string
>
NotNullWhenFalseMembers
=>
UnderlyingMethod
.
NotNullWhenFalseMembers
;
internal
override
bool
ReturnValueIsMarshalledExplicitly
{
get
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
浏览文件 @
51917b53
...
...
@@ -22500,6 +22500,274 @@ public C()
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNullWhenTrue_Generic_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C<T>
{
public T field1;
public T? field2;
public T field3;
public T? field4;
public C()
{
if (Init())
{
field1.ToString();
field2.ToString();
field3.ToString(); // 1
field4.ToString(); // 2
}
else
{
throw null!;
}
}
[MemberNotNullWhen(true, nameof(field1), nameof(field2))]
bool Init() => throw null!;
}
", MemberNotNullWhenAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (16,13): warning CS8602: Dereference of a possibly null reference.
// field3.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field3").WithLocation(16, 13),
// (17,13): warning CS8602: Dereference of a possibly null reference.
// field4.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field4").WithLocation(17, 13)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNullWhenTrue_Generic_02()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
class GetResult<T>
{
[MemberNotNullWhen(true, ""Value"")]
public bool OK { get; set; }
public T? Value { get; init; }
}
record Manager(int Age);
class Archive
{
readonly Dictionary<string, Manager> Dict = new Dictionary<string, Manager>();
public GetResult<Manager> this[string key]
=> Dict.TryGetValue(key, out var value)
? new GetResult<Manager> { OK = true, Value = value }
: new GetResult<Manager> { OK = false, Value = null };
}
public class C
{
public void M()
{
Archive archive = new Archive();
var result = archive[""John""];
int age1 = result.OK
? result.Value.Age
: result.Value.Age; // 1
}
}
", MemberNotNullWhenAttributeDefinition, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (33,15): warning CS8602: Dereference of a possibly null reference.
// : result.Value.Age; // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "result.Value", isSuppressed: false).WithLocation(33, 15)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNullWhenFalse_Generic_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C<T> where T : new()
{
[MemberNotNullWhen(false, ""Value"")]
public bool IsBad { get; set; }
public T? Value { get; set; }
}
public class Program
{
public void M(C<object> c)
{
_ = c.IsBad
? c.Value.ToString() // 1
: c.Value.ToString();
}
}
", MemberNotNullWhenAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (16,15): warning CS8602: Dereference of a possibly null reference.
// ? c.Value.ToString() // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c.Value", isSuppressed: false).WithLocation(16, 15)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNull_Generic_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C<T> where T : new()
{
[MemberNotNull(""Value"")]
public void Init() { Value = new T(); }
public T? Value { get; set; }
}
public class Program
{
public void M(bool b, C<object> c)
{
if (b) c.Value.ToString(); // 1
c.Init();
c.Value.ToString();
}
}
", MemberNotNullAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (15,16): warning CS8602: Dereference of a possibly null reference.
// if (b) c.Value.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c.Value", isSuppressed: false).WithLocation(15, 16)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNull_Extension_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C
{
public string? _field;
}
public static class Ext
{
public static string? _field;
[MemberNotNull(""_field"")]
public static void AssertFieldNotNull(this C c) { if (_field == null) throw null!; }
}
class Program
{
void M1(C c)
{
c.AssertFieldNotNull();
Ext._field.ToString();
c._field.ToString(); // 1
}
void M2(C c)
{
Ext.AssertFieldNotNull(c);
Ext._field.ToString();
c._field.ToString(); // 2
}
}
", MemberNotNullAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (23,9): warning CS8602: Dereference of a possibly null reference.
// c._field.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(23, 9),
// (30,9): warning CS8602: Dereference of a possibly null reference.
// c._field.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(30, 9)
);
}
[Fact, WorkItem(47667, "https://github.com/dotnet/roslyn/issues/47667")]
public void MemberNotNullWhen_Extension_01()
{
var c = CreateNullableCompilation(new[] { @"
using System.Diagnostics.CodeAnalysis;
public class C
{
public string? _field;
}
public static class Ext
{
public static string? _field;
[MemberNotNullWhen(true, ""_field"")]
public static bool IsFieldPresent(this C c) { return _field != null; }
[MemberNotNullWhen(false, ""_field"")]
public static bool IsFieldAbsent(this C c) { return _field == null; }
}
class Program
{
void M1(C c)
{
_ = c.IsFieldPresent()
? Ext._field.ToString()
: Ext._field.ToString(); // 1
_ = c.IsFieldPresent()
? c._field.ToString() // 2
: c._field.ToString(); // 3
}
void M2(C c)
{
_ = c.IsFieldAbsent()
? Ext._field.ToString() // 4
: Ext._field.ToString();
_ = c.IsFieldAbsent()
? c._field.ToString() // 5
: c._field.ToString(); // 6
}
}
", MemberNotNullWhenAttributeDefinition }, parseOptions: TestOptions.Regular9);
c.VerifyDiagnostics(
// (26,15): warning CS8602: Dereference of a possibly null reference.
// : Ext._field.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Ext._field", isSuppressed: false).WithLocation(26, 15),
// (29,15): warning CS8602: Dereference of a possibly null reference.
// ? c._field.ToString() // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(29, 15),
// (30,15): warning CS8602: Dereference of a possibly null reference.
// : c._field.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(30, 15),
// (36,15): warning CS8602: Dereference of a possibly null reference.
// ? Ext._field.ToString() // 4
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Ext._field", isSuppressed: false).WithLocation(36, 15),
// (40,15): warning CS8602: Dereference of a possibly null reference.
// ? c._field.ToString() // 5
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(40, 15),
// (41,15): warning CS8602: Dereference of a possibly null reference.
// : c._field.ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c._field", isSuppressed: false).WithLocation(41, 15)
);
}
[Fact]
public void MemberNotNullWhenTrue_NonConstantBool()
{
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录