Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
c4598d72
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,发现更多精彩内容 >>
未验证
提交
c4598d72
编写于
11月 13, 2018
作者:
C
Charles Stoner
提交者:
GitHub
11月 13, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Parse array type in conditional operator (#31050)
上级
d1e0dd9f
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
700 addition
and
26 deletion
+700
-26
src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
+39
-26
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
...rp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
+66
-0
src/Compilers/CSharp/Test/Syntax/Parsing/NullableParsingTests.cs
...pilers/CSharp/Test/Syntax/Parsing/NullableParsingTests.cs
+595
-0
未找到文件。
src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
浏览文件 @
c4598d72
...
...
@@ -4524,7 +4524,7 @@ private static bool CanReuseVariableDeclarator(CSharp.Syntax.VariableDeclaratorS
case
SyntaxKind
.
OpenBracketToken
:
bool
sawNonOmittedSize
;
_termState
|=
TerminatorState
.
IsPossibleEndOfVariableDeclaration
;
var
specifier
=
this
.
ParseArrayRankSpecifier
(
isArrayCreation
:
false
,
expectSizes
:
flags
==
VariableFlags
.
Fixed
,
allowQuestionToken
:
false
,
sawNonOmittedSize
:
out
sawNonOmittedSize
);
var
specifier
=
this
.
ParseArrayRankSpecifier
(
isArrayCreation
:
false
,
expectSizes
:
flags
==
VariableFlags
.
Fixed
,
questionTokenModeOpt
:
null
,
sawNonOmittedSize
:
out
sawNonOmittedSize
);
_termState
=
saveTerm
;
var
open
=
specifier
.
OpenBracketToken
;
var
sizes
=
specifier
.
Sizes
;
...
...
@@ -6214,7 +6214,6 @@ private enum ParseTypeMode
ParseTypeMode
mode
,
bool
expectSizes
)
{
var
isOrAs
=
mode
==
ParseTypeMode
.
AsExpression
||
mode
==
ParseTypeMode
.
AfterIs
;
NameOptions
nameOptions
;
switch
(
mode
)
{
...
...
@@ -6244,31 +6243,15 @@ private enum ParseTypeMode
}
var
type
=
this
.
ParseUnderlyingType
(
parentIsParameter
:
mode
==
ParseTypeMode
.
Parameter
,
options
:
nameOptions
);
Debug
.
Assert
(
type
!=
null
);
if
(
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
QuestionToken
&&
// we do not permit nullable types in a declaration pattern
(
mode
!=
ParseTypeMode
.
AfterIs
&&
mode
!=
ParseTypeMode
.
AfterCase
||
!
IsTrueIdentifier
(
this
.
PeekToken
(
1
))))
if
(
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
QuestionToken
)
{
var
resetPoint
=
this
.
GetResetPoint
(
);
try
var
question
=
EatNullableQualifierIfApplicable
(
mode
);
if
(
question
!=
null
)
{
var
question
=
this
.
EatToken
();
if
(
isOrAs
&&
(
IsTerm
()
||
IsPredefinedType
(
this
.
CurrentToken
.
Kind
)
||
SyntaxFacts
.
IsAnyUnaryExpression
(
this
.
CurrentToken
.
Kind
)))
{
this
.
Reset
(
ref
resetPoint
);
Debug
.
Assert
(
type
!=
null
);
return
type
;
}
question
=
CheckFeatureAvailability
(
question
,
MessageID
.
IDS_FeatureNullable
);
type
=
_syntaxFactory
.
NullableType
(
type
,
question
);
}
finally
{
this
.
Release
(
ref
resetPoint
);
}
}
switch
(
mode
)
...
...
@@ -6301,7 +6284,7 @@ private enum ParseTypeMode
while
(
this
.
IsPossibleRankAndDimensionSpecifier
())
{
bool
unused
;
var
rank
=
this
.
ParseArrayRankSpecifier
(
mode
==
ParseTypeMode
.
ArrayCreation
,
expectSizes
,
allowQuestionToken
:
tru
e
,
out
unused
);
var
rank
=
this
.
ParseArrayRankSpecifier
(
mode
==
ParseTypeMode
.
ArrayCreation
,
expectSizes
,
questionTokenModeOpt
:
mod
e
,
out
unused
);
ranks
.
Add
(
rank
);
expectSizes
=
false
;
}
...
...
@@ -6318,6 +6301,36 @@ private enum ParseTypeMode
return
type
;
}
private
SyntaxToken
EatNullableQualifierIfApplicable
(
ParseTypeMode
mode
)
{
Debug
.
Assert
(
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
QuestionToken
);
// we do not permit nullable types in a declaration pattern
if
(
mode
!=
ParseTypeMode
.
AfterIs
&&
mode
!=
ParseTypeMode
.
AfterCase
||
!
IsTrueIdentifier
(
this
.
PeekToken
(
1
)))
{
var
resetPoint
=
this
.
GetResetPoint
();
try
{
var
question
=
this
.
EatToken
();
var
isOrAs
=
mode
==
ParseTypeMode
.
AsExpression
||
mode
==
ParseTypeMode
.
AfterIs
;
if
(
isOrAs
&&
(
IsTerm
()
||
IsPredefinedType
(
this
.
CurrentToken
.
Kind
)
||
SyntaxFacts
.
IsAnyUnaryExpression
(
this
.
CurrentToken
.
Kind
)))
{
this
.
Reset
(
ref
resetPoint
);
return
null
;
}
return
CheckFeatureAvailability
(
question
,
MessageID
.
IDS_FeatureNullable
);
}
finally
{
this
.
Release
(
ref
resetPoint
);
}
}
return
null
;
}
private
bool
PointerTypeModsFollowedByRankAndDimensionSpecifier
()
{
// Are pointer specifiers (if any) followed by an array specifier?
...
...
@@ -6340,7 +6353,7 @@ private bool IsPossibleRankAndDimensionSpecifier()
return
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
OpenBracketToken
;
}
private
ArrayRankSpecifierSyntax
ParseArrayRankSpecifier
(
bool
isArrayCreation
,
bool
expectSizes
,
bool
allowQuestionToken
,
out
bool
sawNonOmittedSize
)
private
ArrayRankSpecifierSyntax
ParseArrayRankSpecifier
(
bool
isArrayCreation
,
bool
expectSizes
,
ParseTypeMode
?
questionTokenModeOpt
,
out
bool
sawNonOmittedSize
)
{
sawNonOmittedSize
=
false
;
bool
sawOmittedSize
=
false
;
...
...
@@ -6407,9 +6420,9 @@ private ArrayRankSpecifierSyntax ParseArrayRankSpecifier(bool isArrayCreation, b
var
close
=
this
.
EatToken
(
SyntaxKind
.
CloseBracketToken
);
SyntaxToken
questionToken
=
null
;
if
(
allowQuestionToken
&&
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
QuestionToken
)
if
(
questionTokenModeOpt
!=
null
&&
this
.
CurrentToken
.
Kind
==
SyntaxKind
.
QuestionToken
)
{
questionToken
=
this
.
EatToken
(
);
questionToken
=
EatNullableQualifierIfApplicable
(
questionTokenModeOpt
.
GetValueOrDefault
()
);
}
return
_syntaxFactory
.
ArrayRankSpecifier
(
open
,
list
,
close
,
questionToken
);
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
浏览文件 @
c4598d72
...
...
@@ -456,6 +456,72 @@ static void F2(object? w)
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(14, 26));
}
[Fact]
public void NullableAndConditionalOperators()
{
var source =
@"class Program
{
static void F1(object x)
{
_ = x is string? 1 : 2;
_ = x is string? ? 1 : 2;
_ = x is string ? ? 1 : 2;
_ = x as string?? x;
_ = x as string ? ?? x;
}
static void F2(object y)
{
_ = y is object[]? 1 : 2;
_ = y is object[]? ? 1 : 2;
_ = y is object[] ? ? 1 : 2;
_ = y as object[]?? y;
_ = y as object[] ? ?? y;
}
static void F3<T>(object z)
{
_ = z is T[][]? 1 : 2;
_ = z is T[]?[] ? 1 : 2;
_ = z is T[] ? [] ? 1 : 2;
_ = z as T[][]?? z;
_ = z as T[] ? [] ?? z;
}
}";
var comp = CreateCompilation(source, parseOptions: TestOptions.Regular7);
comp.VerifyDiagnostics(
// (6,24): error CS8107: Feature 'nullable reference types' is not available in C# 7.0. Please use language version 8.0 or greater.
// _ = x is string? ? 1 : 2;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "?").WithArguments("nullable reference types", "8.0").WithLocation(6, 24),
// (7,25): error CS8107: Feature 'nullable reference types' is not available in C# 7.0. Please use language version 8.0 or greater.
// _ = x is string ? ? 1 : 2;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "?").WithArguments("nullable reference types", "8.0").WithLocation(7, 25),
// (9,25): error CS8107: Feature 'nullable reference types' is not available in C# 7.0. Please use language version 8.0 or greater.
// _ = x as string ? ?? x;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "?").WithArguments("nullable reference types", "8.0").WithLocation(9, 25),
// (14,26): error CS8107: Feature 'nullable reference types' is not available in C# 7.0. Please use language version 8.0 or greater.
// _ = y is object[]? ? 1 : 2;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "?").WithArguments("nullable reference types", "8.0").WithLocation(14, 26),
// (15,27): error CS8107: Feature 'nullable reference types' is not available in C# 7.0. Please use language version 8.0 or greater.
// _ = y is object[] ? ? 1 : 2;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "?").WithArguments("nullable reference types", "8.0").WithLocation(15, 27),
// (17,27): error CS8107: Feature 'nullable reference types' is not available in C# 7.0. Please use language version 8.0 or greater.
// _ = y as object[] ? ?? y;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "?").WithArguments("nullable reference types", "8.0").WithLocation(17, 27),
// (22,21): error CS8107: Feature 'nullable reference types' is not available in C# 7.0. Please use language version 8.0 or greater.
// _ = z is T[]?[] ? 1 : 2;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "?").WithArguments("nullable reference types", "8.0").WithLocation(22, 21),
// (23,22): error CS8107: Feature 'nullable reference types' is not available in C# 7.0. Please use language version 8.0 or greater.
// _ = z is T[] ? [] ? 1 : 2;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "?").WithArguments("nullable reference types", "8.0").WithLocation(23, 22),
// (25,22): error CS8107: Feature 'nullable reference types' is not available in C# 7.0. Please use language version 8.0 or greater.
// _ = z as T[] ? [] ?? z;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "?").WithArguments("nullable reference types", "8.0").WithLocation(25, 22));
comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics();
}
[Fact, WorkItem(29318, "https://github.com/dotnet/roslyn/issues/29318")]
public void IsOperatorOnNonNullExpression()
{
src/Compilers/CSharp/Test/Syntax/Parsing/NullableParsingTests.cs
浏览文件 @
c4598d72
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录