Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
9d2a2cfe
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,发现更多精彩内容 >>
提交
9d2a2cfe
编写于
11月 01, 2019
作者:
Y
Yair Halberstadt
提交者:
Charles Stoner
11月 01, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix Host Object Access in Interactive (#39584)
上级
964de174
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
121 addition
and
5 deletion
+121
-5
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
+24
-4
src/Scripting/CSharpTest/InteractiveSessionTests.cs
src/Scripting/CSharpTest/InteractiveSessionTests.cs
+97
-1
未找到文件。
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
浏览文件 @
9d2a2cfe
...
...
@@ -1537,7 +1537,7 @@ private BoundExpression SynthesizeMethodGroupReceiver(CSharpSyntaxNode syntax, A
}
else
{
return
TryBindInteractiveReceiver
(
syntax
,
ContainingMember
(),
currentType
,
declaringType
);
return
TryBindInteractiveReceiver
(
syntax
,
declaringType
);
}
}
...
...
@@ -1795,7 +1795,7 @@ private BoundExpression SynthesizeReceiver(SyntaxNode node, Symbol member, Diagn
}
else
{
return
TryBindInteractiveReceiver
(
node
,
ContainingMember
(),
currentType
,
declaringType
);
return
TryBindInteractiveReceiver
(
node
,
declaringType
);
}
}
...
...
@@ -1810,9 +1810,11 @@ internal Symbol ContainingMember()
return
containingMember
;
}
private
BoundExpression
TryBindInteractiveReceiver
(
SyntaxNode
syntax
,
Symbol
currentMember
,
NamedTypeSymbol
currentType
,
NamedTypeSymbol
memberDeclaringType
)
private
BoundExpression
TryBindInteractiveReceiver
(
SyntaxNode
syntax
,
NamedTypeSymbol
memberDeclaringType
)
{
if
(
currentType
.
TypeKind
==
TypeKind
.
Submission
&&
currentMember
.
RequiresInstanceReceiver
())
if
(
this
.
ContainingType
.
TypeKind
==
TypeKind
.
Submission
// check we have access to `this`
&&
isInstanceContext
())
{
if
(
memberDeclaringType
.
TypeKind
==
TypeKind
.
Submission
)
{
...
...
@@ -1830,6 +1832,24 @@ private BoundExpression TryBindInteractiveReceiver(SyntaxNode syntax, Symbol cur
}
return
null
;
bool
isInstanceContext
()
{
var
containingMember
=
this
.
ContainingMemberOrLambda
;
do
{
if
(
containingMember
.
IsStatic
)
{
return
false
;
}
if
(
containingMember
.
Kind
==
SymbolKind
.
NamedType
)
{
break
;
}
containingMember
=
containingMember
.
ContainingSymbol
;
}
while
((
object
)
containingMember
!=
null
);
return
true
;
}
}
public
BoundExpression
BindNamespaceOrTypeOrExpression
(
ExpressionSyntax
node
,
DiagnosticBag
diagnostics
)
...
...
src/Scripting/CSharpTest/InteractiveSessionTests.cs
浏览文件 @
9d2a2cfe
...
...
@@ -2,7 +2,6 @@
using
System
;
using
System.Collections.Generic
;
using
System.Globalization
;
using
System.IO
;
using
System.Linq
;
using
System.Reflection
;
...
...
@@ -1148,6 +1147,14 @@ public void AwaitChain2()
Assert
.
Equal
(
3
,
state
.
ReturnValue
);
}
[
Fact
,
WorkItem
(
39548
,
"https://github.com/dotnet/roslyn/issues/39548"
)]
public
async
Task
PatternVariableDeclaration
()
{
var
state
=
await
CSharpScript
.
RunAsync
(
"var x = (false, 4);"
);
state
=
await
state
.
ContinueWithAsync
(
"x is (false, var y)"
);
Assert
.
Equal
(
true
,
state
.
ReturnValue
);
}
#
endregion
#
region
References
...
...
@@ -1776,6 +1783,95 @@ public void HostObjectAssemblyReference3()
}
}
public
class
E
{
public
bool
TryGetValue
(
out
object
obj
)
{
obj
=
new
object
();
return
true
;
}
}
[
Fact
]
[
WorkItem
(
39565
,
"https://github.com/dotnet/roslyn/issues/39565"
)]
public
async
Task
MethodCallWithImplicitReceiverAndOutVar
()
{
var
code
=
@"
if(TryGetValue(out var result)){
_ = result;
}
return true;
"
;
var
result
=
await
CSharpScript
.
EvaluateAsync
<
bool
>(
code
,
globalsType
:
typeof
(
E
),
globals
:
new
E
());
Assert
.
True
(
result
);
}
public
class
F
{
public
bool
Value
=
true
;
}
[
Fact
]
public
void
StaticMethodCannotAccessGlobalInstance
()
{
var
code
=
@"
static bool M()
{
return Value;
}
return M();
"
;
var
script
=
CSharpScript
.
Create
<
bool
>(
code
,
globalsType
:
typeof
(
F
));
ScriptingTestHelpers
.
AssertCompilationError
(()
=>
script
.
RunAsync
(
new
F
()).
Wait
(),
// (4,9): error CS0120: An object reference is required for the non-static field, method, or property 'InteractiveSessionTests.F.Value'
// return Value;
Diagnostic
(
ErrorCode
.
ERR_ObjectRequired
,
"Value"
).
WithArguments
(
"Microsoft.CodeAnalysis.CSharp.Scripting.UnitTests.InteractiveSessionTests.F.Value"
).
WithLocation
(
4
,
9
));
}
[
Fact
]
[
WorkItem
(
39581
,
"https://github.com/dotnet/roslyn/issues/39581"
)]
public
void
StaticLocalFunctionCannotAccessGlobalInstance
()
{
var
code
=
@"
bool M()
{
return Inner();
static bool Inner()
{
return Value;
}
}
return M();
"
;
var
script
=
CSharpScript
.
Create
<
bool
>(
code
,
globalsType
:
typeof
(
F
));
ScriptingTestHelpers
.
AssertCompilationError
(()
=>
script
.
RunAsync
(
new
F
()).
Wait
(),
// (7,10): error CS0120: An object reference is required for the non-static field, method, or property 'InteractiveSessionTests.F.Value'
// return Value;
Diagnostic
(
ErrorCode
.
ERR_ObjectRequired
,
"Value"
).
WithArguments
(
"Microsoft.CodeAnalysis.CSharp.Scripting.UnitTests.InteractiveSessionTests.F.Value"
).
WithLocation
(
7
,
10
));
}
[
Fact
]
public
async
Task
LocalFunctionCanAccessGlobalInstance
()
{
var
code
=
@"
bool M()
{
return Inner();
bool Inner()
{
return Value;
}
}
return M();
"
;
var
result
=
await
CSharpScript
.
EvaluateAsync
<
bool
>(
code
,
globalsType
:
typeof
(
F
),
globals
:
new
F
());
Assert
.
True
(
result
);
}
#
endregion
#
region
Exceptions
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录