Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
e27214f0
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,发现更多精彩内容 >>
提交
e27214f0
编写于
6月 01, 2016
作者:
L
lorcanmooney
提交者:
Charles Stoner
6月 10, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Disallow object initializers in default parameter values
上级
7a9eda0e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
48 addition
and
20 deletion
+48
-20
src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs
...pilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs
+27
-20
src/Compilers/CSharp/Test/Semantic/Semantics/NamedAndOptionalTests.cs
...s/CSharp/Test/Semantic/Semantics/NamedAndOptionalTests.cs
+21
-0
未找到文件。
src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs
浏览文件 @
e27214f0
...
...
@@ -176,25 +176,6 @@ internal static class ParameterHelpers
Conversion
conversion
=
binder
.
Conversions
.
ClassifyImplicitConversionFromExpression
(
defaultExpression
,
parameterType
,
ref
useSiteDiagnostics
);
diagnostics
.
Add
(
defaultExpression
.
Syntax
,
useSiteDiagnostics
);
// SPEC VIOLATION:
// By the spec an optional parameter initializer is required to be either:
// * a constant,
// * new S() where S is a value type
// * default(S) where S is a value type.
//
// The native compiler considers default(T) to be a valid
// initializer regardless of whether T is a value type
// reference type, type parameter type, and so on.
// We should consider simply allowing this in the spec.
//
// Also when valuetype S has a parameterless constructor,
// new S() is clearly not a constant expression and should produce an error
bool
isValidDefaultValue
=
(
defaultExpression
.
ConstantValue
!=
null
)
||
(
defaultExpression
.
Kind
==
BoundKind
.
DefaultOperator
)
||
(
defaultExpression
.
Kind
==
BoundKind
.
ObjectCreationExpression
&&
((
BoundObjectCreationExpression
)
defaultExpression
).
Constructor
.
IsDefaultValueTypeConstructor
());
SyntaxToken
outKeyword
;
SyntaxToken
refKeyword
;
SyntaxToken
paramsKeyword
;
...
...
@@ -234,7 +215,7 @@ internal static class ParameterHelpers
hasErrors
=
true
;
}
}
else
if
(!
defaultExpression
.
HasAnyErrors
&&
!
isValidDefaultValue
)
else
if
(!
defaultExpression
.
HasAnyErrors
&&
!
IsValidDefaultValue
(
defaultExpression
)
)
{
// error CS1736: Default parameter value for '{0}' must be a compile-time constant
diagnostics
.
Add
(
ErrorCode
.
ERR_DefaultValueMustBeConstant
,
parameterSyntax
.
Default
.
Value
.
Location
,
parameterSyntax
.
Identifier
.
ValueText
);
...
...
@@ -319,6 +300,32 @@ internal static class ParameterHelpers
return
hasErrors
;
}
private
static
bool
IsValidDefaultValue
(
BoundExpression
expression
)
{
// SPEC VIOLATION:
// By the spec an optional parameter initializer is required to be either:
// * a constant,
// * new S() where S is a value type
// * default(S) where S is a value type.
//
// The native compiler considers default(T) to be a valid
// initializer regardless of whether T is a value type
// reference type, type parameter type, and so on.
// We should consider simply allowing this in the spec.
//
// Also when valuetype S has a parameterless constructor,
// new S() is clearly not a constant expression and should produce an error
return
(
expression
.
ConstantValue
!=
null
)
||
(
expression
.
Kind
==
BoundKind
.
DefaultOperator
)
||
(
expression
.
Kind
==
BoundKind
.
ObjectCreationExpression
&&
IsValidDefaultValue
((
BoundObjectCreationExpression
)
expression
));
}
private
static
bool
IsValidDefaultValue
(
BoundObjectCreationExpression
expression
)
{
return
expression
.
Constructor
.
IsDefaultValueTypeConstructor
()
&&
expression
.
InitializerExpressionOpt
==
null
;
}
internal
static
MethodSymbol
FindContainingGenericMethod
(
Symbol
symbol
)
{
for
(
Symbol
current
=
symbol
;
(
object
)
current
!=
null
;
current
=
current
.
ContainingSymbol
)
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/NamedAndOptionalTests.cs
浏览文件 @
e27214f0
...
...
@@ -845,6 +845,27 @@ public void OptionalValueInvokesStaticMethod()
Diagnostic
(
ErrorCode
.
ERR_DefaultValueMustBeConstant
,
"M2()"
).
WithArguments
(
"value"
).
WithLocation
(
5
,
37
));
}
[
WorkItem
(
11638
,
"https://github.com/dotnet/roslyn/issues/11638"
)]
[
Fact
]
public
void
OptionalValueHasObjectInitializer
()
{
var
source
=
@"class C
{
static void Test(Vector3 vector = new Vector3() { X = 1f, Y = 1f, Z = 1f}) { }
}
public struct Vector3
{
public float X;
public float Y;
public float Z;
}"
;
CreateCompilationWithMscorlib
(
source
).
VerifyDiagnostics
(
// (3,39): error CS1736: Default parameter value for 'vector' must be a compile-time constant
Diagnostic
(
ErrorCode
.
ERR_DefaultValueMustBeConstant
,
"new Vector3() { X = 1f, Y = 1f, Z = 1f}"
).
WithArguments
(
"vector"
).
WithLocation
(
3
,
39
));
}
[
WorkItem
(
542411
,
"http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/542411"
)]
[
WorkItem
(
542365
,
"http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/542365"
)]
[
Fact
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录