@@ -45,6 +45,20 @@ private object ResolveSymbol(Expr.Reference expr)
4545 if ( TypeUtils . CanConvertPrimitive ( expression . Type , conversionType ) )
4646 return Expression . Convert ( expression , conversionType ) ;
4747
48+ //
49+ // 10.2.6 Implicit nullable conversions
50+ // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#1026-implicit-nullable-conversions
51+ //
52+ // For each predefined implicit identity or numeric conversion from a non-nullable value type S
53+ // to a non-nullable value type T, implicit conversions exist from S? to T? and from S to T?.
54+ //
55+ if ( Nullable . GetUnderlyingType ( conversionType ) is { } underlyingType )
56+ {
57+ var sourceType = expression . Type . GetNonNullableType ( ) ;
58+ if ( sourceType == underlyingType || TypeUtils . CanConvertPrimitive ( sourceType , underlyingType ) )
59+ return Expression . Convert ( expression , conversionType ) ;
60+ }
61+
4862 var conversion = TypeUtils . FindConversionOperator ( expression . Type , conversionType ) ;
4963 if ( conversion is not null )
5064 return Expression . Convert ( expression , conversionType , conversion ) ;
@@ -111,16 +125,86 @@ private static Type ApplyConditionalConversions(Expr.Conditional expr, ref Expre
111125 Error . NonApplicableConditionalOperator ( ifTrue . Type , ifFalse . Type ) ;
112126 return null ! ;
113127
114- static bool IsNullLiteral ( Expr expr )
128+ static bool CanBeNull ( Type type ) =>
129+ ! type . IsValueType || type . IsNullable ( ) ;
130+ }
131+
132+ /// <summary>
133+ /// Creates a null-coalescing expression whose operands are converted to its result type.
134+ /// </summary>
135+ /// <param name="op">The <see cref="Identifier"/> representing the operator.</param>
136+ /// <param name="lhs">The expression of the left operand.</param>
137+ /// <param name="rhs">The expression of the right operand.</param>
138+ /// <returns>
139+ /// The null-coalescing expression.
140+ /// </returns>
141+ private static BinaryExpression ApplyNullCoalescing ( Identifier op , Expression lhs , Expression rhs )
142+ {
143+ //
144+ // 12.15 The null coalescing operator
145+ // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#1215-the-null-coalescing-operator
146+ //
147+ // In order of preference, the type of a ?? b is A₀, A, or B, where A is the type of a, B is the type of b,
148+ // and A₀ is the underlying type of A if A is a nullable value type, or A otherwise:
149+ //
150+ // * If A is a non-nullable value type, a compile-time error occurs.
151+ // * Otherwise, if A is a nullable value type and an implicit conversion exists from b to A₀,
152+ // the result type is A₀.
153+ // * Otherwise, if an implicit conversion exists from b to A, the result type is A.
154+ // * Otherwise, if A is a nullable value type and an implicit conversion exists from A₀ to B,
155+ // the result type is B.
156+ //
157+ var lhsType = lhs . Type ;
158+ var rhsType = rhs . Type ;
159+
160+ if ( lhsType . IsValueType && ! lhsType . IsNullable ( ) )
161+ Error . NonApplicableBinaryOperator ( op , lhsType , rhsType ) ;
162+
163+ var underlyingType = Nullable . GetUnderlyingType ( lhsType ) ;
164+ if ( underlyingType is not null )
115165 {
116- while ( expr is Expr . Parenthesized parenthesized )
117- expr = parenthesized . Expression ;
166+ var value = ApplyImplicitConversion ( rhs , underlyingType ) ;
167+ if ( value is not null )
168+ return Expression . Coalesce ( lhs , value ) ;
169+ }
118170
119- return expr is Expr . Literal { Value : null } ;
171+ var converted = ApplyImplicitConversion ( rhs , lhsType ) ;
172+ if ( converted is not null )
173+ return Expression . Coalesce ( lhs , converted ) ;
174+
175+ if ( underlyingType is not null )
176+ {
177+ //
178+ // A non-null value of a is unwrapped and converted to B. Convert a to the nullable form of B instead,
179+ // which keeps its null value. For a reference type B, the conversion is a boxing conversion.
180+ //
181+ converted = rhsType . IsValueType
182+ ? ApplyImplicitConversion ( lhs , rhsType . IsNullable ( ) ? rhsType : rhsType . MakeNullableType ( ) )
183+ : rhsType . IsAssignableFrom ( underlyingType )
184+ ? Expression . Convert ( lhs , rhsType )
185+ : null ;
186+
187+ if ( converted is not null )
188+ return Expression . Coalesce ( converted , rhs ) ;
120189 }
121190
122- static bool CanBeNull ( Type type ) =>
123- ! type . IsValueType || type . IsNullable ( ) ;
191+ Error . NonApplicableBinaryOperator ( op , lhsType , rhsType ) ;
192+ return null ! ;
193+ }
194+
195+ /// <summary>
196+ /// Determines whether the specified expression is the null literal, possibly in parentheses.
197+ /// </summary>
198+ /// <param name="expr">The expression to test.</param>
199+ /// <returns>
200+ /// <see langword="true" /> if the expression is the null literal; otherwise, <see langword="false" />.
201+ /// </returns>
202+ private static bool IsNullLiteral ( Expr expr )
203+ {
204+ while ( expr is Expr . Parenthesized parenthesized )
205+ expr = parenthesized . Expression ;
206+
207+ return expr is Expr . Literal { Value : null } ;
124208 }
125209
126210 /// <summary>
@@ -260,29 +344,40 @@ static Expression Convert(Expression argument, Type conversionType)
260344
261345 private static Expression ApplyBinaryExpression ( Identifier op , Func < Expression , Expression , Expression > apply , Expression lhs , Expression rhs )
262346 {
263- var enumUnderlyingType = lhs . Type . IsEnum
264- ? lhs . Type . GetEnumUnderlyingType ( )
265- : rhs . Type . IsEnum
266- ? rhs . Type . GetEnumUnderlyingType ( )
267- : null ;
347+ //
348+ // 12.4.8 Lifted operators
349+ // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#1248-lifted-operators
350+ //
351+ // The operators on non-nullable value types have lifted forms, which take and return the nullable forms
352+ // of the types. If either operand is of a nullable value type, the operator is selected by the underlying
353+ // types of the operands, and both operands are converted to the nullable forms of the operator's operand types.
354+ //
355+ var lhsType = lhs . Type ;
356+ var rhsType = rhs . Type ;
268357
269- var enumType = lhs . Type . IsEnum
270- ? lhs . Type
271- : rhs . Type . IsEnum
272- ? rhs . Type
358+ var lifted = lhsType . IsNullable ( ) || rhsType . IsNullable ( ) ;
359+ var lhsNonNullableType = lhsType . GetNonNullableType ( ) ;
360+ var rhsNonNullableType = rhsType . GetNonNullableType ( ) ;
361+
362+ var enumType = lhsNonNullableType . IsEnum
363+ ? lhsNonNullableType
364+ : rhsNonNullableType . IsEnum
365+ ? rhsNonNullableType
273366 : null ;
274367
275- if ( enumUnderlyingType is not null )
368+ if ( enumType is not null )
276369 {
277- if ( lhs . Type != rhs . Type )
278- if ( lhs . Type . IsEnum ? enumUnderlyingType != rhs . Type : enumUnderlyingType != lhs . Type )
279- Error . NonApplicableBinaryOperator ( op , lhs . Type , rhs . Type ) ;
370+ var enumUnderlyingType = enumType . GetEnumUnderlyingType ( ) ;
371+
372+ if ( lhsNonNullableType != rhsNonNullableType )
373+ if ( lhsNonNullableType . IsEnum ? enumUnderlyingType != rhsNonNullableType : enumUnderlyingType != lhsNonNullableType )
374+ Error . NonApplicableBinaryOperator ( op , lhsType , rhsType ) ;
280375
281- if ( lhs . Type . IsEnum )
282- lhs = Expression . Convert ( lhs , enumUnderlyingType ) ;
376+ if ( lhsNonNullableType . IsEnum )
377+ lhs = ConvertOperand ( lhs , enumUnderlyingType , lifted ) ;
283378
284- if ( rhs . Type . IsEnum )
285- rhs = Expression . Convert ( rhs , enumUnderlyingType ) ;
379+ if ( rhsNonNullableType . IsEnum )
380+ rhs = ConvertOperand ( rhs , enumUnderlyingType , lifted ) ;
286381 }
287382
288383 if ( enumType is null && op . Name is ">>" or ">>>" or "<<" )
@@ -291,32 +386,46 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,
291386 // The predefined shift operators take an int, uint, long, or ulong value and an int count,
292387 // so an operand of a smaller integral type, including char, is converted to int, as in C#.
293388 //
294- var lhsType = lhs . Type ;
295- var rhsType = rhs . Type ;
296-
297389 lhs = ApplyUnaryNumericPromotion ( lhs ) ;
298390 rhs = ApplyUnaryNumericPromotion ( rhs ) ;
299391
300- if ( ! TypeUtils . IsInteger ( lhs . Type ) || rhs . Type != typeof ( int ) )
392+ var shiftType = lhs . Type . GetNonNullableType ( ) ;
393+ if ( ! TypeUtils . IsInteger ( shiftType ) || rhs . Type . GetNonNullableType ( ) != typeof ( int ) )
301394 Error . NonApplicableBinaryOperator ( op , lhsType , rhsType ) ;
302395
303396 if ( op . Name == ">>>" )
304397 {
305- if ( lhs . Type == typeof ( int ) )
306- return Expression . Convert (
307- apply ( Expression . Convert ( lhs , typeof ( uint ) ) , rhs ) ,
308- typeof ( int ) ) ;
309-
310- if ( lhs . Type == typeof ( long ) )
311- return Expression . Convert (
312- apply ( Expression . Convert ( lhs , typeof ( ulong ) ) , rhs ) ,
313- typeof ( long ) ) ;
398+ if ( shiftType == typeof ( int ) )
399+ return ConvertOperand (
400+ apply ( ConvertOperand ( lhs , typeof ( uint ) , lifted ) , rhs ) ,
401+ typeof ( int ) ,
402+ lifted ) ;
403+
404+ if ( shiftType == typeof ( long ) )
405+ return ConvertOperand (
406+ apply ( ConvertOperand ( lhs , typeof ( ulong ) , lifted ) , rhs ) ,
407+ typeof ( long ) ,
408+ lifted ) ;
314409 }
315410
316411 return apply ( lhs , rhs ) ;
317412 }
318413
319414 ApplyBinaryNumericPromotions ( op , ref lhs , ref rhs ) ;
415+
416+ if ( lifted )
417+ {
418+ //
419+ // Expression lifts a user-defined operator, such as DateTime subtraction, only if both operands
420+ // are of nullable types, so convert the remaining operand of a non-nullable value type.
421+ //
422+ if ( lhs . Type . IsValueType )
423+ lhs = ConvertOperand ( lhs , lhs . Type . GetNonNullableType ( ) , lifted : true ) ;
424+
425+ if ( rhs . Type . IsValueType )
426+ rhs = ConvertOperand ( rhs , rhs . Type . GetNonNullableType ( ) , lifted : true ) ;
427+ }
428+
320429 var result = apply ( lhs , rhs ) ;
321430
322431 if ( enumType is not null )
@@ -332,29 +441,49 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,
332441 return result ;
333442
334443 default :
335- return Expression . Convert ( result , enumType ) ;
444+ return ConvertOperand ( result , enumType , lifted ) ;
336445 }
337446 }
338447
339448 return result ;
340449 }
341450
451+ /// <summary>
452+ /// Converts an operand to the specified type, or to the nullable form of the type for a lifted operator.
453+ /// </summary>
454+ /// <param name="operand">The expression of the operand.</param>
455+ /// <param name="type">The non-nullable value type to convert to.</param>
456+ /// <param name="lifted"><see langword="true" /> to convert to the nullable form of <paramref name="type"/>.</param>
457+ /// <returns>
458+ /// The converted expression, or <paramref name="operand"/> if it is already of the resulting type.
459+ /// </returns>
460+ private static Expression ConvertOperand ( Expression operand , Type type , bool lifted )
461+ {
462+ if ( lifted )
463+ type = type . MakeNullableType ( ) ;
464+
465+ return operand . Type != type
466+ ? Expression . Convert ( operand , type )
467+ : operand ;
468+ }
469+
342470 private static Expression ApplyUnaryNumericPromotion ( Expression operand )
343471 {
344472 //
345473 // 12.4.7.2 Unary numeric promotions
346474 // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#12472-unary-numeric-promotions
347475 //
348476 // Unary numeric promotion consists of converting operands of type sbyte, byte, short, ushort, or char to type int.
477+ // For a lifted operator, an operand of the nullable form of these types is converted to int?.
349478 //
350- var type = operand . Type ;
479+ var type = operand . Type . GetNonNullableType ( ) ;
351480
352481 if ( type == typeof ( sbyte )
353482 || type == typeof ( byte )
354483 || type == typeof ( short )
355484 || type == typeof ( ushort )
356485 || type == typeof ( char ) )
357- return Expression . Convert ( operand , typeof ( int ) ) ;
486+ return ConvertOperand ( operand , typeof ( int ) , operand . Type . IsNullable ( ) ) ;
358487
359488 return operand ;
360489 }
@@ -380,9 +509,13 @@ private static void ApplyBinaryNumericPromotions(Identifier op, ref Expression l
380509 // both operands are converted to type long.
381510 // * Otherwise, if either operand is of type uint, the other operand is converted to type uint.
382511 // * Otherwise, both operands are converted to type int.
512+ //
513+ // For a lifted operator, the rules apply to the underlying types, and both operands are converted
514+ // to the nullable form of the resulting type.
383515
384- var lhsType = lhs . Type ;
385- var rhsType = rhs . Type ;
516+ var lhsType = lhs . Type . GetNonNullableType ( ) ;
517+ var rhsType = rhs . Type . GetNonNullableType ( ) ;
518+ var lifted = lhs . Type . IsNullable ( ) || rhs . Type . IsNullable ( ) ;
386519
387520 if ( ! TypeUtils . IsNumeric ( lhsType )
388521 || ! TypeUtils . IsNumeric ( rhsType ) )
@@ -418,7 +551,7 @@ private static void ApplyBinaryNumericPromotions(Identifier op, ref Expression l
418551 || lhsType == typeof ( float )
419552 || rhsType == typeof ( double )
420553 || rhsType == typeof ( float ) )
421- Error . NonApplicableBinaryOperator ( op , lhsType , rhsType ) ;
554+ Error . NonApplicableBinaryOperator ( op , lhs . Type , rhs . Type ) ;
422555 }
423556 else if ( conversionType == typeof ( ulong ) )
424557 {
@@ -434,7 +567,7 @@ private static void ApplyBinaryNumericPromotions(Identifier op, ref Expression l
434567 || rhsType == typeof ( short )
435568 || rhsType == typeof ( int )
436569 || rhsType == typeof ( long ) )
437- Error . NonApplicableBinaryOperator ( op , lhsType , rhsType ) ;
570+ Error . NonApplicableBinaryOperator ( op , lhs . Type , rhs . Type ) ;
438571 }
439572 else if ( conversionType == typeof ( uint ) )
440573 {
@@ -451,11 +584,8 @@ private static void ApplyBinaryNumericPromotions(Identifier op, ref Expression l
451584 conversionType = typeof ( long ) ;
452585 }
453586
454- if ( lhsType != conversionType )
455- lhs = Expression . Convert ( lhs , conversionType ) ;
456-
457- if ( rhsType != conversionType )
458- rhs = Expression . Convert ( rhs , conversionType ) ;
587+ lhs = ConvertOperand ( lhs , conversionType , lifted ) ;
588+ rhs = ConvertOperand ( rhs , conversionType , lifted ) ;
459589
460590 return ;
461591 }
0 commit comments