Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
19b00627
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,发现更多精彩内容 >>
提交
19b00627
编写于
2月 03, 2015
作者:
N
nmgafter
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1112822-Color Color versus const initializer cycles (changeset 1409657)
上级
131c88d6
变更
12
显示空白变更内容
内联
并排
Showing
12 changed file
with
597 addition
and
494 deletion
+597
-494
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
+1
-1
src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
+17
-14
src/Compilers/CSharp/Portable/CodeGen/Optimizer.cs
src/Compilers/CSharp/Portable/CodeGen/Optimizer.cs
+1
-1
src/Compilers/CSharp/Portable/Symbols/ConstantValueUtils.cs
src/Compilers/CSharp/Portable/Symbols/ConstantValueUtils.cs
+1
-0
src/Compilers/CSharp/Portable/Symbols/LocalSymbol.cs
src/Compilers/CSharp/Portable/Symbols/LocalSymbol.cs
+5
-6
src/Compilers/CSharp/Portable/Symbols/Source/SourceLocalSymbol.cs
...ilers/CSharp/Portable/Symbols/Source/SourceLocalSymbol.cs
+397
-395
src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedLocal.cs
...s/CSharp/Portable/Symbols/Synthesized/SynthesizedLocal.cs
+1
-1
src/Compilers/CSharp/Portable/Symbols/Synthesized/TypeSubstitutedLocalSymbol.cs
...ortable/Symbols/Synthesized/TypeSubstitutedLocalSymbol.cs
+3
-3
src/Compilers/CSharp/Test/Semantic/Semantics/ColorColorTests.cs
...mpilers/CSharp/Test/Semantic/Semantics/ColorColorTests.cs
+6
-6
src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs
...Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs
+129
-62
src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs
...lers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs
+9
-5
src/Compilers/Core/Portable/Diagnostic/DiagnosticBag.cs
src/Compilers/Core/Portable/Diagnostic/DiagnosticBag.cs
+27
-0
未找到文件。
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
浏览文件 @
19b00627
...
...
@@ -1052,7 +1052,7 @@ private BoundExpression BindNonMethod(SimpleNameSyntax node, Symbol symbol, Diag
case
SymbolKind
.
Local
:
{
var
localSymbol
=
(
LocalSymbol
)
symbol
;
var
constantValueOpt
=
localSymbol
.
IsConst
?
localSymbol
.
GetConstantValue
(
this
.
LocalInProgres
s
)
:
null
;
var
constantValueOpt
=
localSymbol
.
IsConst
?
localSymbol
.
GetConstantValue
(
node
,
this
.
LocalInProgress
,
diagnostic
s
)
:
null
;
TypeSymbol
type
;
Location
localSymbolLocation
=
localSymbol
.
Locations
[
0
];
...
...
src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
浏览文件 @
19b00627
...
...
@@ -682,6 +682,7 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
Debug
.
Assert
((
object
)
declTypeOpt
!=
null
||
isVar
);
Debug
.
Assert
(
typeSyntax
!=
null
);
var
localDiagnostics
=
DiagnosticBag
.
GetInstance
();
// if we are not given desired syntax, we use declarator
associatedSyntaxNode
=
associatedSyntaxNode
??
declarator
;
...
...
@@ -690,7 +691,7 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
BoundExpression
initializerOpt
;
// Check for variable declaration errors.
hasErrors
|=
this
.
ValidateDeclarationNameConflictsInScope
(
localSymbol
,
d
iagnostics
);
hasErrors
|=
this
.
ValidateDeclarationNameConflictsInScope
(
localSymbol
,
localD
iagnostics
);
EqualsValueClauseSyntax
equalsValueClauseSyntax
=
declarator
.
Initializer
;
if
(
isVar
)
...
...
@@ -698,7 +699,7 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
aliasOpt
=
null
;
var
binder
=
new
ImplicitlyTypedLocalBinder
(
this
,
localSymbol
);
initializerOpt
=
binder
.
BindInferredVariableInitializer
(
d
iagnostics
,
equalsValueClauseSyntax
,
declarator
);
initializerOpt
=
binder
.
BindInferredVariableInitializer
(
localD
iagnostics
,
equalsValueClauseSyntax
,
declarator
);
// If we got a good result then swap the inferred type for the "var"
if
((
object
)
initializerOpt
?.
Type
!=
null
)
...
...
@@ -707,7 +708,7 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
if
(
declTypeOpt
.
SpecialType
==
SpecialType
.
System_Void
)
{
Error
(
d
iagnostics
,
ErrorCode
.
ERR_ImplicitlyTypedVariableAssignedBadValue
,
declarator
,
declTypeOpt
);
Error
(
localD
iagnostics
,
ErrorCode
.
ERR_ImplicitlyTypedVariableAssignedBadValue
,
declarator
,
declTypeOpt
);
declTypeOpt
=
CreateErrorType
(
"var"
);
hasErrors
=
true
;
}
...
...
@@ -716,7 +717,7 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
{
if
(
declTypeOpt
.
IsStatic
)
{
Error
(
d
iagnostics
,
ErrorCode
.
ERR_VarDeclIsStaticClass
,
typeSyntax
,
initializerOpt
.
Type
);
Error
(
localD
iagnostics
,
ErrorCode
.
ERR_VarDeclIsStaticClass
,
typeSyntax
,
initializerOpt
.
Type
);
hasErrors
=
true
;
}
}
...
...
@@ -736,11 +737,11 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
else
{
// Basically inlined BindVariableInitializer, but with conversion optional.
initializerOpt
=
BindPossibleArrayInitializer
(
equalsValueClauseSyntax
.
Value
,
declTypeOpt
,
d
iagnostics
);
initializerOpt
=
BindPossibleArrayInitializer
(
equalsValueClauseSyntax
.
Value
,
declTypeOpt
,
localD
iagnostics
);
if
(
kind
!=
LocalDeclarationKind
.
FixedVariable
)
{
// If this is for a fixed statement, we'll do our own conversion since there are some special cases.
initializerOpt
=
GenerateConversionForAssignment
(
declTypeOpt
,
initializerOpt
,
d
iagnostics
);
initializerOpt
=
GenerateConversionForAssignment
(
declTypeOpt
,
initializerOpt
,
localD
iagnostics
);
}
}
}
...
...
@@ -754,7 +755,7 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
{
if
(!
hasErrors
)
{
Error
(
d
iagnostics
,
ErrorCode
.
ERR_ImplicitlyTypedLocalCannotBeFixed
,
declarator
);
Error
(
localD
iagnostics
,
ErrorCode
.
ERR_ImplicitlyTypedLocalCannotBeFixed
,
declarator
);
hasErrors
=
true
;
}
}
...
...
@@ -763,11 +764,11 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
{
if
(!
hasErrors
)
{
Error
(
d
iagnostics
,
ErrorCode
.
ERR_BadFixedInitType
,
declarator
);
Error
(
localD
iagnostics
,
ErrorCode
.
ERR_BadFixedInitType
,
declarator
);
hasErrors
=
true
;
}
}
else
if
(!
IsValidFixedVariableInitializer
(
declTypeOpt
,
localSymbol
,
ref
initializerOpt
,
d
iagnostics
))
else
if
(!
IsValidFixedVariableInitializer
(
declTypeOpt
,
localSymbol
,
ref
initializerOpt
,
localD
iagnostics
))
{
hasErrors
=
true
;
}
...
...
@@ -777,7 +778,7 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
&&
((
MethodSymbol
)
this
.
ContainingMemberOrLambda
).
IsAsync
&&
declTypeOpt
.
IsRestrictedType
())
{
Error
(
d
iagnostics
,
ErrorCode
.
ERR_BadSpecialByRefLocal
,
typeSyntax
,
declTypeOpt
);
Error
(
localD
iagnostics
,
ErrorCode
.
ERR_BadSpecialByRefLocal
,
typeSyntax
,
declTypeOpt
);
hasErrors
=
true
;
}
...
...
@@ -788,7 +789,7 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
Debug
.
Assert
((
object
)
localSymbol
!=
null
);
ImmutableArray
<
BoundExpression
>
arguments
=
BindDeclaratorArguments
(
declarator
,
d
iagnostics
);
ImmutableArray
<
BoundExpression
>
arguments
=
BindDeclaratorArguments
(
declarator
,
localD
iagnostics
);
if
(
kind
==
LocalDeclarationKind
.
FixedVariable
||
kind
==
LocalDeclarationKind
.
UsingVariable
)
{
...
...
@@ -799,19 +800,21 @@ private TypeSymbol BindVariableType(CSharpSyntaxNode declarationNode, Diagnostic
if
(
initializerOpt
==
null
)
{
Error
(
d
iagnostics
,
ErrorCode
.
ERR_FixedMustInit
,
declarator
);
Error
(
localD
iagnostics
,
ErrorCode
.
ERR_FixedMustInit
,
declarator
);
hasErrors
=
true
;
}
}
else
if
(
kind
==
LocalDeclarationKind
.
Constant
&&
initializerOpt
!=
null
)
else
if
(
kind
==
LocalDeclarationKind
.
Constant
&&
initializerOpt
!=
null
&&
!
localDiagnostics
.
HasAnyResolvedErrors
()
)
{
foreach
(
var
diagnostic
in
localSymbol
.
GetConstantValueDiagnostics
(
initializerOpt
))
var
constantValueDiagnostics
=
localSymbol
.
GetConstantValueDiagnostics
(
initializerOpt
);
foreach
(
var
diagnostic
in
constantValueDiagnostics
)
{
diagnostics
.
Add
(
diagnostic
);
hasErrors
=
true
;
}
}
diagnostics
.
AddRangeAndFree
(
localDiagnostics
);
var
boundDeclType
=
new
BoundTypeExpression
(
typeSyntax
,
aliasOpt
,
inferredType
:
isVar
,
type
:
declTypeOpt
);
return
new
BoundLocalDeclaration
(
associatedSyntaxNode
,
localSymbol
,
boundDeclType
,
initializerOpt
,
arguments
,
hasErrors
);
}
...
...
src/Compilers/CSharp/Portable/CodeGen/Optimizer.cs
浏览文件 @
19b00627
...
...
@@ -1862,7 +1862,7 @@ public override ImmutableArray<SyntaxReference> DeclaringSyntaxReferences
get
{
throw
new
NotImplementedException
();
}
}
internal
override
ConstantValue
GetConstantValue
(
LocalSymbol
inProgres
s
)
internal
override
ConstantValue
GetConstantValue
(
SyntaxNode
node
,
LocalSymbol
inProgress
,
DiagnosticBag
diagnostic
s
)
{
throw
new
NotImplementedException
();
}
...
...
src/Compilers/CSharp/Portable/Symbols/ConstantValueUtils.cs
浏览文件 @
19b00627
...
...
@@ -127,6 +127,7 @@ internal static class ConstantValueUtils
}
}
}
return
value
;
}
...
...
src/Compilers/CSharp/Portable/Symbols/LocalSymbol.cs
浏览文件 @
19b00627
...
...
@@ -289,7 +289,7 @@ public bool HasConstantValue
return
false
;
}
ConstantValue
constantValue
=
this
.
GetConstantValue
(
null
);
ConstantValue
constantValue
=
this
.
GetConstantValue
(
null
,
null
,
null
);
return
constantValue
!=
null
&&
!
constantValue
.
IsBad
;
//can be null in error scenarios
}
}
...
...
@@ -307,9 +307,8 @@ public object ConstantValue
return
null
;
}
ConstantValue
constantValue
=
this
.
GetConstantValue
(
null
);
//null just means we aren't currently evaluating another constant
return
constantValue
==
null
?
null
:
constantValue
.
Value
;
//can be null in error scenarios
ConstantValue
constantValue
=
this
.
GetConstantValue
(
null
,
null
,
null
);
return
constantValue
?.
Value
;
//can be null in error scenarios
}
}
...
...
@@ -321,7 +320,7 @@ public object ConstantValue
get
;
}
internal
abstract
ConstantValue
GetConstantValue
(
LocalSymbol
inProgress
);
internal
abstract
ConstantValue
GetConstantValue
(
SyntaxNode
node
,
LocalSymbol
inProgress
,
DiagnosticBag
diagnostics
=
null
);
internal
abstract
ImmutableArray
<
Diagnostic
>
GetConstantValueDiagnostics
(
BoundExpression
boundInitValue
);
...
...
src/Compilers/CSharp/Portable/Symbols/Source/SourceLocalSymbol.cs
浏览文件 @
19b00627
...
...
@@ -290,7 +290,7 @@ internal override bool IsCompilerGenerated
get
{
return
false
;
}
}
internal
override
ConstantValue
GetConstantValue
(
LocalSymbol
inProgres
s
)
internal
override
ConstantValue
GetConstantValue
(
SyntaxNode
node
,
LocalSymbol
inProgress
,
DiagnosticBag
diagnostic
s
)
{
return
null
;
}
...
...
@@ -374,14 +374,7 @@ private void MakeConstantTuple(LocalSymbol inProgress, BoundExpression boundInit
var
value
=
Microsoft
.
CodeAnalysis
.
ConstantValue
.
Bad
;
var
initValueNodeLocation
=
_initializer
.
Value
.
Location
;
var
diagnostics
=
DiagnosticBag
.
GetInstance
();
if
(
inProgress
==
this
)
{
// The problem is circularity, but Dev12 reports ERR_NotConstantExpression instead of ERR_CircConstValue.
// Also, the native compiler squiggles the RHS for ERR_NotConstantExpression but the LHS for ERR_CircConstValue.
diagnostics
.
Add
(
ErrorCode
.
ERR_NotConstantExpression
,
initValueNodeLocation
,
this
);
}
else
{
Debug
.
Assert
(
inProgress
!=
this
);
var
type
=
this
.
Type
;
if
(
boundInitValue
==
null
)
{
...
...
@@ -390,13 +383,22 @@ private void MakeConstantTuple(LocalSymbol inProgress, BoundExpression boundInit
}
value
=
ConstantValueUtils
.
GetAndValidateConstantValue
(
boundInitValue
,
this
,
type
,
initValueNodeLocation
,
diagnostics
);
}
Interlocked
.
CompareExchange
(
ref
_constantTuple
,
new
EvaluatedConstant
(
value
,
diagnostics
.
ToReadOnlyAndFree
()),
null
);
}
}
internal
override
ConstantValue
GetConstantValue
(
LocalSymbol
inProgress
)
internal
override
ConstantValue
GetConstantValue
(
SyntaxNode
node
,
LocalSymbol
inProgress
,
DiagnosticBag
diagnostics
=
null
)
{
if
(
this
.
IsConst
&&
inProgress
==
this
)
{
if
(
diagnostics
!=
null
)
{
diagnostics
.
Add
(
ErrorCode
.
ERR_CircConstValue
,
node
.
GetLocation
(),
this
);
}
return
Microsoft
.
CodeAnalysis
.
ConstantValue
.
Bad
;
}
MakeConstantTuple
(
inProgress
,
boundInitValue
:
null
);
return
_constantTuple
==
null
?
null
:
_constantTuple
.
Value
;
}
...
...
src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedLocal.cs
浏览文件 @
19b00627
...
...
@@ -152,7 +152,7 @@ internal override bool IsCompilerGenerated
get
{
return
true
;
}
}
internal
override
ConstantValue
GetConstantValue
(
LocalSymbol
inProgres
s
)
internal
override
ConstantValue
GetConstantValue
(
SyntaxNode
node
,
LocalSymbol
inProgress
,
DiagnosticBag
diagnostic
s
)
{
return
null
;
}
...
...
src/Compilers/CSharp/Portable/Symbols/Synthesized/TypeSubstitutedLocalSymbol.cs
浏览文件 @
19b00627
...
...
@@ -88,9 +88,9 @@ internal override RefKind RefKind
get
{
return
_originalVariable
.
RefKind
;
}
}
internal
override
ConstantValue
GetConstantValue
(
LocalSymbol
inProgres
s
)
internal
override
ConstantValue
GetConstantValue
(
SyntaxNode
node
,
LocalSymbol
inProgress
,
DiagnosticBag
diagnostic
s
)
{
return
_originalVariable
.
GetConstantValue
(
inProgres
s
);
return
_originalVariable
.
GetConstantValue
(
node
,
inProgress
,
diagnostic
s
);
}
internal
override
ImmutableArray
<
Diagnostic
>
GetConstantValueDiagnostics
(
BoundExpression
boundInitValue
)
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/ColorColorTests.cs
浏览文件 @
19b00627
...
...
@@ -1809,9 +1809,6 @@ void M()
Assert
.
Equal
(
SymbolKind
.
NamedType
,
symbolInfo
.
Symbol
.
Kind
);
compilation
.
VerifyDiagnostics
(
// (11,21): error CS0133: The expression being assigned to 'E' must be constant
// const E E = E.A;
Diagnostic
(
ErrorCode
.
ERR_NotConstantExpression
,
"E.A"
).
WithArguments
(
"E"
).
WithLocation
(
11
,
21
),
// (12,13): warning CS0219: The variable 'z' is assigned but its value is never used
// var z = E;
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"z"
).
WithArguments
(
"z"
).
WithLocation
(
12
,
13
)
...
...
@@ -1872,6 +1869,9 @@ void M()
// (11,9): error CS0822: Implicitly-typed variables cannot be constant
// const var E = E.A;
Diagnostic
(
ErrorCode
.
ERR_ImplicitlyTypedVariableCannotBeConst
,
"const var E = E.A;"
).
WithLocation
(
11
,
9
),
// (11,23): error CS0110: The evaluation of the constant value for 'E' involves a circular definition
// const var E = E.A;
Diagnostic
(
ErrorCode
.
ERR_CircConstValue
,
"E"
).
WithArguments
(
"E"
).
WithLocation
(
11
,
23
),
// (11,23): error CS0841: Cannot use local variable 'E' before it is declared
// const var E = E.A;
Diagnostic
(
ErrorCode
.
ERR_VariableUsedBeforeDeclaration
,
"E"
).
WithArguments
(
"E"
).
WithLocation
(
11
,
23
)
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs
浏览文件 @
19b00627
...
...
@@ -1615,62 +1615,92 @@ void M()
}"
;
CreateCompilationWithMscorlib
(
source
).
VerifyDiagnostics
(
// (7,15): error CS0283: The type 'C.S' cannot be declared const
Diagnostic
(
ErrorCode
.
ERR_BadConstType
,
"S"
).
WithArguments
(
"C.S"
),
// const S s = new S();
Diagnostic
(
ErrorCode
.
ERR_BadConstType
,
"S"
).
WithArguments
(
"C.S"
).
WithLocation
(
7
,
15
),
// (8,28): error CS0023: Operator '-' cannot be applied to operand of type 'ulong'
Diagnostic
(
ErrorCode
.
ERR_BadUnaryOp
,
"-9223372036854775808UL"
).
WithArguments
(
"-"
,
"ulong"
),
// const double ul1 = -9223372036854775808UL + 0;
Diagnostic
(
ErrorCode
.
ERR_BadUnaryOp
,
"-9223372036854775808UL"
).
WithArguments
(
"-"
,
"ulong"
).
WithLocation
(
8
,
28
),
// (9,28): error CS0023: Operator '-' cannot be applied to operand of type 'ulong'
Diagnostic
(
ErrorCode
.
ERR_BadUnaryOp
,
"-9223372036854775808ul"
).
WithArguments
(
"-"
,
"ulong"
),
// const double ul2 = -9223372036854775808ul + 0;
Diagnostic
(
ErrorCode
.
ERR_BadUnaryOp
,
"-9223372036854775808ul"
).
WithArguments
(
"-"
,
"ulong"
).
WithLocation
(
9
,
28
),
// (12,27): error CS0133: The expression being assigned to 's2' must be constant
Diagnostic
(
ErrorCode
.
ERR_NotConstantExpression
,
"s1"
).
WithArguments
(
"s2"
),
// const string s2 = s1; // Not a constant
Diagnostic
(
ErrorCode
.
ERR_NotConstantExpression
,
"s1"
).
WithArguments
(
"s2"
).
WithLocation
(
12
,
27
),
// (14,27): error CS0134: 'o1' is of type 'object'. A const field of a reference type other than string can only be initialized with null.
Diagnostic
(
ErrorCode
.
ERR_NotNullConstRefField
,
@"""hello"""
).
WithArguments
(
"o1"
,
"object"
),
// (16,18): error CS0020: Division by constant zero
Diagnostic
(
ErrorCode
.
ERR_IntDivByZero
,
"1 / 0"
),
// (16,28): error CS0020: Division by constant zero
Diagnostic
(
ErrorCode
.
ERR_IntDivByZero
,
"1L/0L"
),
// (16,38): error CS0020: Division by constant zero
Diagnostic
(
ErrorCode
.
ERR_IntDivByZero
,
"1UL/0UL"
),
// (16,50): error CS0020: Division by constant zero
Diagnostic
(
ErrorCode
.
ERR_IntDivByZero
,
"1M/0M"
),
// const object o1 = "hello"; // Constants of ref type other than string must be null.
Diagnostic
(
ErrorCode
.
ERR_NotNullConstRefField
,
@"""hello"""
).
WithArguments
(
"o1"
,
"object"
).
WithLocation
(
14
,
27
),
// (16,60): error CS0463: Evaluation of the decimal constant expression failed
Diagnostic
(
ErrorCode
.
ERR_DecConstError
,
"-79228162514264337593543950335m - 1m"
),
// (18,19): error CS0133: The expression being assigned to 'z' must be constant
Diagnostic
(
ErrorCode
.
ERR_NotConstantExpression
,
"1 + (z + 1)"
).
WithArguments
(
"z"
),
// int y = (1 / 0) + (1L/0L) + (1UL/0UL) + (1M/0M) + (-79228162514264337593543950335m - 1m);
Diagnostic
(
ErrorCode
.
ERR_DecConstError
,
"-79228162514264337593543950335m - 1m"
).
WithLocation
(
16
,
60
),
// (16,50): error CS0020: Division by constant zero
// int y = (1 / 0) + (1L/0L) + (1UL/0UL) + (1M/0M) + (-79228162514264337593543950335m - 1m);
Diagnostic
(
ErrorCode
.
ERR_IntDivByZero
,
"1M/0M"
).
WithLocation
(
16
,
50
),
// (16,38): error CS0020: Division by constant zero
// int y = (1 / 0) + (1L/0L) + (1UL/0UL) + (1M/0M) + (-79228162514264337593543950335m - 1m);
Diagnostic
(
ErrorCode
.
ERR_IntDivByZero
,
"1UL/0UL"
).
WithLocation
(
16
,
38
),
// (16,28): error CS0020: Division by constant zero
// int y = (1 / 0) + (1L/0L) + (1UL/0UL) + (1M/0M) + (-79228162514264337593543950335m - 1m);
Diagnostic
(
ErrorCode
.
ERR_IntDivByZero
,
"1L/0L"
).
WithLocation
(
16
,
28
),
// (16,18): error CS0020: Division by constant zero
// int y = (1 / 0) + (1L/0L) + (1UL/0UL) + (1M/0M) + (-79228162514264337593543950335m - 1m);
Diagnostic
(
ErrorCode
.
ERR_IntDivByZero
,
"1 / 0"
).
WithLocation
(
16
,
18
),
// (18,28): error CS0110: The evaluation of the constant value for 'z' involves a circular definition
// const int z = 1 + (z + 1);
Diagnostic
(
ErrorCode
.
ERR_CircConstValue
,
"z"
).
WithArguments
(
"z"
).
WithLocation
(
18
,
28
),
// (20,29): error CS0221: Constant value '9838263505978427528' cannot be converted to a 'int' (use 'unchecked' syntax to override)
Diagnostic
(
ErrorCode
.
ERR_ConstOutOfRangeChecked
,
"(int)0x8888888888888888"
).
WithArguments
(
"9838263505978427528"
,
"int"
),
// int intConversion = (int)0x8888888888888888;
Diagnostic
(
ErrorCode
.
ERR_ConstOutOfRangeChecked
,
"(int)0x8888888888888888"
).
WithArguments
(
"9838263505978427528"
,
"int"
).
WithLocation
(
20
,
29
),
// (21,31): error CS0221: Constant value '9838263505978427528' cannot be converted to a 'uint' (use 'unchecked' syntax to override)
Diagnostic
(
ErrorCode
.
ERR_ConstOutOfRangeChecked
,
"(uint)0x8888888888888888"
).
WithArguments
(
"9838263505978427528"
,
"uint"
),
// uint uintConversion = (uint)0x8888888888888888;
Diagnostic
(
ErrorCode
.
ERR_ConstOutOfRangeChecked
,
"(uint)0x8888888888888888"
).
WithArguments
(
"9838263505978427528"
,
"uint"
).
WithLocation
(
21
,
31
),
// (22,31): error CS0221: Constant value '9838263505978427528' cannot be converted to a 'long' (use 'unchecked' syntax to override)
Diagnostic
(
ErrorCode
.
ERR_ConstOutOfRangeChecked
,
"(long)0x8888888888888888"
).
WithArguments
(
"9838263505978427528"
,
"long"
),
// long longConversion = (long)0x8888888888888888;
Diagnostic
(
ErrorCode
.
ERR_ConstOutOfRangeChecked
,
"(long)0x8888888888888888"
).
WithArguments
(
"9838263505978427528"
,
"long"
).
WithLocation
(
22
,
31
),
// (23,33): error CS0221: Constant value '1E+50' cannot be converted to a 'ulong' (use 'unchecked' syntax to override)
Diagnostic
(
ErrorCode
.
ERR_ConstOutOfRangeChecked
,
"(ulong)1E50"
).
WithArguments
(
"1E+50"
,
"ulong"
),
// ulong ulongConversion = (ulong)1E50;
Diagnostic
(
ErrorCode
.
ERR_ConstOutOfRangeChecked
,
"(ulong)1E50"
).
WithArguments
(
"1E+50"
,
"ulong"
).
WithLocation
(
23
,
33
),
// (25,27): error CS0220: The operation overflows at compile time in checked mode
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"int.MaxValue + 1"
),
// int intOverflow = int.MaxValue + 1;
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"int.MaxValue + 1"
).
WithLocation
(
25
,
27
),
// (26,29): error CS0220: The operation overflows at compile time in checked mode
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"uint.MaxValue + 1"
),
// uint uintOverflow = uint.MaxValue + 1;
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"uint.MaxValue + 1"
).
WithLocation
(
26
,
29
),
// (27,29): error CS0220: The operation overflows at compile time in checked mode
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"long.MaxValue + 1"
),
// long longOverflow = long.MaxValue + 1;
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"long.MaxValue + 1"
).
WithLocation
(
27
,
29
),
// (28,31): error CS0220: The operation overflows at compile time in checked mode
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"ulong.MaxValue + 1"
),
// ulong ulongOverflow = ulong.MaxValue + 1;
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"ulong.MaxValue + 1"
).
WithLocation
(
28
,
31
),
// (30,28): error CS0220: The operation overflows at compile time in checked mode
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"int.MinValue - 1"
),
// int intUnderflow = int.MinValue - 1;
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"int.MinValue - 1"
).
WithLocation
(
30
,
28
),
// (31,30): error CS0220: The operation overflows at compile time in checked mode
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"uint.MinValue - 1"
),
// uint uintUnderflow = uint.MinValue - 1;
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"uint.MinValue - 1"
).
WithLocation
(
31
,
30
),
// (32,30): error CS0220: The operation overflows at compile time in checked mode
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"long.MinValue - 1"
),
// long longUnderflow = long.MinValue - 1;
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"long.MinValue - 1"
).
WithLocation
(
32
,
30
),
// (33,32): error CS0220: The operation overflows at compile time in checked mode
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"ulong.MinValue - 1"
),
// ulong ulongUnderflow = ulong.MinValue - 1;
Diagnostic
(
ErrorCode
.
ERR_CheckedOverflow
,
"ulong.MinValue - 1"
).
WithLocation
(
33
,
32
),
// (7,17): warning CS0219: The variable 's' is assigned but its value is never used
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"s"
).
WithArguments
(
"s"
),
// const S s = new S();
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"s"
).
WithArguments
(
"s"
).
WithLocation
(
7
,
17
),
// (20,13): warning CS0219: The variable 'intConversion' is assigned but its value is never used
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"intConversion"
).
WithArguments
(
"intConversion"
),
// int intConversion = (int)0x8888888888888888;
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"intConversion"
).
WithArguments
(
"intConversion"
).
WithLocation
(
20
,
13
),
// (21,14): warning CS0219: The variable 'uintConversion' is assigned but its value is never used
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"uintConversion"
).
WithArguments
(
"uintConversion"
),
// uint uintConversion = (uint)0x8888888888888888;
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"uintConversion"
).
WithArguments
(
"uintConversion"
).
WithLocation
(
21
,
14
),
// (22,14): warning CS0219: The variable 'longConversion' is assigned but its value is never used
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"longConversion"
).
WithArguments
(
"longConversion"
),
// long longConversion = (long)0x8888888888888888;
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"longConversion"
).
WithArguments
(
"longConversion"
).
WithLocation
(
22
,
14
),
// (23,15): warning CS0219: The variable 'ulongConversion' is assigned but its value is never used
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"ulongConversion"
).
WithArguments
(
"ulongConversion"
));
// ulong ulongConversion = (ulong)1E50;
Diagnostic
(
ErrorCode
.
WRN_UnreferencedVarAssg
,
"ulongConversion"
).
WithArguments
(
"ulongConversion"
).
WithLocation
(
23
,
15
)
);
}
[
Fact
]
public
void
TestDynamicConstantError
()
{
...
...
@@ -2755,6 +2785,43 @@ static void Main(string[] args)
);
}
[
Fact
]
public
static
void
DoubleRecursiveConst
()
{
var
source
=
@"using System;
class C
{
public static void Main()
{
const Func<int> a = () => { const int b = a(); return 1; };
}
}"
;
CreateCompilationWithMscorlib
(
source
).
VerifyDiagnostics
(
// (6,51): error CS0133: The expression being assigned to 'b' must be constant
// const Func<int> a = () => { const int b = a(); return 1; };
Diagnostic
(
ErrorCode
.
ERR_NotConstantExpression
,
"a()"
).
WithArguments
(
"b"
).
WithLocation
(
6
,
51
)
);
}
[
Fact
]
public
static
void
RecursiveConst
()
{
var
source
=
@"class C
{
public static void Main()
{
const int z = 1 + z + 1;
}
}"
;
CreateCompilationWithMscorlib
(
source
).
VerifyDiagnostics
(
// (5,27): error CS0110: The evaluation of the constant value for 'z' involves a circular definition
// const int z = 1 + z + 1;
Diagnostic
(
ErrorCode
.
ERR_CircConstValue
,
"z"
).
WithArguments
(
"z"
).
WithLocation
(
5
,
27
)
);
}
[
Fact
,
WorkItem
(
1098197
,
"DevDiv"
)]
public
static
void
Bug1098197_02
()
{
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs
浏览文件 @
19b00627
...
...
@@ -3581,17 +3581,21 @@ static void M()
[Fact]
public void CS0133ERR_NotConstantExpression04()
{
DiagnosticsUtils.TestDiagnosticsExact(
var source =
@"class C
{
static void M()
{
const int x = x + x;
}
}
",
//"'x + x' error CS0133: The expression being assigned to 'x' must be constant",
"'x + x' error CS0133: The expression being assigned to 'x' must be constant");
}";
CreateCompilationWithMscorlib45(source).VerifyDiagnostics(
// (5,27): error CS0110: The evaluation of the constant value for 'x' involves a circular definition
// const int x = x + x;
Diagnostic(ErrorCode.ERR_CircConstValue, "x").WithArguments("x").WithLocation(5, 27),
// (5,23): error CS0110: The evaluation of the constant value for 'x' involves a circular definition
// const int x = x + x;
Diagnostic(ErrorCode.ERR_CircConstValue, "x").WithArguments("x").WithLocation(5, 23));
}
[Fact]
...
...
src/Compilers/Core/Portable/Diagnostic/DiagnosticBag.cs
浏览文件 @
19b00627
...
...
@@ -76,6 +76,33 @@ public bool HasAnyErrors()
return
false
;
}
/// <summary>
/// Returns true if the bag has any non-lazy diagnostics with Severity=Error.
/// </summary>
/// <remarks>
/// Does not resolve any lazy diagnostics in the bag.
///
/// Generally, this should only be called by the creator (modulo pooling) of the bag (i.e. don't use bags to communicate -
/// if you need more info, pass more info).
/// </remarks>
internal
bool
HasAnyResolvedErrors
()
{
if
(
IsEmptyWithoutResolution
)
{
return
false
;
}
foreach
(
Diagnostic
diagnostic
in
Bag
)
{
if
((
diagnostic
as
DiagnosticWithInfo
)?.
HasLazyInfo
!=
true
&&
diagnostic
.
Severity
==
DiagnosticSeverity
.
Error
)
{
return
true
;
}
}
return
false
;
}
/// <summary>
/// Add a diagnostic to the bag.
/// </summary>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录