提交 3ac90b63 编写于 作者: J Jared Parsons

Self assignment in expression trees should error

All assignments in an expression tree should be an error.  Self
assignment was being flagged as only a warning which lead to later
errors in code generation.  Changed the behavior to warn and error for
self assignment (matches native compiler behavior).

close #3826
上级 154ab390
......@@ -123,7 +123,8 @@ private static Symbol GetLocalOrParameterSymbol(BoundExpression expr)
public override BoundNode VisitAssignmentOperator(BoundAssignmentOperator node)
{
if (!CheckForAssignmentToSelf(node) && _inExpressionLambda && node.Left.Kind != BoundKind.ObjectInitializerMember && node.Left.Kind != BoundKind.DynamicObjectInitializerMember)
CheckForAssignmentToSelf(node);
if (_inExpressionLambda && node.Left.Kind != BoundKind.ObjectInitializerMember && node.Left.Kind != BoundKind.DynamicObjectInitializerMember)
{
Error(ErrorCode.ERR_ExpressionTreeContainsAssignment, node);
}
......
......@@ -1295,5 +1295,30 @@ static void stuff()
Assert.Equal("Program a", symbolInfo.Symbol.ToTestDisplayString());
}
[Fact]
[WorkItem(3826, "https://github.com/dotnet/roslyn/issues/3826")]
public void ExpressionTreeSelfAssignmentShouldError()
{
var source = @"
using System;
using System.Linq.Expressions;
class Program
{
static void Main()
{
Expression<Func<int, int>> x = y => y = y;
}
}";
var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
compilation.VerifyDiagnostics(
// (9,45): warning CS1717: Assignment made to same variable; did you mean to assign something else?
// Expression<Func<int, int>> x = y => y = y;
Diagnostic(ErrorCode.WRN_AssignmentToSelf, "y = y").WithLocation(9, 45),
// (9,45): error CS0832: An expression tree may not contain an assignment operator
// Expression<Func<int, int>> x = y => y = y;
Diagnostic(ErrorCode.ERR_ExpressionTreeContainsAssignment, "y = y").WithLocation(9, 45));
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册