@@ -280,6 +280,32 @@ private static Expr SkipParentheses(Expr expr)
280280 return expr ;
281281 }
282282
283+ /// <summary>
284+ /// Converts the null literal opposite an operand of a nullable value type to that type.
285+ /// For the <c>+</c> operator with an operand of a nullable enum type, the literal is converted
286+ /// to the nullable form of the underlying type of the enum.
287+ /// </summary>
288+ /// <param name="expr">The binary expression.</param>
289+ /// <param name="lhs">The expression of the left operand.</param>
290+ /// <param name="rhs">The expression of the right operand.</param>
291+ private static void ApplyNullLiteralConversion ( Expr . Binary expr , ref Expression lhs , ref Expression rhs )
292+ {
293+ //
294+ // The null literal converts to a nullable value type, so opposite an operand of such a type
295+ // it takes that type, as in 'x == null' or 'x ?? null'. For an enum type E, only E + U and U + E
296+ // are defined, so in 'x + null' it takes the nullable form of the underlying type U instead.
297+ //
298+ if ( lhs . Type . IsNullable ( ) && IsNullLiteral ( expr . Right ) )
299+ rhs = Expression . Constant ( null , GetNullLiteralType ( expr . Operator , lhs . Type ) ) ;
300+ else if ( rhs . Type . IsNullable ( ) && IsNullLiteral ( expr . Left ) )
301+ lhs = Expression . Constant ( null , GetNullLiteralType ( expr . Operator , rhs . Type ) ) ;
302+
303+ static Type GetNullLiteralType ( Identifier op , Type operandType ) =>
304+ op . Name == "+" && operandType . GetNonNullableType ( ) is { IsEnum : true } enumType
305+ ? enumType . GetEnumUnderlyingType ( ) . MakeNullableType ( )
306+ : operandType ;
307+ }
308+
283309 /// <summary>
284310 /// Converts the integer literal 0 opposite an operand of an enum type to that enum type
285311 /// if the operator is defined for two operands of the enum type. As in the C# compiler, a literal
0 commit comments