Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
a25ee304
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,发现更多精彩内容 >>
提交
a25ee304
编写于
2月 23, 2015
作者:
N
Neal Gafter
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow ?. expression as value of async Task method even if type is unconstrained
Fixes #74 Closes #799
上级
c3ba4e02
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
71 addition
and
17 deletion
+71
-17
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
+19
-17
src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenShortCircuitOperatorTests.cs
...arp/Test/Emit/CodeGen/CodeGenShortCircuitOperatorTests.cs
+52
-0
未找到文件。
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
浏览文件 @
a25ee304
...
...
@@ -6307,10 +6307,11 @@ private BoundConditionalAccess BindConditionalAccessExpression(ConditionalAccess
// access cannot be a pointer
if
((!
accessType
.
IsReferenceType
&&
!
accessType
.
IsValueType
)
||
accessType
.
IsPointerType
())
{
// Assume result type of the access is void when result value isn't used and cannot be made nullable.
// We are not doing this for types that can be made nullable to still allow expression evaluator to
// to get the value.
bool
resultIsNotUsed
=
false
;
// Result type of the access is void when result value cannot be made nullable.
// For improved diagnostics we detect the cases where the value will be used and produce a
// more specific (though not technically correct) diagnostic here:
// "Error CS0023: Operator '?' cannot be applied to operand of type 'T'"
bool
resultIsUsed
=
true
;
CSharpSyntaxNode
parent
=
node
.
Parent
;
if
(
parent
!=
null
)
...
...
@@ -6318,37 +6319,35 @@ private BoundConditionalAccess BindConditionalAccessExpression(ConditionalAccess
switch
(
parent
.
Kind
())
{
case
SyntaxKind
.
ExpressionStatement
:
resultIs
NotUsed
=
((
ExpressionStatementSyntax
)
parent
).
Expression
=
=
node
;
resultIs
Used
=
((
ExpressionStatementSyntax
)
parent
).
Expression
!
=
node
;
break
;
case
SyntaxKind
.
SimpleLambdaExpression
:
resultIs
NotUsed
=
(((
SimpleLambdaExpressionSyntax
)
parent
).
Body
==
node
)
&&
ContainingMethodOrLambdaReturnsVoid
();
resultIs
Used
=
(((
SimpleLambdaExpressionSyntax
)
parent
).
Body
!=
node
)
||
ContainingMethodOrLambdaRequiresValue
();
break
;
case
SyntaxKind
.
ParenthesizedLambdaExpression
:
resultIs
NotUsed
=
(((
ParenthesizedLambdaExpressionSyntax
)
parent
).
Body
==
node
)
&&
ContainingMethodOrLambdaReturnsVoid
();
resultIs
Used
=
(((
ParenthesizedLambdaExpressionSyntax
)
parent
).
Body
!=
node
)
||
ContainingMethodOrLambdaRequiresValue
();
break
;
case
SyntaxKind
.
ArrowExpressionClause
:
resultIs
NotUsed
=
(((
ArrowExpressionClauseSyntax
)
parent
).
Expression
==
node
)
&&
ContainingMethodOrLambdaReturnsVoid
();
resultIs
Used
=
(((
ArrowExpressionClauseSyntax
)
parent
).
Expression
!=
node
)
||
ContainingMethodOrLambdaRequiresValue
();
break
;
case
SyntaxKind
.
ForStatement
:
// Incrementors and Initializers doesn't have to produce a value
var
loop
=
(
ForStatementSyntax
)
parent
;
resultIs
NotUsed
=
loop
.
Incrementors
.
Contains
(
node
)
||
loop
.
Initializers
.
Contains
(
node
);
resultIs
Used
=
!
loop
.
Incrementors
.
Contains
(
node
)
&&
!
loop
.
Initializers
.
Contains
(
node
);
break
;
}
}
if
(
resultIsNotUsed
)
{
accessType
=
GetSpecialType
(
SpecialType
.
System_Void
,
diagnostics
,
node
);
}
else
if
(
resultIsUsed
)
{
return
GenerateBadConditionalAccessNodeError
(
node
,
receiver
,
access
,
diagnostics
);
}
accessType
=
GetSpecialType
(
SpecialType
.
System_Void
,
diagnostics
,
node
);
}
// if access has value type, the type of the conditional access is nullable of that
...
...
@@ -6360,10 +6359,13 @@ private BoundConditionalAccess BindConditionalAccessExpression(ConditionalAccess
return
new
BoundConditionalAccess
(
node
,
receiver
,
access
,
accessType
);
}
private
bool
ContainingMethodOrLambdaRe
turnsVoid
()
private
bool
ContainingMethodOrLambdaRe
quiresValue
()
{
Symbol
containingMember
=
ContainingMemberOrLambda
;
return
(
object
)
containingMember
!=
null
&&
containingMember
.
Kind
==
SymbolKind
.
Method
&&
((
MethodSymbol
)
containingMember
).
ReturnsVoid
;
var
containingMethod
=
ContainingMemberOrLambda
as
MethodSymbol
;
return
(
object
)
containingMethod
==
null
||
!
containingMethod
.
ReturnsVoid
&&
!
containingMethod
.
IsTaskReturningAsync
(
this
.
Compilation
);
}
private
BoundConditionalAccess
GenerateBadConditionalAccessNodeError
(
ConditionalAccessExpressionSyntax
node
,
BoundExpression
receiver
,
BoundExpression
access
,
DiagnosticBag
diagnostics
)
...
...
src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenShortCircuitOperatorTests.cs
浏览文件 @
a25ee304
...
...
@@ -5542,5 +5542,57 @@ public T M()
M
---"
);
}
[
WorkItem
(
74
,
"https://github.com/dotnet/roslyn/issues/74"
)]
[
Fact
]
public
void
ConditionalInAsyncTask
()
{
var
source
=
@"
#pragma warning disable CS1998 // suppress 'no await in async' warning
using System;
using System.Threading.Tasks;
class Foo<T>
{
public T Method(int i)
{
Console.Write(i);
return default(T); // returns value of unconstrained type parameter type
}
public void M1(Foo<T> x) => x?.Method(4);
public async void M2(Foo<T> x) => x?.Method(5);
public async Task M3(Foo<T> x) => x?.Method(6);
public async Task M4() {
Foo<T> a = new Foo<T>();
Foo<T> b = null;
Action f1 = async () => a?.Method(1);
f1();
f1 = async () => b?.Method(0);
f1();
Func<Task> f2 = async () => a?.Method(2);
await f2();
Func<Task> f3 = async () => b?.Method(3);
await f3();
M1(a); M1(b);
M2(a); M2(b);
await M3(a);
await M3(b);
}
}
class Program
{
public static void Main()
{
// this will complete synchronously as there are no truly async ops.
new Foo<int>().M4();
}
}"
;
var
compilation
=
CreateCompilationWithMscorlib45
(
source
,
references
:
new
[]
{
SystemRef_v4_0_30319_17929
,
SystemCoreRef_v4_0_30319_17929
,
CSharpRef
},
options
:
TestOptions
.
DebugExe
);
CompileAndVerify
(
compilation
,
expectedOutput
:
"12456"
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录