Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
2c5e11c3
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,发现更多精彩内容 >>
未验证
提交
2c5e11c3
编写于
8月 07, 2020
作者:
C
Charles Stoner
提交者:
GitHub
8月 07, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Consider nullability in conversion to constraint type (#46405)
上级
e79e45e0
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
606 addition
and
70 deletion
+606
-70
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
+66
-11
src/Compilers/CSharp/Portable/Symbols/ConstraintsHelper.cs
src/Compilers/CSharp/Portable/Symbols/ConstraintsHelper.cs
+2
-2
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
...rp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
+538
-57
未找到文件。
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
浏览文件 @
2c5e11c3
...
...
@@ -6263,7 +6263,7 @@ private bool HasTopLevelNullabilityConversion(TypeWithAnnotations source, TypeWi
break
;
case
ConversionKind
.
Boxing
:
resultState
=
getBoxingConversionResultState
(
operandType
);
resultState
=
getBoxingConversionResultState
(
targetTypeWithNullability
,
operandType
);
break
;
case
ConversionKind
.
Unboxing
:
...
...
@@ -6473,10 +6473,19 @@ static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations t
switch
(
state
)
{
case
NullableFlowState
.
MaybeNull
:
if
(
operandType
.
Type
?.
IsTypeParameterDisallowingAnnotationInCSharp8
()
!=
true
&&
targetType
.
Type
?.
IsTypeParameterDisallowingAnnotationInCSharp8
()
==
true
)
if
(
targetType
.
Type
?.
IsTypeParameterDisallowingAnnotationInCSharp8
()
==
true
)
{
return
NullableFlowState
.
MaybeDefault
;
var
type
=
operandType
.
Type
;
if
(
type
is
null
||
!
type
.
IsTypeParameterDisallowingAnnotationInCSharp8
())
{
return
NullableFlowState
.
MaybeDefault
;
}
else
if
(
targetType
.
NullableAnnotation
.
IsNotAnnotated
()
&&
type
is
TypeParameterSymbol
typeParameter1
&&
dependsOnTypeParameter
(
typeParameter1
,
(
TypeParameterSymbol
)
targetType
.
Type
,
NullableAnnotation
.
NotAnnotated
,
out
var
annotation
))
{
return
(
annotation
==
NullableAnnotation
.
Annotated
)
?
NullableFlowState
.
MaybeDefault
:
NullableFlowState
.
MaybeNull
;
}
}
break
;
case
NullableFlowState
.
MaybeDefault
:
...
...
@@ -6490,11 +6499,31 @@ static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations t
}
// Converting to a less-derived type (object, interface, type parameter).
// If the operand is MaybeNull
or MaybeDefault
, the result should be
// If the operand is MaybeNull, the result should be
// MaybeNull (if the target type allows) or MaybeDefault otherwise.
static
NullableFlowState
getBoxingConversionResultState
(
TypeWithState
operandType
)
static
NullableFlowState
getBoxingConversionResultState
(
TypeWith
Annotations
targetType
,
TypeWith
State
operandType
)
{
return
getConversionResultState
(
operandType
);
var
state
=
operandType
.
State
;
if
(
state
==
NullableFlowState
.
MaybeNull
)
{
var
type
=
operandType
.
Type
;
if
(
type
is
null
||
!
type
.
IsTypeParameterDisallowingAnnotationInCSharp8
())
{
return
NullableFlowState
.
MaybeDefault
;
}
else
if
(
targetType
.
NullableAnnotation
.
IsNotAnnotated
()
&&
type
is
TypeParameterSymbol
typeParameter1
&&
targetType
.
Type
is
TypeParameterSymbol
typeParameter2
)
{
bool
dependsOn
=
dependsOnTypeParameter
(
typeParameter1
,
typeParameter2
,
NullableAnnotation
.
NotAnnotated
,
out
var
annotation
);
Debug
.
Assert
(
dependsOn
);
// If this case fails, add a corresponding test.
if
(
dependsOn
)
{
return
(
annotation
==
NullableAnnotation
.
Annotated
)
?
NullableFlowState
.
MaybeDefault
:
NullableFlowState
.
MaybeNull
;
}
}
}
return
state
;
}
// Converting to a more-derived type (struct, class, type parameter).
...
...
@@ -6515,13 +6544,39 @@ static NullableFlowState getConversionResultState(TypeWithState operandType)
var
state
=
operandType
.
State
;
if
(
state
==
NullableFlowState
.
MaybeNull
)
{
var
type
=
operandType
.
Type
;
if
(
type
is
null
||
!
type
.
IsTypeParameterDisallowingAnnotationInCSharp8
())
return
NullableFlowState
.
MaybeDefault
;
}
return
state
;
}
// If type parameter 1 depends on type parameter 2 (that is, if type parameter 2 appears
// in the constraint types of type parameter 1), returns the effective annotation on
// type parameter 2 in the constraints of type parameter 1.
static
bool
dependsOnTypeParameter
(
TypeParameterSymbol
typeParameter1
,
TypeParameterSymbol
typeParameter2
,
NullableAnnotation
typeParameter1Annotation
,
out
NullableAnnotation
annotation
)
{
if
(
typeParameter1
.
Equals
(
typeParameter2
,
TypeCompareKind
.
AllIgnoreOptions
))
{
annotation
=
typeParameter1Annotation
;
return
true
;
}
bool
dependsOn
=
false
;
var
combinedAnnotation
=
NullableAnnotation
.
Annotated
;
foreach
(
var
constraintType
in
typeParameter1
.
ConstraintTypesNoUseSiteDiagnostics
)
{
if
(
constraintType
.
Type
is
TypeParameterSymbol
constraintTypeParameter
&&
dependsOnTypeParameter
(
constraintTypeParameter
,
typeParameter2
,
constraintType
.
NullableAnnotation
,
out
var
constraintAnnotation
))
{
return
NullableFlowState
.
MaybeDefault
;
dependsOn
=
true
;
combinedAnnotation
=
combinedAnnotation
.
Meet
(
constraintAnnotation
);
}
}
return
state
;
if
(
dependsOn
)
{
annotation
=
combinedAnnotation
.
Join
(
typeParameter1Annotation
);
return
true
;
}
annotation
=
default
;
return
false
;
}
}
...
...
src/Compilers/CSharp/Portable/Symbols/ConstraintsHelper.cs
浏览文件 @
2c5e11c3
...
...
@@ -991,7 +991,7 @@ static NullableFlowState getTypeArgumentState(in TypeWithAnnotations typeWithAnn
}
if
(
type
.
IsValueType
)
{
return
type
.
IsNullableType
()
?
NullableFlowState
.
MaybeNull
:
NullableFlowState
.
NotNull
;
return
type
.
IsNullableType
OrTypeParameter
()
?
NullableFlowState
.
MaybeNull
:
NullableFlowState
.
NotNull
;
}
switch
(
typeWithAnnotations
.
NullableAnnotation
)
{
...
...
@@ -1015,7 +1015,7 @@ static NullableFlowState getTypeArgumentState(in TypeWithAnnotations typeWithAnn
}
else
{
result
=
(
NullableFlowState
)
Math
.
Min
((
int
)
result
.
Value
,
(
int
)
constraintState
);
result
=
result
.
Value
.
Meet
(
constraintState
);
}
}
return
result
??
NullableFlowState
.
MaybeNull
;
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
浏览文件 @
2c5e11c3
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录