Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
b34f4ad9
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,发现更多精彩内容 >>
未验证
提交
b34f4ad9
编写于
3月 01, 2019
作者:
C
Charles Stoner
提交者:
GitHub
3月 01, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Handle interface members in NullableWalker.AsMemberOfType (#33764)
上级
2566316b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
325 addition
and
21 deletion
+325
-21
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
+52
-20
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
...rp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
+273
-1
未找到文件。
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
浏览文件 @
b34f4ad9
...
...
@@ -3299,38 +3299,70 @@ private static Symbol AsMemberOfType(TypeSymbol type, Symbol symbol)
}
var
symbolDef
=
symbol
.
OriginalDefinition
;
var
symbolDefContainer
=
symbolDef
.
ContainingType
;
while
(
true
)
if
(
symbolDefContainer
.
IsTupleType
)
{
return
AsMemberOfTupleType
((
TupleTypeSymbol
)
containingType
,
symbol
);
}
if
(
symbolDefContainer
.
IsAnonymousType
)
{
if
(
containingType
.
OriginalDefinition
.
Equals
(
symbolDefContainer
,
TypeCompareKind
.
AllIgnoreOptions
))
int
?
memberIndex
=
symbol
.
Kind
==
SymbolKind
.
Property
?
symbol
.
MemberIndexOpt
:
null
;
if
(!
memberIndex
.
HasValue
)
{
if
(
symbolDefContainer
.
IsTupleType
)
return
symbol
;
}
return
AnonymousTypeManager
.
GetAnonymousTypeProperty
(
containingType
,
memberIndex
.
GetValueOrDefault
());
}
if
(!
symbolDefContainer
.
IsGenericType
)
{
Debug
.
Assert
(
symbol
.
ContainingType
.
IsDefinition
);
return
symbol
;
}
if
(
symbolDefContainer
.
IsInterface
)
{
if
(
tryAsMemberOfSingleType
(
containingType
,
out
Symbol
result
))
{
return
result
;
}
foreach
(
var
@interface
in
containingType
.
AllInterfacesNoUseSiteDiagnostics
)
{
if
(
tryAsMemberOfSingleType
(
@interface
,
out
result
))
{
return
AsMemberOfTupleType
((
TupleTypeSymbol
)
containingType
,
symbol
)
;
return
result
;
}
if
(
symbolDefContainer
.
IsAnonymousType
)
}
}
else
{
while
(
true
)
{
if
(
tryAsMemberOfSingleType
(
containingType
,
out
Symbol
result
))
{
int
?
memberIndex
=
symbol
.
Kind
==
SymbolKind
.
Property
?
symbol
.
MemberIndexOpt
:
null
;
if
(!
memberIndex
.
HasValue
)
{
break
;
}
return
AnonymousTypeManager
.
GetAnonymousTypeProperty
(
containingType
,
memberIndex
.
GetValueOrDefault
());
return
result
;
}
var
result
=
symbolDef
.
SymbolAsMember
(
containingType
)
;
if
(
result
is
MethodSymbol
resultMethod
&&
resultMethod
.
IsGenericMethod
)
containingType
=
containingType
.
BaseTypeNoUseSiteDiagnostics
;
if
(
(
object
)
containingType
==
null
)
{
return
resultMethod
.
Construct
(((
MethodSymbol
)
symbol
).
TypeArguments
)
;
break
;
}
return
result
;
}
containingType
=
containingType
.
BaseTypeNoUseSiteDiagnostics
;
if
(
containingType
is
null
)
}
Debug
.
Assert
(
false
);
// If this assert fails, add an appropriate test.
return
symbol
;
bool
tryAsMemberOfSingleType
(
NamedTypeSymbol
singleType
,
out
Symbol
result
)
{
if
(!
singleType
.
OriginalDefinition
.
Equals
(
symbolDefContainer
,
TypeCompareKind
.
AllIgnoreOptions
))
{
break
;
result
=
null
;
return
false
;
}
result
=
symbolDef
.
SymbolAsMember
(
singleType
);
if
(
result
is
MethodSymbol
resultMethod
&&
resultMethod
.
IsGenericMethod
)
{
result
=
resultMethod
.
Construct
(((
MethodSymbol
)
symbol
).
TypeArguments
);
}
return
true
;
}
// https://github.com/dotnet/roslyn/issues/29967 Handle other cases such as interfaces.
return
symbol
;
}
private
static
Symbol
AsMemberOfTupleType
(
TupleTypeSymbol
tupleType
,
Symbol
symbol
)
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
浏览文件 @
b34f4ad9
...
...
@@ -80249,7 +80249,7 @@ class Program
[Fact]
[WorkItem(26628, "https://github.com/dotnet/roslyn/issues/26628")]
public void Implicit
Constructor_02
()
public void Implicit
StaticConstructor_01
()
{
var source =
@"#pragma warning disable 414
...
...
@@ -80264,6 +80264,24 @@ class Program
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(4, 23));
}
[Fact]
[WorkItem(26628, "https://github.com/dotnet/roslyn/issues/26628")]
[WorkItem(33394, "https://github.com/dotnet/roslyn/issues/33394")]
public void ImplicitStaticConstructor_02()
{
var source =
@"class C
{
C(string s) { }
static C Empty = new C(null); // warning
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (4,28): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// static C Empty = new C(null); // warning
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(4, 28));
}
[Fact]
[WorkItem(25868, "https://github.com/dotnet/roslyn/issues/25868")]
public void ByRefTarget_01()
...
...
@@ -81083,6 +81101,260 @@ static void F()
Diagnostic(ErrorCode.ERR_TaskRetNoObjectRequiredLambda, "return").WithLocation(9, 13));
}
[Fact]
[WorkItem(29967, "https://github.com/dotnet/roslyn/issues/29967")]
public void InterfaceProperties_01()
{
var source =
@"interface IA<T>
{
T A { get; }
}
interface IB<T> : IA<T>
{
T B { get; }
}
class Program
{
static IB<T> CreateB<T>(T t)
{
throw null!;
}
static void F1<T>(T x1)
{
if (x1 == null) return;
var y1 = CreateB(x1);
y1.A.ToString(); // 1
y1.B.ToString(); // 2
}
static void F2<T>() where T : class
{
T x2 = null; // 3
var y2 = CreateB(x2);
y2.ToString();
y2.A.ToString(); // 4
y2.B.ToString(); // 5
}
static void F3<T>() where T : class, new()
{
T? x3 = new T();
var y3 = CreateB(x3);
y3.A.ToString();
y3.B.ToString();
}
static void F4<T>() where T : struct
{
T? x4 = null;
var y4 = CreateB(x4);
_ = y4.A.Value; // 6
_ = y4.B.Value; // 7
}
static void F5<T>() where T : struct
{
T? x5 = new T();
var y5 = CreateB(x5);
_ = y5.A.Value; // 8
_ = y5.B.Value; // 9
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (19,9): warning CS8602: Possible dereference of a null reference.
// y1.A.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y1.A").WithLocation(19, 9),
// (20,9): warning CS8602: Possible dereference of a null reference.
// y1.B.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y1.B").WithLocation(20, 9),
// (24,16): warning CS8600: Converting null literal or possible null value to non-nullable type.
// T x2 = null; // 3
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(24, 16),
// (27,9): warning CS8602: Possible dereference of a null reference.
// y2.A.ToString(); // 4
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y2.A").WithLocation(27, 9),
// (28,9): warning CS8602: Possible dereference of a null reference.
// y2.B.ToString(); // 5
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y2.B").WithLocation(28, 9),
// (41,13): warning CS8629: Nullable value type may be null.
// _ = y4.A.Value; // 6
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y4.A").WithLocation(41, 13),
// (42,13): warning CS8629: Nullable value type may be null.
// _ = y4.B.Value; // 7
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y4.B").WithLocation(42, 13),
// (48,13): warning CS8629: Nullable value type may be null.
// _ = y5.A.Value; // 8
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y5.A").WithLocation(48, 13),
// (49,13): warning CS8629: Nullable value type may be null.
// _ = y5.B.Value; // 9
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y5.B").WithLocation(49, 13));
}
[Fact]
[WorkItem(29967, "https://github.com/dotnet/roslyn/issues/29967")]
public void InterfaceProperties_02()
{
var source =
@"interface IA<T>
{
T A { get; }
}
interface IB<T>
{
T B { get; }
}
interface IC<T, U> : IA<T>, IB<U>
{
}
class Program
{
static IC<T, U> CreateC<T, U>(T t, U u)
{
throw null!;
}
static void F<T, U>()
where T : class, new()
where U : class
{
T? x = new T();
T y = null; // 1
var xy = CreateC(x, y);
xy.A.ToString();
xy.B.ToString(); // 2
var yx = CreateC(y, x);
yx.A.ToString(); // 3
yx.B.ToString();
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (23,15): warning CS8600: Converting null literal or possible null value to non-nullable type.
// T y = null; // 1
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(23, 15),
// (26,9): warning CS8602: Possible dereference of a null reference.
// xy.B.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "xy.B").WithLocation(26, 9),
// (28,9): warning CS8602: Possible dereference of a null reference.
// yx.A.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "yx.A").WithLocation(28, 9));
}
[Fact]
[WorkItem(29967, "https://github.com/dotnet/roslyn/issues/29967")]
public void InterfaceMethods_01()
{
var source =
@"interface IA<T>
{
void A(T t);
}
interface IB<T> : IA<T>
{
void B(T t);
}
class Program
{
static IB<T> CreateB<T>(T t)
{
throw null!;
}
static void F1<T>(T x1)
{
if (x1 == null) return;
T y1 = default; // 1
var ix1 = CreateB(x1);
var iy1 = CreateB(y1);
ix1.A(y1);
ix1.B(y1);
iy1.A(x1);
iy1.B(x1);
}
static void F2<T>() where T : class, new()
{
T x2 = null; // 2
T? y2 = new T();
var ix2 = CreateB(x2);
var iy2 = CreateB(y2);
ix2.A(y2);
ix2.B(y2);
iy2.A(x2); // 3
iy2.B(x2); // 4
}
static void F3<T>() where T : struct
{
T? x3 = null;
T? y3 = new T();
var ix3 = CreateB(x3);
var iy3 = CreateB(y3);
ix3.A(y3);
ix3.B(y3);
iy3.A(x3);
iy3.B(x3);
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (18,16): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type.
// T y1 = default; // 1
Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(18, 16),
// (28,16): warning CS8600: Converting null literal or possible null value to non-nullable type.
// T x2 = null; // 2
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(28, 16),
// (34,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IA<T>.A(T t)'.
// iy2.A(x2); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IA<T>.A(T t)").WithLocation(34, 15),
// (35,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IB<T>.B(T t)'.
// iy2.B(x2); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IB<T>.B(T t)").WithLocation(35, 15));
}
[Fact]
[WorkItem(29967, "https://github.com/dotnet/roslyn/issues/29967")]
public void InterfaceMethods_02()
{
var source =
@"interface IA
{
T A<T>(T u);
}
interface IB<T>
{
T B<U>(U u);
}
interface IC<T> : IA, IB<T>
{
}
class Program
{
static IC<T> CreateC<T>(T t)
{
throw null!;
}
static void F<T, U>()
where T : class, new()
where U : class
{
T? x = new T();
U y = null; // 1
var ix = CreateC(x);
var iy = CreateC(y);
ix.A(y).ToString(); // 2
ix.B(y).ToString();
iy.A(x).ToString();
iy.B(x).ToString(); // 3
}
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (23,15): warning CS8600: Converting null literal or possible null value to non-nullable type.
// U y = null; // 1
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(23, 15),
// (26,9): warning CS8602: Possible dereference of a null reference.
// ix.A(y).ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ix.A(y)").WithLocation(26, 9),
// (29,9): warning CS8602: Possible dereference of a null reference.
// iy.B(x).ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "iy.B(x)").WithLocation(29, 9));
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/33347")]
public void NestedNullConditionalAccess()
{
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录