Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
b8c54ca9
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,发现更多精彩内容 >>
未验证
提交
b8c54ca9
编写于
5月 09, 2019
作者:
F
Fred Silberberg
提交者:
GitHub
5月 09, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #35544 from 333fred/visit-conversion
Refactor ApplyConversion into VisitConversion
上级
c448c236
d4dc079f
变更
5
展开全部
隐藏空白更改
内联
并排
Showing
5 changed file
with
501 addition
and
272 deletion
+501
-272
src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs
src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs
+1
-1
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
+294
-207
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker_Patterns.cs
...s/CSharp/Portable/FlowAnalysis/NullableWalker_Patterns.cs
+3
-3
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
...rp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
+111
-61
src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs
...harp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs
+92
-0
未找到文件。
src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs
浏览文件 @
b8c54ca9
...
...
@@ -191,7 +191,7 @@ private BoundExpression CreateUserDefinedConversion(SyntaxNode syntax, BoundExpr
conversion
:
conversion
.
UserDefinedFromConversion
,
isCast
:
false
,
conversionGroup
,
wasCompilerGenerated
:
tru
e
,
wasCompilerGenerated
:
fals
e
,
destination
:
conversion
.
BestUserDefinedConversionAnalysis
.
FromType
,
diagnostics
:
diagnostics
);
...
...
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
浏览文件 @
b8c54ca9
此差异已折叠。
点击以展开。
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker_Patterns.cs
浏览文件 @
b8c54ca9
...
...
@@ -498,7 +498,6 @@ public override BoundNode VisitSwitchExpression(BoundSwitchExpression node)
conversions
.
Add
(
conversion
);
var
armType
=
VisitRvalueWithState
(
expression
);
resultTypes
.
Add
(
armType
);
TrackInferredTypesThroughConversions
(
arm
.
Value
,
expression
,
_visitResult
);
Join
(
ref
endState
,
ref
this
.
State
);
// Build placeholders for inference in order to preserve annotations.
...
...
@@ -516,7 +515,7 @@ public override BoundNode VisitSwitchExpression(BoundSwitchExpression node)
for
(
int
i
=
0
;
i
<
numSwitchArms
;
i
++)
{
var
expression
=
expressions
[
i
];
resultTypes
[
i
]
=
ApplyConversion
(
expression
,
expression
,
conversions
[
i
],
inferredTypeWithAnnotations
,
resultTypes
[
i
],
checkConversion
:
true
,
resultTypes
[
i
]
=
VisitConversion
(
conversionOpt
:
null
,
expression
,
conversions
[
i
],
inferredTypeWithAnnotations
,
resultTypes
[
i
],
checkConversion
:
true
,
fromExplicitCast
:
false
,
useLegacyWarnings
:
false
,
AssignmentKind
.
Assignment
,
reportRemainingWarnings
:
true
,
reportTopLevelWarnings
:
false
);
}
...
...
@@ -527,8 +526,9 @@ public override BoundNode VisitSwitchExpression(BoundSwitchExpression node)
for
(
int
i
=
0
;
i
<
numSwitchArms
;
i
++)
{
var
nodeForSyntax
=
expressions
[
i
];
var
conversionOpt
=
node
.
SwitchArms
[
i
].
Value
switch
{
BoundConversion
c
when
c
!=
nodeForSyntax
=>
c
,
_
=>
null
};
// Report top-level warnings
_
=
ApplyConversion
(
nodeForSyntax
,
operandOpt
:
nodeForSyntax
,
conversions
[
i
],
targetTypeWithNullability
:
inferredTypeWithAnnotations
,
operandType
:
resultTypes
[
i
],
_
=
VisitConversion
(
conversionOpt
,
conversionOperand
:
nodeForSyntax
,
conversions
[
i
],
targetTypeWithNullability
:
inferredTypeWithAnnotations
,
operandType
:
resultTypes
[
i
],
checkConversion
:
true
,
fromExplicitCast
:
false
,
useLegacyWarnings
:
false
,
AssignmentKind
.
Assignment
,
reportRemainingWarnings
:
false
,
reportTopLevelWarnings
:
true
);
}
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
浏览文件 @
b8c54ca9
此差异已折叠。
点击以展开。
src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs
浏览文件 @
b8c54ca9
...
...
@@ -727,5 +727,97 @@ public override void Initialize(AnalysisContext context)
},
SyntaxKind
.
IdentifierName
);
}
}
[
Fact
]
public
void
MultipleConversions
()
{
var
source
=
@"
class A { public static explicit operator C(A a) => new D(); }
class B : A { }
class C { }
class D : C { }
class E
{
void M()
{
var d = (D)(C?)new B();
}
}"
;
var
comp
=
CreateCompilation
(
source
,
options
:
WithNonNullTypesTrue
(),
parseOptions
:
TestOptions
.
Regular8WithNullableAnalysis
);
comp
.
VerifyDiagnostics
(
// (10,17): warning CS8600: Converting null literal or possible null value to non-nullable type.
// var d = (D)(C?)new B();
Diagnostic
(
ErrorCode
.
WRN_ConvertingNullableToNonNullable
,
"(D)(C?)new B()"
).
WithLocation
(
10
,
17
));
var
syntaxTree
=
comp
.
SyntaxTrees
[
0
];
var
root
=
syntaxTree
.
GetRoot
();
var
model
=
comp
.
GetSemanticModel
(
syntaxTree
);
var
aType
=
comp
.
GetTypeByMetadataName
(
"A"
);
var
bType
=
comp
.
GetTypeByMetadataName
(
"B"
);
var
cType
=
comp
.
GetTypeByMetadataName
(
"C"
);
var
dType
=
comp
.
GetTypeByMetadataName
(
"D"
);
var
nullable
=
new
NullabilityInfo
(
PublicNullableAnnotation
.
Annotated
,
PublicNullableFlowState
.
MaybeNull
);
var
notNullable
=
new
NullabilityInfo
(
PublicNullableAnnotation
.
NotAnnotated
,
PublicNullableFlowState
.
NotNull
);
var
dCast
=
(
CastExpressionSyntax
)
root
.
DescendantNodes
().
OfType
<
EqualsValueClauseSyntax
>().
Single
().
Value
;
var
dInfo
=
model
.
GetTypeInfo
(
dCast
);
Assert
.
Equal
(
dType
,
dInfo
.
Type
);
Assert
.
Equal
(
dType
,
dInfo
.
ConvertedType
);
Assert
.
Equal
(
nullable
,
dInfo
.
Nullability
);
Assert
.
Equal
(
nullable
,
dInfo
.
ConvertedNullability
);
var
cCast
=
(
CastExpressionSyntax
)
dCast
.
Expression
;
var
cInfo
=
model
.
GetTypeInfo
(
cCast
);
Assert
.
Equal
(
cType
,
cInfo
.
Type
);
Assert
.
Equal
(
cType
,
cInfo
.
ConvertedType
);
Assert
.
Equal
(
nullable
,
cInfo
.
Nullability
);
Assert
.
Equal
(
nullable
,
cInfo
.
ConvertedNullability
);
var
objectCreation
=
cCast
.
Expression
;
var
creationInfo
=
model
.
GetTypeInfo
(
objectCreation
);
Assert
.
Equal
(
bType
,
creationInfo
.
Type
);
Assert
.
Equal
(
aType
,
creationInfo
.
ConvertedType
);
Assert
.
Equal
(
notNullable
,
creationInfo
.
Nullability
);
Assert
.
Equal
(
nullable
,
creationInfo
.
ConvertedNullability
);
}
[
Fact
]
public
void
ConditionalOperator_InvalidType
()
{
var
source
=
@"
class C
{
void M()
{
var x = new Undefined() ? new object() : null;
}
}"
;
var
comp
=
CreateCompilation
(
source
,
options
:
WithNonNullTypesTrue
(),
parseOptions
:
TestOptions
.
Regular8WithNullableAnalysis
);
comp
.
VerifyDiagnostics
(
// (6,21): error CS0246: The type or namespace name 'Undefined' could not be found (are you missing a using directive or an assembly reference?)
// var x = new Undefined() ? new object() : null;
Diagnostic
(
ErrorCode
.
ERR_SingleTypeNameNotFound
,
"Undefined"
).
WithArguments
(
"Undefined"
).
WithLocation
(
6
,
21
));
var
syntaxTree
=
comp
.
SyntaxTrees
[
0
];
var
root
=
syntaxTree
.
GetRoot
();
var
model
=
comp
.
GetSemanticModel
(
syntaxTree
);
var
conditional
=
root
.
DescendantNodes
().
OfType
<
ConditionalExpressionSyntax
>().
Single
();
var
notNull
=
new
NullabilityInfo
(
PublicNullableAnnotation
.
NotAnnotated
,
PublicNullableFlowState
.
NotNull
);
var
@null
=
new
NullabilityInfo
(
PublicNullableAnnotation
.
Annotated
,
PublicNullableFlowState
.
MaybeNull
);
var
leftInfo
=
model
.
GetTypeInfo
(
conditional
.
WhenTrue
);
var
rightInfo
=
model
.
GetTypeInfo
(
conditional
.
WhenFalse
);
Assert
.
Equal
(
notNull
,
leftInfo
.
Nullability
);
Assert
.
Equal
(
notNull
,
leftInfo
.
ConvertedNullability
);
Assert
.
Equal
(
@null
,
rightInfo
.
Nullability
);
Assert
.
Equal
(
notNull
,
rightInfo
.
ConvertedNullability
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录