Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
8176685d
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,发现更多精彩内容 >>
未验证
提交
8176685d
编写于
11月 12, 2018
作者:
J
Julien Couvreur
提交者:
GitHub
11月 12, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #28115 from wachulski/fix/27866/else-without-if-error-message
Fix 'else without if' parsing - better error message
上级
14992b1f
701170ce
变更
19
展开全部
隐藏空白更改
内联
并排
Showing
19 changed file
with
813 addition
and
17 deletion
+813
-17
src/Compilers/CSharp/Portable/CSharpResources.Designer.cs
src/Compilers/CSharp/Portable/CSharpResources.Designer.cs
+9
-0
src/Compilers/CSharp/Portable/CSharpResources.resx
src/Compilers/CSharp/Portable/CSharpResources.resx
+4
-1
src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
+2
-1
src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
+12
-5
src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
...Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
+5
-0
src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
...Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
+5
-0
src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIfStatement.cs
...est/IOperation/IOperation/IOperationTests_IIfStatement.cs
+382
-10
src/Compilers/CSharp/Test/Syntax/Parsing/StatementParsingTests.cs
...ilers/CSharp/Test/Syntax/Parsing/StatementParsingTests.cs
+339
-0
未找到文件。
src/Compilers/CSharp/Portable/CSharpResources.Designer.cs
浏览文件 @
8176685d
...
...
@@ -3930,6 +3930,15 @@ internal class CSharpResources {
}
}
/// <summary>
/// Looks up a localized string similar to 'else' cannot start a statement..
/// </summary>
internal static string ERR_ElseCannotStartStatement {
get {
return ResourceManager.GetString("ERR_ElseCannotStartStatement", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Empty character literal.
/// </summary>
...
...
src/Compilers/CSharp/Portable/CSharpResources.resx
浏览文件 @
8176685d
...
...
@@ -5638,4 +5638,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data
name=
"ERR_ExpressionTreeCantContainRefStruct"
xml:space=
"preserve"
>
<value>
Expression tree cannot contain value of ref struct or restricted type '{0}'.
</value>
</data>
</root>
<data
name=
"ERR_ElseCannotStartStatement"
xml:space=
"preserve"
>
<value>
'else' cannot start a statement.
</value>
</data>
</root>
\ No newline at end of file
src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
浏览文件 @
8176685d
...
...
@@ -1635,7 +1635,8 @@ internal enum ErrorCode
ERR_NullableDirectiveQualifierExpected
=
8637
,
WRN_CantInferNullabilityOfMethodTypeArgs
=
8638
,
WRN_NoBestNullabilityArrayElements
=
8639
,
ERR_ExpressionTreeCantContainRefStruct
=
8640
ERR_ExpressionTreeCantContainRefStruct
=
8640
,
ERR_ElseCannotStartStatement
=
8641
#
endregion
diagnostics
introduced
for
C
#
8.0
...
...
src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
浏览文件 @
8176685d
...
...
@@ -6678,6 +6678,7 @@ private StatementSyntax ParseStatementNoDeclaration(bool allowAnyExpression)
case
SyntaxKind
.
GotoKeyword
:
return
this
.
ParseGotoStatement
();
case
SyntaxKind
.
IfKeyword
:
case
SyntaxKind
.
ElseKeyword
:
// Including 'else' keyword to handle 'else without if' error cases
return
this
.
ParseIfStatement
();
case
SyntaxKind
.
LockKeyword
:
return
this
.
ParseLockStatement
();
...
...
@@ -7338,6 +7339,7 @@ private bool IsPossibleStatement(bool acceptAccessibilityMods)
case
SyntaxKind
.
ForEachKeyword
:
case
SyntaxKind
.
GotoKeyword
:
case
SyntaxKind
.
IfKeyword
:
case
SyntaxKind
.
ElseKeyword
:
case
SyntaxKind
.
LockKeyword
:
case
SyntaxKind
.
ReturnKeyword
:
case
SyntaxKind
.
SwitchKeyword
:
...
...
@@ -7933,14 +7935,18 @@ private GotoStatementSyntax ParseGotoStatement()
private
IfStatementSyntax
ParseIfStatement
()
{
Debug
.
Assert
(
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
IfKeyword
);
var
@if
=
this
.
EatToken
(
SyntaxKind
.
IfKeyword
);
Debug
.
Assert
(
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
IfKeyword
||
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
ElseKeyword
);
bool
firstTokenIsElse
=
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
ElseKeyword
;
var
@if
=
firstTokenIsElse
?
this
.
EatToken
(
SyntaxKind
.
IfKeyword
,
ErrorCode
.
ERR_ElseCannotStartStatement
)
:
this
.
EatToken
(
SyntaxKind
.
IfKeyword
);
var
openParen
=
this
.
EatToken
(
SyntaxKind
.
OpenParenToken
);
var
condition
=
this
.
ParseExpressionCore
();
var
closeParen
=
this
.
EatToken
(
SyntaxKind
.
CloseParenToken
);
var
statement
=
this
.
ParseEmbeddedStatement
();
var
elseClause
=
ParseElseClauseOpt
();
var
statement
=
firstTokenIsElse
?
this
.
ParseExpressionStatement
()
:
this
.
ParseEmbeddedStatement
();
var
elseClause
=
this
.
ParseElseClauseOpt
();
return
_syntaxFactory
.
IfStatement
(
@if
,
openParen
,
condition
,
closeParen
,
statement
,
elseClause
);
}
...
...
@@ -8775,6 +8781,7 @@ private static bool IsInvalidSubExpression(SyntaxKind kind)
case
SyntaxKind
.
ForEachKeyword
:
case
SyntaxKind
.
GotoKeyword
:
case
SyntaxKind
.
IfKeyword
:
case
SyntaxKind
.
ElseKeyword
:
case
SyntaxKind
.
LockKeyword
:
case
SyntaxKind
.
ReturnKeyword
:
case
SyntaxKind
.
SwitchKeyword
:
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target
state=
"new"
>
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"ERR_ElseCannotStartStatement"
>
<source>
'else' cannot start a statement.
</source>
<target
state=
"new"
>
'else' cannot start a statement.
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"ERR_ExplicitNonNullTypesAttribute"
>
<source>
Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
</source>
<target
state=
"new"
>
Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target
state=
"new"
>
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"ERR_ElseCannotStartStatement"
>
<source>
'else' cannot start a statement.
</source>
<target
state=
"new"
>
'else' cannot start a statement.
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"ERR_ExplicitNonNullTypesAttribute"
>
<source>
Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
</source>
<target
state=
"new"
>
Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target state="new">Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ElseCannotStartStatement">
<source>'else' cannot start a statement.</source>
<target state="new">'else' cannot start a statement.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExplicitNonNullTypesAttribute">
<source>Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</source>
<target state="new">Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target
state=
"new"
>
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"ERR_ElseCannotStartStatement"
>
<source>
'else' cannot start a statement.
</source>
<target
state=
"new"
>
'else' cannot start a statement.
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"ERR_ExplicitNonNullTypesAttribute"
>
<source>
Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
</source>
<target
state=
"new"
>
Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
</target>
...
...
src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
浏览文件 @
8176685d
...
...
@@ -47,6 +47,11 @@
<target
state=
"new"
>
Type parameter '{1}' has the 'unmanaged' constraint so '{1}' cannot be used as a constraint for '{0}'
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"ERR_ElseCannotStartStatement"
>
<source>
'else' cannot start a statement.
</source>
<target
state=
"new"
>
'else' cannot start a statement.
</target>
<note
/>
</trans-unit>
<trans-unit
id=
"ERR_ExplicitNonNullTypesAttribute"
>
<source>
Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
</source>
<target
state=
"new"
>
Explicit application of 'System.Runtime.CompilerServices.NonNullTypesAttribute' is not allowed.
</target>
...
...
src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIfStatement.cs
浏览文件 @
8176685d
此差异已折叠。
点击以展开。
src/Compilers/CSharp/Test/Syntax/Parsing/StatementParsingTests.cs
浏览文件 @
8176685d
...
...
@@ -2054,6 +2054,40 @@ public void TestIfElse()
Assert
.
NotNull
(
ss
.
Else
.
Statement
);
}
[
Fact
]
public
void
TestIfElseIf
()
{
var
text
=
"if (a) { } else if (b) { }"
;
var
statement
=
this
.
ParseStatement
(
text
);
Assert
.
NotNull
(
statement
);
Assert
.
Equal
(
SyntaxKind
.
IfStatement
,
statement
.
Kind
());
Assert
.
Equal
(
text
,
statement
.
ToString
());
Assert
.
Equal
(
0
,
statement
.
Errors
().
Length
);
var
ss
=
(
IfStatementSyntax
)
statement
;
Assert
.
NotNull
(
ss
.
IfKeyword
);
Assert
.
Equal
(
SyntaxKind
.
IfKeyword
,
ss
.
IfKeyword
.
Kind
());
Assert
.
NotNull
(
ss
.
OpenParenToken
);
Assert
.
NotNull
(
ss
.
Condition
);
Assert
.
Equal
(
"a"
,
ss
.
Condition
.
ToString
());
Assert
.
NotNull
(
ss
.
CloseParenToken
);
Assert
.
NotNull
(
ss
.
Statement
);
Assert
.
NotNull
(
ss
.
Else
);
Assert
.
NotNull
(
ss
.
Else
.
ElseKeyword
);
Assert
.
Equal
(
SyntaxKind
.
ElseKeyword
,
ss
.
Else
.
ElseKeyword
.
Kind
());
Assert
.
NotNull
(
ss
.
Else
.
Statement
);
var
subIf
=
(
IfStatementSyntax
)
ss
.
Else
.
Statement
;
Assert
.
NotNull
(
subIf
.
IfKeyword
);
Assert
.
Equal
(
SyntaxKind
.
IfKeyword
,
subIf
.
IfKeyword
.
Kind
());
Assert
.
NotNull
(
subIf
.
Condition
);
Assert
.
Equal
(
"b"
,
subIf
.
Condition
.
ToString
());
Assert
.
NotNull
(
subIf
.
CloseParenToken
);
Assert
.
NotNull
(
subIf
.
Statement
);
}
[
Fact
]
public
void
TestLock
()
{
...
...
@@ -2741,6 +2775,311 @@ public void NullExceptionInLabeledStatement()
);
}
[
WorkItem
(
27866
,
"https://github.com/dotnet/roslyn/issues/27866"
)]
[
Fact
]
public
void
ParseElseWithoutPrecedingIfStatement
()
{
UsingStatement
(
"else {}"
,
// (1,1): error CS8641: 'else' cannot start a statement.
// else {}
Diagnostic
(
ErrorCode
.
ERR_ElseCannotStartStatement
,
"else"
).
WithLocation
(
1
,
1
),
// (1,1): error CS1003: Syntax error, '(' expected
// else {}
Diagnostic
(
ErrorCode
.
ERR_SyntaxError
,
"else"
).
WithArguments
(
"("
,
"else"
).
WithLocation
(
1
,
1
),
// (1,1): error CS1525: Invalid expression term 'else'
// else {}
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
1
),
// (1,1): error CS1026: ) expected
// else {}
Diagnostic
(
ErrorCode
.
ERR_CloseParenExpected
,
"else"
).
WithLocation
(
1
,
1
),
// (1,1): error CS1525: Invalid expression term 'else'
// else {}
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
1
),
// (1,1): error CS1002: ; expected
// else {}
Diagnostic
(
ErrorCode
.
ERR_SemicolonExpected
,
"else"
).
WithLocation
(
1
,
1
)
);
N
(
SyntaxKind
.
IfStatement
);
{
M
(
SyntaxKind
.
IfKeyword
);
M
(
SyntaxKind
.
OpenParenToken
);
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
CloseParenToken
);
M
(
SyntaxKind
.
ExpressionStatement
);
{
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
SemicolonToken
);
}
N
(
SyntaxKind
.
ElseClause
);
{
N
(
SyntaxKind
.
ElseKeyword
);
N
(
SyntaxKind
.
Block
);
{
N
(
SyntaxKind
.
OpenBraceToken
);
N
(
SyntaxKind
.
CloseBraceToken
);
}
}
}
EOF
();
}
[
WorkItem
(
27866
,
"https://github.com/dotnet/roslyn/issues/27866"
)]
[
Fact
]
public
void
ParseElseAndElseWithoutPrecedingIfStatement
()
{
UsingStatement
(
"{ else {} else {} }"
,
// (1,3): error CS8641: 'else' cannot start a statement.
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_ElseCannotStartStatement
,
"else"
).
WithLocation
(
1
,
3
),
// (1,3): error CS1003: Syntax error, '(' expected
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_SyntaxError
,
"else"
).
WithArguments
(
"("
,
"else"
).
WithLocation
(
1
,
3
),
// (1,3): error CS1525: Invalid expression term 'else'
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
3
),
// (1,3): error CS1026: ) expected
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_CloseParenExpected
,
"else"
).
WithLocation
(
1
,
3
),
// (1,3): error CS1525: Invalid expression term 'else'
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
3
),
// (1,3): error CS1002: ; expected
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_SemicolonExpected
,
"else"
).
WithLocation
(
1
,
3
),
// (1,11): error CS8641: 'else' cannot start a statement.
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_ElseCannotStartStatement
,
"else"
).
WithLocation
(
1
,
11
),
// (1,11): error CS1003: Syntax error, '(' expected
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_SyntaxError
,
"else"
).
WithArguments
(
"("
,
"else"
).
WithLocation
(
1
,
11
),
// (1,11): error CS1525: Invalid expression term 'else'
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
11
),
// (1,11): error CS1026: ) expected
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_CloseParenExpected
,
"else"
).
WithLocation
(
1
,
11
),
// (1,11): error CS1525: Invalid expression term 'else'
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
11
),
// (1,11): error CS1002: ; expected
// { else {} else {} }
Diagnostic
(
ErrorCode
.
ERR_SemicolonExpected
,
"else"
).
WithLocation
(
1
,
11
)
);
N
(
SyntaxKind
.
Block
);
{
N
(
SyntaxKind
.
OpenBraceToken
);
N
(
SyntaxKind
.
IfStatement
);
{
M
(
SyntaxKind
.
IfKeyword
);
M
(
SyntaxKind
.
OpenParenToken
);
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
CloseParenToken
);
M
(
SyntaxKind
.
ExpressionStatement
);
{
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
SemicolonToken
);
}
N
(
SyntaxKind
.
ElseClause
);
{
N
(
SyntaxKind
.
ElseKeyword
);
N
(
SyntaxKind
.
Block
);
{
N
(
SyntaxKind
.
OpenBraceToken
);
N
(
SyntaxKind
.
CloseBraceToken
);
}
}
}
N
(
SyntaxKind
.
IfStatement
);
{
M
(
SyntaxKind
.
IfKeyword
);
M
(
SyntaxKind
.
OpenParenToken
);
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
CloseParenToken
);
M
(
SyntaxKind
.
ExpressionStatement
);
{
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
SemicolonToken
);
}
N
(
SyntaxKind
.
ElseClause
);
{
N
(
SyntaxKind
.
ElseKeyword
);
N
(
SyntaxKind
.
Block
);
{
N
(
SyntaxKind
.
OpenBraceToken
);
N
(
SyntaxKind
.
CloseBraceToken
);
}
}
}
N
(
SyntaxKind
.
CloseBraceToken
);
}
EOF
();
}
[
WorkItem
(
27866
,
"https://github.com/dotnet/roslyn/issues/27866"
)]
[
Fact
]
public
void
ParseSubsequentElseWithoutPrecedingIfStatement
()
{
UsingStatement
(
"{ if (a) { } else { } else { } }"
,
// (1,23): error CS8641: 'else' cannot start a statement.
// { if (a) { } else { } else { } }
Diagnostic
(
ErrorCode
.
ERR_ElseCannotStartStatement
,
"else"
).
WithLocation
(
1
,
23
),
// (1,23): error CS1003: Syntax error, '(' expected
// { if (a) { } else { } else { } }
Diagnostic
(
ErrorCode
.
ERR_SyntaxError
,
"else"
).
WithArguments
(
"("
,
"else"
).
WithLocation
(
1
,
23
),
// (1,23): error CS1525: Invalid expression term 'else'
// { if (a) { } else { } else { } }
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
23
),
// (1,23): error CS1026: ) expected
// { if (a) { } else { } else { } }
Diagnostic
(
ErrorCode
.
ERR_CloseParenExpected
,
"else"
).
WithLocation
(
1
,
23
),
// (1,23): error CS1525: Invalid expression term 'else'
// { if (a) { } else { } else { } }
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
23
),
// (1,23): error CS1002: ; expected
// { if (a) { } else { } else { } }
Diagnostic
(
ErrorCode
.
ERR_SemicolonExpected
,
"else"
).
WithLocation
(
1
,
23
)
);
N
(
SyntaxKind
.
Block
);
{
N
(
SyntaxKind
.
OpenBraceToken
);
N
(
SyntaxKind
.
IfStatement
);
{
N
(
SyntaxKind
.
IfKeyword
);
N
(
SyntaxKind
.
OpenParenToken
);
N
(
SyntaxKind
.
IdentifierName
);
{
N
(
SyntaxKind
.
IdentifierToken
,
"a"
);
}
N
(
SyntaxKind
.
CloseParenToken
);
N
(
SyntaxKind
.
Block
);
{
N
(
SyntaxKind
.
OpenBraceToken
);
N
(
SyntaxKind
.
CloseBraceToken
);
}
N
(
SyntaxKind
.
ElseClause
);
{
N
(
SyntaxKind
.
ElseKeyword
);
N
(
SyntaxKind
.
Block
);
{
N
(
SyntaxKind
.
OpenBraceToken
);
N
(
SyntaxKind
.
CloseBraceToken
);
}
}
}
N
(
SyntaxKind
.
IfStatement
);
{
M
(
SyntaxKind
.
IfKeyword
);
M
(
SyntaxKind
.
OpenParenToken
);
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
CloseParenToken
);
M
(
SyntaxKind
.
ExpressionStatement
);
{
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
SemicolonToken
);
}
N
(
SyntaxKind
.
ElseClause
);
{
N
(
SyntaxKind
.
ElseKeyword
);
N
(
SyntaxKind
.
Block
);
{
N
(
SyntaxKind
.
OpenBraceToken
);
N
(
SyntaxKind
.
CloseBraceToken
);
}
}
}
N
(
SyntaxKind
.
CloseBraceToken
);
}
EOF
();
}
[
WorkItem
(
27866
,
"https://github.com/dotnet/roslyn/issues/27866"
)]
[
Fact
]
public
void
ParseElseKeywordPlacedAsIfEmbeddedStatement
()
{
UsingStatement
(
"if (a) else {}"
,
// (1,8): error CS8641: 'else' cannot start a statement.
// if (a) else {}
Diagnostic
(
ErrorCode
.
ERR_ElseCannotStartStatement
,
"else"
).
WithLocation
(
1
,
8
),
// (1,8): error CS1003: Syntax error, '(' expected
// if (a) else {}
Diagnostic
(
ErrorCode
.
ERR_SyntaxError
,
"else"
).
WithArguments
(
"("
,
"else"
).
WithLocation
(
1
,
8
),
// (1,8): error CS1525: Invalid expression term 'else'
// if (a) else {}
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
8
),
// (1,8): error CS1026: ) expected
// if (a) else {}
Diagnostic
(
ErrorCode
.
ERR_CloseParenExpected
,
"else"
).
WithLocation
(
1
,
8
),
// (1,8): error CS1525: Invalid expression term 'else'
// if (a) else {}
Diagnostic
(
ErrorCode
.
ERR_InvalidExprTerm
,
"else"
).
WithArguments
(
"else"
).
WithLocation
(
1
,
8
),
// (1,8): error CS1002: ; expected
// if (a) else {}
Diagnostic
(
ErrorCode
.
ERR_SemicolonExpected
,
"else"
).
WithLocation
(
1
,
8
)
);
N
(
SyntaxKind
.
IfStatement
);
{
N
(
SyntaxKind
.
IfKeyword
);
N
(
SyntaxKind
.
OpenParenToken
);
N
(
SyntaxKind
.
IdentifierName
);
{
N
(
SyntaxKind
.
IdentifierToken
,
"a"
);
}
N
(
SyntaxKind
.
CloseParenToken
);
N
(
SyntaxKind
.
IfStatement
);
{
M
(
SyntaxKind
.
IfKeyword
);
M
(
SyntaxKind
.
OpenParenToken
);
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
CloseParenToken
);
M
(
SyntaxKind
.
ExpressionStatement
);
{
M
(
SyntaxKind
.
IdentifierName
);
{
M
(
SyntaxKind
.
IdentifierToken
);
}
M
(
SyntaxKind
.
SemicolonToken
);
}
N
(
SyntaxKind
.
ElseClause
);
{
N
(
SyntaxKind
.
ElseKeyword
);
N
(
SyntaxKind
.
Block
);
{
N
(
SyntaxKind
.
OpenBraceToken
);
N
(
SyntaxKind
.
CloseBraceToken
);
}
}
}
}
EOF
();
}
private
sealed
class
TokenAndTriviaWalker
:
CSharpSyntaxWalker
{
public
int
Tokens
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录