Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
61a287ab
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,体验更适合开发者的 AI 搜索 >>
未验证
提交
61a287ab
编写于
12月 07, 2020
作者:
R
Rikki Gibson
提交者:
GitHub
12月 07, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Input param postconditions (#49576)
上级
46c97fe4
变更
2
展开全部
隐藏空白更改
内联
并排
Showing
2 changed file
with
547 addition
and
290 deletion
+547
-290
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
+30
-11
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
...rp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
+517
-279
未找到文件。
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
浏览文件 @
61a287ab
...
...
@@ -1850,6 +1850,22 @@ private static bool ShouldReportNullableAssignment(TypeWithAnnotations type, Nul
ReportDiagnostic
(
ErrorCode
.
WRN_NullReferenceArgument
,
location
,
GetParameterAsDiagnosticArgument
(
parameterOpt
),
GetContainingSymbolAsDiagnosticArgument
(
parameterOpt
));
if
(
targetType
.
Type
.
IsPossiblyNullableReferenceTypeTypeParameter
())
{
var
slotBuilder
=
ArrayBuilder
<
int
>.
GetInstance
();
GetSlotsToMarkAsNotNullable
(
value
,
slotBuilder
);
foreach
(
var
slot
in
slotBuilder
)
{
Debug
.
Assert
(
State
[
slot
]
==
NullableFlowState
.
MaybeDefault
);
State
[
slot
]
=
NullableFlowState
.
MaybeNull
;
}
slotBuilder
.
Free
();
}
else
{
LearnFromNonNullTest
(
value
,
ref
State
);
}
}
else
if
(
useLegacyWarnings
)
{
...
...
@@ -4230,7 +4246,7 @@ private static TypeWithState GetNullCoalescingResultType(TypeWithState rightResu
// Per LDM 2019-02-13 decision, the result of a conditional access "may be null" even if
// both the receiver and right-hand-side are believed not to be null.
SetResultType
(
node
,
TypeWithState
.
Create
(
resultType
,
NullableFlowState
.
Maybe
Null
));
SetResultType
(
node
,
TypeWithState
.
Create
(
resultType
,
NullableFlowState
.
Maybe
Default
));
_currentConditionalReceiverVisitResult
=
default
;
_lastConditionalAccessSlot
=
previousConditionalAccessSlot
;
return
null
;
...
...
@@ -5308,7 +5324,10 @@ private VisitArgumentResult VisitArgumentEvaluate(BoundExpression argument, RefK
stateForLambda
:
result
.
StateForLambda
);
// If the parameter has annotations, we perform an additional check for nullable value types
CheckDisallowedNullAssignment
(
stateAfterConversion
,
parameterAnnotations
,
argumentNoConversion
.
Syntax
.
Location
);
if
(
CheckDisallowedNullAssignment
(
stateAfterConversion
,
parameterAnnotations
,
argumentNoConversion
.
Syntax
.
Location
))
{
LearnFromNonNullTest
(
argumentNoConversion
,
ref
State
);
}
SetResultType
(
argumentNoConversion
,
stateAfterConversion
,
updateAnalyzedNullability
:
false
);
}
break
;
...
...
@@ -5340,19 +5359,23 @@ private VisitArgumentResult VisitArgumentEvaluate(BoundExpression argument, RefK
Debug
.
Assert
(!
this
.
IsConditionalState
);
}
private
void
CheckDisallowedNullAssignment
(
TypeWithState
state
,
FlowAnalysisAnnotations
annotations
,
Location
location
,
BoundExpression
?
boundValueOpt
=
null
)
/// <summary>Returns <see langword="true"/> if this is an assignment forbidden by DisallowNullAttribute, otherwise <see langword="false"/>.</summary>
private
bool
CheckDisallowedNullAssignment
(
TypeWithState
state
,
FlowAnalysisAnnotations
annotations
,
Location
location
,
BoundExpression
?
boundValueOpt
=
null
)
{
if
(
boundValueOpt
is
{
WasCompilerGenerated
:
true
})
{
// We need to skip `return backingField;` in auto-prop getters
return
;
return
false
;
}
// We do this extra check for types whose non-nullable version cannot be represented
if
(
IsDisallowedNullAssignment
(
state
,
annotations
))
{
ReportDiagnostic
(
ErrorCode
.
WRN_DisallowNullAttributeForbidsMaybeNullAssignment
,
location
);
return
true
;
}
return
false
;
}
private
static
bool
IsDisallowedNullAssignment
(
TypeWithState
valueState
,
FlowAnalysisAnnotations
targetAnnotations
)
...
...
@@ -5595,10 +5618,6 @@ void learnFromPostConditions(BoundExpression argument, TypeWithAnnotations param
// Note: NotNull = NotNullWhen(true) + NotNullWhen(false)
bool
notNullWhenTrue
=
(
parameterAnnotations
&
FlowAnalysisAnnotations
.
NotNullWhenTrue
)
!=
0
;
bool
notNullWhenFalse
=
(
parameterAnnotations
&
FlowAnalysisAnnotations
.
NotNullWhenFalse
)
!=
0
;
bool
disallowNull
=
(
parameterAnnotations
&
FlowAnalysisAnnotations
.
DisallowNull
)
!=
0
;
bool
setNotNullFromParameterType
=
!
argument
.
IsSuppressed
&&
!
parameterType
.
Type
.
IsPossiblyNullableReferenceTypeTypeParameter
()
&&
parameterType
.
NullableAnnotation
.
IsNotAnnotated
();
// Note: MaybeNull = MaybeNullWhen(true) + MaybeNullWhen(false)
bool
maybeNullWhenTrue
=
(
parameterAnnotations
&
FlowAnalysisAnnotations
.
MaybeNullWhenTrue
)
!=
0
;
...
...
@@ -5608,7 +5627,7 @@ void learnFromPostConditions(BoundExpression argument, TypeWithAnnotations param
{
LearnFromNullTest
(
argument
,
ref
State
);
}
else
if
(
((
notNullWhenTrue
&&
notNullWhenFalse
)
||
disallowNull
||
setNotNullFromParameterType
)
else
if
(
notNullWhenTrue
&&
notNullWhenFalse
&&
!
IsConditionalState
&&
!(
maybeNullWhenTrue
||
maybeNullWhenFalse
))
{
...
...
@@ -7045,9 +7064,9 @@ static bool dependsOnTypeParameter(TypeParameterSymbol typeParameter1, TypeParam
diagnosticLocation
:
operandLocation
);
// in the case of a lifted conversion, we assume that the call to the operator occurs only if the argument is not-null
if
(!
isLiftedConversion
)
if
(!
isLiftedConversion
&&
CheckDisallowedNullAssignment
(
operandType
,
parameterAnnotations
,
conversionOperand
.
Syntax
.
Location
)
)
{
CheckDisallowedNullAssignment
(
operandType
,
parameterAnnotations
,
conversionOperand
.
Syntax
.
Location
);
LearnFromNonNullTest
(
conversionOperand
,
ref
State
);
}
// method parameter type -> method return type
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
浏览文件 @
61a287ab
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录