提交 9b8b7ec9 编写于 作者: T Ty Overby

Merge pull request #5771 from TyOverby/4593-null-coalesce

Allow converted nulls in expr-tree coalesce
......@@ -546,7 +546,7 @@ public override BoundNode VisitNameOfOperator(BoundNameOfOperator node)
public override BoundNode VisitNullCoalescingOperator(BoundNullCoalescingOperator node)
{
if (_inExpressionLambda && node.LeftOperand.ConstantValue != null && node.LeftOperand.ConstantValue.IsNull)
if (_inExpressionLambda && node.LeftOperand.IsLiteralNull())
{
Error(ErrorCode.ERR_ExpressionTreeContainsBadCoalesce, node.LeftOperand);
}
......
......@@ -3132,7 +3132,85 @@ static void Main()
new[] { ExpressionAssemblyRef }, expectedOutput: TrimExpectedOutput(expectedOutput));
}
[WorkItem(4593, "https://github.com/dotnet/roslyn/issues/4593")]
[Fact]
public void ExprTreeConvertedNullOnLHS()
{
var text =
@"using System;
using System.Linq.Expressions;
class Program
{
Expression<Func<object>> testExpr = () => null ?? ""hello"";
}";
CreateCompilationWithMscorlibAndSystemCore(text).VerifyDiagnostics(
// (6,47): error CS0845: An expression tree lambda may not contain a coalescing operator with a null literal left-hand side
// Expression<Func<object>> testExpr = () => null ?? new object();
Diagnostic(ErrorCode.ERR_ExpressionTreeContainsBadCoalesce, "null").WithLocation(6, 47) );
}
[WorkItem(4593, "https://github.com/dotnet/roslyn/issues/4593")]
[Fact]
public void ExprTreeNullableInt()
{
var text =
@"using System;
using System.Linq.Expressions;
class Program
{
static void Main()
{
Expression<Func<int?>> testExpr = () => (int?)null ?? (int?)5;
ExpressionVisitor ev = new ExpressionVisitor();
ev.Visit(testExpr);
Console.Write(ev.toStr);
}
}";
var expectedOutput = @"
Lambda:
Type->System.Func`1[System.Nullable`1[System.Int32]]
Parameters->
Body->
Coalesce:
Type->System.Nullable`1[System.Int32]
Method->
IsLifted->False
IsLiftedToNull->False
Left->
Convert:
Type->System.Nullable`1[System.Int32]
Method->
IsLifted->True
IsLiftedToNull->True
Operand->
Constant:
Type->System.Object
Value->
Right->
Convert:
Type->System.Nullable`1[System.Int32]
Method->
IsLifted->True
IsLiftedToNull->True
Operand->
Constant:
Type->System.Int32
Value->5
Conversion->
";
//CreateCompilationWithMscorlibAndSystemCore(text).VerifyDiagnostics();
CompileAndVerify(
new[] { text, TreeWalkerLib },
new[] { ExpressionAssemblyRef }, expectedOutput: TrimExpectedOutput(expectedOutput));
}
[WorkItem(544442, "DevDiv")]
[WorkItem(4593, "https://github.com/dotnet/roslyn/issues/4593")]
[Fact]
public void ExprTreeFieldInitCoalesceWithNullOnLHS()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册