@@ -207,6 +207,78 @@ private static bool IsNullLiteral(Expr expr)
207207 return expr is Expr . Literal { Value : null } ;
208208 }
209209
210+ /// <summary>
211+ /// Determines whether the specified expression is the integer literal 0, possibly in parentheses.
212+ /// </summary>
213+ /// <param name="expr">The expression to test.</param>
214+ /// <returns>
215+ /// <see langword="true" /> if the expression is the integer literal 0; otherwise, <see langword="false" />.
216+ /// </returns>
217+ private static bool IsZeroLiteral ( Expr expr )
218+ {
219+ while ( expr is Expr . Parenthesized parenthesized )
220+ expr = parenthesized . Expression ;
221+
222+ return expr is Expr . Literal { Value : 0 or 0u or 0L or 0UL } ;
223+ }
224+
225+ /// <summary>
226+ /// Converts the integer literal 0 opposite an operand of an enum type to that enum type
227+ /// if the operator is defined for two operands of the enum type.
228+ /// </summary>
229+ /// <param name="expr">The binary expression.</param>
230+ /// <param name="lhs">The expression of the left operand.</param>
231+ /// <param name="rhs">The expression of the right operand.</param>
232+ private static void ApplyEnumZeroConversion ( Expr . Binary expr , ref Expression lhs , ref Expression rhs )
233+ {
234+ //
235+ // 10.2.4 Implicit enumeration conversions
236+ // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#1024-implicit-enumeration-conversions
237+ //
238+ // An implicit enumeration conversion permits a constant expression with any integer type and the value zero
239+ // to be converted to any enum type and to any nullable value type whose underlying type is an enum type.
240+ //
241+ // Of such constant expressions, only the integer literal 0 is recognized.
242+ //
243+ switch ( expr . Operator . Name )
244+ {
245+ case "-" :
246+ case "&" :
247+ case "|" :
248+ case "^" :
249+ case "==" :
250+ case "!=" :
251+ case "<" :
252+ case ">" :
253+ case "<=" :
254+ case ">=" :
255+ break ;
256+
257+ default :
258+ return ;
259+ }
260+
261+ var lhsType = lhs . Type . GetNonNullableType ( ) ;
262+ var rhsType = rhs . Type . GetNonNullableType ( ) ;
263+
264+ if ( lhsType . IsEnum && IsZeroLiteral ( expr . Right ) )
265+ {
266+ //
267+ // Both E - E and E - U accept 'x - 0'. As in the C# compiler, E - U is selected and returns E
268+ // if the literal is of the underlying type U; otherwise, E - E is selected and returns U, as in 'x - 0L'.
269+ //
270+ if ( expr . Operator . Name != "-" || rhsType != lhsType . GetEnumUnderlyingType ( ) )
271+ rhs = Expression . Convert ( rhs , lhsType ) ;
272+ }
273+ else if ( rhsType . IsEnum && IsZeroLiteral ( expr . Left ) )
274+ {
275+ //
276+ // As in the C# compiler, E - E is selected for '0 - x' over U - E, so the result is of the underlying type.
277+ //
278+ lhs = Expression . Convert ( lhs , rhsType ) ;
279+ }
280+ }
281+
210282 /// <summary>
211283 /// Applies an implicit conversion of a branch of a conditional expression to the type of the other branch.
212284 /// </summary>
@@ -365,22 +437,65 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,
365437 ? rhsNonNullableType
366438 : null ;
367439
440+ //
441+ // The type to which the result of an enum operator is converted. It remains null for an enum comparison,
442+ // whose result is bool, and if neither operand is of an enum type.
443+ //
444+ Type ? enumResultType = null ;
445+
368446 if ( enumType is not null )
369447 {
448+ //
449+ // 12.10.5 Addition operator, 12.10.6 Subtraction operator, 12.12.6 Enumeration comparison operators,
450+ // and 12.13.3 Enumeration logical operators
451+ // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#12105-addition-operator
452+ //
453+ // Every enum type E with the underlying type U implicitly provides the following operators:
454+ //
455+ // E operator +(E x, U y);
456+ // E operator +(U x, E y);
457+ // U operator -(E x, E y);
458+ // E operator -(E x, U y);
459+ // E operator &(E x, E y); and likewise |, ^
460+ // bool operator ==(E x, E y); and likewise !=, <, >, <=, >=
461+ //
462+ // Each operator is evaluated on the values converted to U, as in (E)((U)x + (U)y).
463+ // The C# compiler also accepts U - E and returns E, so it is supported as well.
464+ //
370465 var enumUnderlyingType = enumType . GetEnumUnderlyingType ( ) ;
466+ var otherType = lhsNonNullableType . IsEnum ? rhsNonNullableType : lhsNonNullableType ;
467+
468+ var isEnumPair = lhsNonNullableType == rhsNonNullableType ;
469+ var isUnderlyingPair = otherType == enumUnderlyingType
470+ || TypeUtils . CanConvertPrimitive ( otherType , enumUnderlyingType ) ;
471+
472+ switch ( op . Name )
473+ {
474+ case "+" when isUnderlyingPair :
475+ case "-" when isUnderlyingPair :
476+ case "&" or "|" or "^" when isEnumPair :
477+ enumResultType = enumType ;
478+ break ;
371479
372- if ( lhsNonNullableType != rhsNonNullableType )
373- if ( lhsNonNullableType . IsEnum ? enumUnderlyingType != rhsNonNullableType : enumUnderlyingType != lhsNonNullableType )
480+ case "-" when isEnumPair :
481+ enumResultType = enumUnderlyingType ;
482+ break ;
483+
484+ case "==" or "!=" or "<" or ">" or "<=" or ">=" when isEnumPair :
485+ break ;
486+
487+ default :
374488 Error . NonApplicableBinaryOperator ( op , lhsType , rhsType ) ;
489+ break ;
490+ }
375491
376492 if ( lhsNonNullableType . IsEnum )
377493 lhs = ConvertOperand ( lhs , enumUnderlyingType , lifted ) ;
378494
379495 if ( rhsNonNullableType . IsEnum )
380496 rhs = ConvertOperand ( rhs , enumUnderlyingType , lifted ) ;
381497 }
382-
383- if ( enumType is null && op . Name is ">>" or ">>>" or "<<" )
498+ else if ( op . Name is ">>" or ">>>" or "<<" )
384499 {
385500 //
386501 // The predefined shift operators take an int, uint, long, or ulong value and an int count,
@@ -428,24 +543,9 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,
428543
429544 var result = apply ( lhs , rhs ) ;
430545
431- if ( enumType is not null )
432- {
433- switch ( op . Name )
434- {
435- case "==" :
436- case "!=" :
437- case ">" :
438- case "<" :
439- case ">=" :
440- case "<=" :
441- return result ;
442-
443- default :
444- return ConvertOperand ( result , enumType , lifted ) ;
445- }
446- }
447-
448- return result ;
546+ return enumResultType is not null
547+ ? ConvertOperand ( result , enumResultType , lifted )
548+ : result ;
449549 }
450550
451551 /// <summary>
0 commit comments