提交 48da22d2 编写于 作者: N Neal Gafter 提交者: Julien Couvreur

Permit a throw expression as the body of an expression-bodied lambda (#15636)

Fixes #15555
Checking in on behalf of @gafter 
上级 8e19b537
......@@ -689,11 +689,10 @@ private static bool IsThrowExpressionInProperContext(ThrowExpressionSyntax node)
var binaryParent = (BinaryExpressionSyntax)parent;
return node == binaryParent.Right;
}
case SyntaxKind.ArrowExpressionClause: // =>
{
var arrowClauseParent = (ArrowExpressionClauseSyntax)parent;
return node == arrowClauseParent.Expression;
}
case SyntaxKind.ArrowExpressionClause:
case SyntaxKind.ParenthesizedLambdaExpression:
case SyntaxKind.SimpleLambdaExpression:
return true;
// We do not support && and || because
// 1. The precedence would not syntactically allow it
// 2. It isn't clear what the semantics should be
......
......@@ -2355,5 +2355,55 @@ class B
var comp = CreateCompilationWithMscorlibAndSystemCore(src, options: TestOptions.DebugExe);
CompileAndVerify(comp, expectedOutput: "1");
}
[Fact]
public void ThrowExpression_Lambda()
{
var src = @"using System;
class C
{
public static void Main()
{
Action a = () => throw new Exception(""1"");
try
{
a();
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
Func<int, int> b = x => throw new Exception(""2"");
try
{
b(0);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
b = (int x) => throw new Exception(""3"");
try
{
b(0);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
b = (x) => throw new Exception(""4"");
try
{
b(0);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
}";
var comp = CreateCompilationWithMscorlibAndSystemCore(src, options: TestOptions.DebugExe);
CompileAndVerify(comp, expectedOutput: "1234");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册