diff --git a/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_Warnings.cs b/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_Warnings.cs index a4a438bf8d4bec1ad9305f590175b9414e161dee..5d697aef15bf70e157036f3080da11f0b584b272 100644 --- a/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_Warnings.cs +++ b/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_Warnings.cs @@ -238,7 +238,16 @@ private void CheckBinaryOperator(BoundBinaryOperator node) private void CheckCompoundAssignmentOperator(BoundCompoundAssignmentOperator node) { - CheckForBitwiseOrSignExtend(node, node.Operator.Kind, node.Left, node.Right); + BoundExpression left = node.Left; + + if (!node.Operator.Kind.IsDynamic() && !node.LeftConversion.IsIdentity && node.LeftConversion.Exists) + { + // Need to represent the implicit conversion as a node in order to be able to produce correct diagnostics. + left = new BoundConversion(left.Syntax, left, node.LeftConversion, node.Operator.Kind.IsChecked(), + explicitCastInCode: false, constantValueOpt: null, type: node.Operator.LeftType); + } + + CheckForBitwiseOrSignExtend(node, node.Operator.Kind, left, node.Right); CheckLiftedCompoundAssignment(node); if (_inExpressionLambda) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/OperatorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/OperatorTests.cs index 1af7d96bc178e1f42cedc2d1c49b558773cab407..27d6b222b643edf8ae2c639444bf0cbac149e6cb 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/OperatorTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/OperatorTests.cs @@ -8728,5 +8728,26 @@ operator int (IntHolder ih) operator IntHolder(int i) 'y' is 5"); } + + [Fact, WorkItem(4027, "https://github.com/dotnet/roslyn/issues/4027")] + public void NotSignExtendedOperand() + { + string source = @" +class MainClass +{ + public static void Main () + { + short a = 0; + int b = 0; + a |= (short)b; + a = (short)(a | (short)b); + } +} +"; + + var compilation = CreateCompilationWithMscorlib(source, options: TestOptions.DebugDll); + + compilation.VerifyDiagnostics(); + } } }