From e0404c066ccfdf539ab9a20f50e5eeb982798cc6 Mon Sep 17 00:00:00 2001 From: Neal Gafter Date: Wed, 4 Dec 2019 14:09:40 -0800 Subject: [PATCH] Convert operand expressions in an error case (#40093) Fixes #39975 --- .../Portable/Binder/Binder_Operators.cs | 4 ++++ .../Test/Semantic/Semantics/OperatorTests.cs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs index 7319f4c701b..aafdce68377 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs @@ -78,6 +78,8 @@ private BoundExpression BindCompoundAssignment(AssignmentExpressionSyntax node, Error(diagnostics, ErrorCode.ERR_BadBinaryOps, node, node.OperatorToken.Text, left.Display, right.Display); // error: operator can't be applied on dynamic and a type that is not convertible to dynamic: + left = BindToTypeForErrorRecovery(left); + right = BindToTypeForErrorRecovery(right); return new BoundCompoundAssignmentOperator(node, BinaryOperatorSignature.Error, left, right, Conversion.NoConversion, Conversion.NoConversion, LookupResultKind.Empty, CreateErrorType(), hasErrors: true); } @@ -90,6 +92,8 @@ private BoundExpression BindCompoundAssignment(AssignmentExpressionSyntax node, // be used here. // NOTE: no overload resolution candidates. + left = BindToTypeForErrorRecovery(left); + right = BindToTypeForErrorRecovery(right); return new BoundCompoundAssignmentOperator(node, BinaryOperatorSignature.Error, left, right, Conversion.NoConversion, Conversion.NoConversion, LookupResultKind.NotAVariable, CreateErrorType(), hasErrors: true); } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/OperatorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/OperatorTests.cs index 5551c717805..49e88344e3b 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/OperatorTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/OperatorTests.cs @@ -10904,5 +10904,24 @@ static void Main() Diagnostic(ErrorCode.ERR_ConstantStringTooLong, "C1").WithLocation(28, 68) ); } + + [Fact, WorkItem(39975, "https://github.com/dotnet/roslyn/issues/39975")] + public void EnsureOperandsConvertedInErrorExpression_01() + { + string source = +@"class C +{ + static unsafe void M(dynamic d, int* p) + { + d += p; + } +} +"; + CreateCompilation(source, options: TestOptions.ReleaseDll.WithAllowUnsafe(true)).VerifyDiagnostics( + // (5,9): error CS0019: Operator '+=' cannot be applied to operands of type 'dynamic' and 'int*' + // d += p; + Diagnostic(ErrorCode.ERR_BadBinaryOps, "d += p").WithArguments("+=", "dynamic", "int*").WithLocation(5, 9) + ); + } } } -- GitLab