Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
4a668d2b
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,发现更多精彩内容 >>
未验证
提交
4a668d2b
编写于
7月 02, 2018
作者:
J
Jared Parsons
提交者:
GitHub
7月 02, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #28118 from OmarTawfik/bugs/28087/ref-array-crash
Ref-assigning to array elements should produce an error
上级
b28a71cd
d6970a07
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
118 addition
and
32 deletion
+118
-32
src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
+24
-15
src/Compilers/CSharp/Test/Semantic/Semantics/RefLocalsAndReturnsTests.cs
...Sharp/Test/Semantic/Semantics/RefLocalsAndReturnsTests.cs
+94
-17
未找到文件。
src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
浏览文件 @
4a668d2b
...
...
@@ -370,32 +370,41 @@ internal bool CheckValueKind(SyntaxNode node, BoundExpression expr, BindValueKin
}
break
;
// array elements and pointer dereferencing are readwrite variables
case
BoundKind
.
ArrayAccess
:
case
BoundKind
.
PointerIndirectionOperator
:
// array elements and pointer dereferencing are readwrite variables
return
true
;
// The undocumented __refvalue(tr, T) expression results in a variable of type T.
case
BoundKind
.
RefValueOperator
:
// dynamic expressions are readwrite, and can even be passed by ref (which is implemented via a temp)
case
BoundKind
.
DynamicMemberAccess
:
case
BoundKind
.
DynamicIndexerAccess
:
{
if
(
RequiresRefAssignableVariable
(
valueKind
))
{
Error
(
diagnostics
,
ErrorCode
.
ERR_RefLocalOrParamExpected
,
node
);
return
false
;
}
// These are readwrite variables
return
true
;
}
case
BoundKind
.
PointerElementAccess
:
{
if
(
RequiresRefAssignableVariable
(
valueKind
))
{
Error
(
diagnostics
,
ErrorCode
.
ERR_RefLocalOrParamExpected
,
node
);
return
false
;
}
var
receiver
=
((
BoundPointerElementAccess
)
expr
).
Expression
;
if
(
receiver
is
BoundFieldAccess
fieldAccess
&&
fieldAccess
.
FieldSymbol
.
IsFixed
)
{
return
CheckValueKind
(
node
,
fieldAccess
.
ReceiverOpt
,
valueKind
,
checkingReceiver
:
true
,
diagnostics
);
}
}
return
true
;
case
BoundKind
.
RefValueOperator
:
// The undocumented __refvalue(tr, T) expression results in a variable of type T.
// it is a readwrite variable.
return
true
;
case
BoundKind
.
DynamicMemberAccess
:
case
BoundKind
.
DynamicIndexerAccess
:
// dynamic expressions can be read and written to
// can even be passed by reference (which is implemented via a temp)
return
true
;
return
true
;
}
case
BoundKind
.
Parameter
:
var
parameter
=
(
BoundParameter
)
expr
;
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/RefLocalsAndReturnsTests.cs
浏览文件 @
4a668d2b
...
...
@@ -3836,28 +3836,105 @@ public void M(ref int x)
}
[
Fact
]
[
WorkItem
(
2
6978
,
"https://github.com/dotnet/roslyn/issues/26978
"
)]
public
void
BindingRefDynamicObjAssign
ment
()
[
WorkItem
(
2
8087
,
"https://github.com/dotnet/roslyn/issues/28087
"
)]
public
void
AssigningRef_ArrayEle
ment
()
{
CompileAndVerify
(
@"
using System;
class C
CreateCompilation
(
@"
public class C
{
public int P;
public void M(int[] array, ref int value)
{
array[0] = ref value;
}
}"
).
VerifyDiagnostics
(
// (6,9): error CS8373: The left-hand side of a ref assignment must be a ref local or parameter.
// array[0] = ref value;
Diagnostic
(
ErrorCode
.
ERR_RefLocalOrParamExpected
,
"array[0]"
).
WithLocation
(
6
,
9
));
}
static void Main()
[
Fact
]
[
WorkItem
(
28087
,
"https://github.com/dotnet/roslyn/issues/28087"
)]
public
void
AssigningRef_PointerIndirectionOperator
()
{
CreateCompilation
(
@"
public unsafe class C
{
public void M(int* ptr, ref int value)
{
dynamic x = new C();
x.P = 5;
Console.WriteLine(x.P);
dynamic y = new C();
y.P = ref x.P;
Console.WriteLine(y.P);
*ptr = ref value;
}
}"
,
options
:
TestOptions
.
UnsafeReleaseDll
).
VerifyDiagnostics
(
// (6,9): error CS8373: The left-hand side of a ref assignment must be a ref local or parameter.
// *ptr = ref value;
Diagnostic
(
ErrorCode
.
ERR_RefLocalOrParamExpected
,
"*ptr"
).
WithLocation
(
6
,
9
));
}
[
Fact
]
[
WorkItem
(
28087
,
"https://github.com/dotnet/roslyn/issues/28087"
)]
public
void
AssigningRef_PointerElementAccess
()
{
CreateCompilation
(
@"
public unsafe class C
{
public void M(int* ptr, ref int value)
{
ptr[0] = ref value;
}
}"
,
options
:
TestOptions
.
UnsafeReleaseDll
).
VerifyDiagnostics
(
// (6,9): error CS8373: The left-hand side of a ref assignment must be a ref local or parameter.
// ptr[0] = ref value;
Diagnostic
(
ErrorCode
.
ERR_RefLocalOrParamExpected
,
"ptr[0]"
).
WithLocation
(
6
,
9
));
}
[
Fact
]
[
WorkItem
(
28087
,
"https://github.com/dotnet/roslyn/issues/28087"
)]
public
void
AssigningRef_RefvalueExpression
()
{
CreateCompilation
(
@"
public unsafe class C
{
public void M(int x)
{
__refvalue(__makeref(x), int) = ref x;
}
}"
,
options
:
TestOptions
.
UnsafeReleaseDll
).
VerifyDiagnostics
(
// (6,9): error CS8373: The left-hand side of a ref assignment must be a ref local or parameter.
// __refvalue(__makeref(x), int) = ref x;
Diagnostic
(
ErrorCode
.
ERR_RefLocalOrParamExpected
,
"__refvalue(__makeref(x), int)"
).
WithLocation
(
6
,
9
));
}
[
Fact
]
[
WorkItem
(
28087
,
"https://github.com/dotnet/roslyn/issues/28087"
)]
public
void
AssigningRef_DynamicIndexerAccess
()
{
CreateCompilation
(
@"
public unsafe class C
{
public void M(dynamic d, ref int value)
{
d[0] = ref value;
}
}"
,
options
:
TestOptions
.
UnsafeReleaseDll
).
VerifyDiagnostics
(
// (6,9): error CS8373: The left-hand side of a ref assignment must be a ref local or parameter.
// d[0] = ref value;
Diagnostic
(
ErrorCode
.
ERR_RefLocalOrParamExpected
,
"d[0]"
).
WithLocation
(
6
,
9
));
}
[
Fact
]
[
WorkItem
(
28087
,
"https://github.com/dotnet/roslyn/issues/28087"
)]
public
void
AssigningRef_DynamicMemberAccess
()
{
CreateCompilation
(
@"
public unsafe class C
{
public void M(dynamic d, ref int value)
{
d.member = ref value;
}
}"
,
references
:
new
[]
{
CSharpRef
},
expectedOutput
:
@"
5
5"
);
}"
,
options
:
TestOptions
.
UnsafeReleaseDll
).
VerifyDiagnostics
(
// (6,9): error CS8373: The left-hand side of a ref assignment must be a ref local or parameter.
// d.member = ref value;
Diagnostic
(
ErrorCode
.
ERR_RefLocalOrParamExpected
,
"d.member"
).
WithLocation
(
6
,
9
));
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录