Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
cb9ee8ce
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,发现更多精彩内容 >>
提交
cb9ee8ce
编写于
5月 30, 2017
作者:
T
Ty Overby
提交者:
GitHub
5月 30, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #18657 from TyOverby/optimize-is-null
Optimize is null
上级
e34d8177
a2672198
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
287 addition
and
6 deletion
+287
-6
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Patterns.cs
...Portable/Lowering/LocalRewriter/LocalRewriter_Patterns.cs
+27
-6
src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenOperators.cs
src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenOperators.cs
+260
-0
未找到文件。
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Patterns.cs
浏览文件 @
cb9ee8ce
...
...
@@ -133,14 +133,35 @@ private bool IsIrrefutablePatternTest(BoundExpression test)
}
private
BoundExpression
CompareWithConstant
(
BoundExpression
input
,
BoundExpression
boundConstant
)
{
var
systemObject
=
_factory
.
SpecialType
(
SpecialType
.
System_Object
);
if
(
boundConstant
.
ConstantValue
==
ConstantValue
.
Null
)
{
if
(
input
.
Type
.
IsNonNullableValueType
())
{
var
systemBoolean
=
_factory
.
SpecialType
(
SpecialType
.
System_Boolean
);
return
RewriteNullableNullEquality
(
syntax
:
_factory
.
Syntax
,
kind
:
BinaryOperatorKind
.
NullableNullEqual
,
loweredLeft
:
input
,
loweredRight
:
boundConstant
,
returnType
:
systemBoolean
);
}
else
{
return
_factory
.
ObjectEqual
(
_factory
.
Convert
(
systemObject
,
input
),
boundConstant
);
}
}
else
{
return
_factory
.
StaticCall
(
_factory
.
SpecialType
(
SpecialType
.
System_Object
)
,
systemObject
,
"Equals"
,
_factory
.
Convert
(
_factory
.
SpecialType
(
SpecialType
.
System_Object
)
,
boundConstant
),
_factory
.
Convert
(
_factory
.
SpecialType
(
SpecialType
.
System_Object
)
,
input
)
_factory
.
Convert
(
systemObject
,
boundConstant
),
_factory
.
Convert
(
systemObject
,
input
)
);
}
}
private
bool
?
MatchConstantValue
(
BoundExpression
source
,
TypeSymbol
targetType
,
bool
requiredNullTest
)
{
...
...
src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenOperators.cs
浏览文件 @
cb9ee8ce
...
...
@@ -13,6 +13,266 @@ namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
{
public
class
CodeGenOperatorTests
:
CSharpTestBase
{
[
Fact
]
public
void
TestIsNullPattern
()
{
var
source
=
@"
using System;
class C
{
public static void Main()
{
var c = new C();
Console.Write(c is null);
}
}
"
;
// Release
var
compilation
=
CompileAndVerify
(
source
,
expectedOutput
:
"False"
,
options
:
TestOptions
.
ReleaseExe
);
compilation
.
VerifyIL
(
"C.Main"
,
@"{
// Code size 14 (0xe)
.maxstack 2
IL_0000: newobj ""C..ctor()""
IL_0005: ldnull
IL_0006: ceq
IL_0008: call ""void System.Console.Write(bool)""
IL_000d: ret
}"
);
// Debug
compilation
=
CompileAndVerify
(
source
,
expectedOutput
:
"False"
,
options
:
TestOptions
.
DebugExe
);
compilation
.
VerifyIL
(
"C.Main"
,
@"{
// Code size 18 (0x12)
.maxstack 2
.locals init (C V_0) //c
IL_0000: nop
IL_0001: newobj ""C..ctor()""
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldnull
IL_0009: ceq
IL_000b: call ""void System.Console.Write(bool)""
IL_0010: nop
IL_0011: ret
}"
);
}
[
Fact
]
public
void
TestIsNullPatternGenericParam
()
{
var
source
=
@"
using System;
class C
{
public static void M<T>(T o)
{
Console.Write(o is null);
if (o is null)
{
Console.Write(""Branch taken"");
}
}
public static void Main()
{
M(new object());
}
}
"
;
CreateStandardCompilation
(
source
).
VerifyDiagnostics
(
// (7,28): error CS0403: Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)' instead.
// Console.Write(o is null);
Diagnostic
(
ErrorCode
.
ERR_TypeVarCantBeNull
,
"null"
).
WithArguments
(
"T"
).
WithLocation
(
7
,
28
),
// (8,18): error CS0403: Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)' instead.
// if (o is null)
Diagnostic
(
ErrorCode
.
ERR_TypeVarCantBeNull
,
"null"
).
WithArguments
(
"T"
).
WithLocation
(
8
,
18
));
}
[
Fact
]
public
void
TestIsNullPatternGenericParamClass
()
{
var
source
=
@"
using System;
class C
{
public static void M<T>(T o) where T: class
{
Console.Write(o is null);
if (o is null) {
Console.Write("" Branch taken"");
}
}
public static void Main()
{
M(new object());
}
}
"
;
// Release
var
compilation
=
CompileAndVerify
(
source
,
expectedOutput
:
"False"
,
options
:
TestOptions
.
ReleaseExe
);
compilation
.
VerifyIL
(
"C.M<T>(T)"
,
@"{
// Code size 33 (0x21)
.maxstack 2
IL_0000: ldarg.0
IL_0001: box ""T""
IL_0006: ldnull
IL_0007: ceq
IL_0009: call ""void System.Console.Write(bool)""
IL_000e: ldarg.0
IL_000f: box ""T""
IL_0014: brtrue.s IL_0020
IL_0016: ldstr "" Branch taken""
IL_001b: call ""void System.Console.Write(string)""
IL_0020: ret
}"
);
// Debug
compilation
=
CompileAndVerify
(
source
,
expectedOutput
:
"False"
,
options
:
TestOptions
.
DebugExe
);
compilation
.
VerifyIL
(
"C.M<T>(T)"
,
@"{
// Code size 43 (0x2b)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: box ""T""
IL_0007: ldnull
IL_0008: ceq
IL_000a: call ""void System.Console.Write(bool)""
IL_000f: nop
IL_0010: ldarg.0
IL_0011: box ""T""
IL_0016: ldnull
IL_0017: ceq
IL_0019: stloc.0
IL_001a: ldloc.0
IL_001b: brfalse.s IL_002a
IL_001d: nop
IL_001e: ldstr "" Branch taken""
IL_0023: call ""void System.Console.Write(string)""
IL_0028: nop
IL_0029: nop
IL_002a: ret
}"
);
}
[
Fact
]
public
void
TestIsNullPatternObjectLiteral
()
{
var
source
=
@"
using System;
class C
{
public static void Main()
{
Console.Write(((object)null) is null);
if (((object) null) is null) {
Console.Write("" Branch taken"");
}
}
}
"
;
// Release
var
compilation
=
CompileAndVerify
(
source
,
expectedOutput
:
"True Branch taken"
,
options
:
TestOptions
.
ReleaseExe
);
compilation
.
VerifyIL
(
"C.Main"
,
@"{
// Code size 20 (0x14)
.maxstack 2
IL_0000: ldnull
IL_0001: ldnull
IL_0002: ceq
IL_0004: call ""void System.Console.Write(bool)""
IL_0009: ldstr "" Branch taken""
IL_000e: call ""void System.Console.Write(string)""
IL_0013: ret
}"
);
// Debug
compilation
=
CompileAndVerify
(
source
,
expectedOutput
:
"True Branch taken"
,
options
:
TestOptions
.
DebugExe
);
compilation
.
VerifyIL
(
"C.Main"
,
@"{
// Code size 33 (0x21)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldnull
IL_0002: ldnull
IL_0003: ceq
IL_0005: call ""void System.Console.Write(bool)""
IL_000a: nop
IL_000b: ldnull
IL_000c: ldnull
IL_000d: ceq
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: brfalse.s IL_0020
IL_0013: nop
IL_0014: ldstr "" Branch taken""
IL_0019: call ""void System.Console.Write(string)""
IL_001e: nop
IL_001f: nop
IL_0020: ret
}"
);
}
[
Fact
]
public
void
TestIsNullPatternStringConstant
()
{
var
source
=
@"
using System;
class C
{
const String nullString = null;
public static void Main()
{
Console.Write(((string)null) is nullString);
if (((string) null) is nullString) {
Console.Write("" Branch taken"");
}
}
}
"
;
// Release
var
compilation
=
CompileAndVerify
(
source
,
expectedOutput
:
"True Branch taken"
,
options
:
TestOptions
.
ReleaseExe
);
compilation
.
VerifyIL
(
"C.Main"
,
@"{
// Code size 20 (0x14)
.maxstack 2
IL_0000: ldnull
IL_0001: ldnull
IL_0002: ceq
IL_0004: call ""void System.Console.Write(bool)""
IL_0009: ldstr "" Branch taken""
IL_000e: call ""void System.Console.Write(string)""
IL_0013: ret
}"
);
// Debug
compilation
=
CompileAndVerify
(
source
,
expectedOutput
:
"True Branch taken"
,
options
:
TestOptions
.
DebugExe
);
compilation
.
VerifyIL
(
"C.Main"
,
@"{
// Code size 33 (0x21)
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldnull
IL_0002: ldnull
IL_0003: ceq
IL_0005: call ""void System.Console.Write(bool)""
IL_000a: nop
IL_000b: ldnull
IL_000c: ldnull
IL_000d: ceq
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: brfalse.s IL_0020
IL_0013: nop
IL_0014: ldstr "" Branch taken""
IL_0019: call ""void System.Console.Write(string)""
IL_001e: nop
IL_001f: nop
IL_0020: ret
}"
);
}
[
Fact
]
public
void
TestDelegateAndStringOperators
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录