@@ -1469,38 +1469,73 @@ private BoundExpression BuildParamsArray(
1469
1469
// if it's available. However, we also disable the optimization if we're in an expression lambda, the
1470
1470
// point of which is just to represent the semantics of an operation, and we don't know that all consumers
1471
1471
// of expression lambdas will appropriately understand Array.Empty<T>().
1472
- // We disable it for pointer types as well, since they cannot be used as Type Arguments.
1473
1472
if ( arrayArgs . Length == 0
1474
1473
&& ! _inExpressionLambda
1475
- && paramArrayType is ArrayTypeSymbol ats // could be false if there's a semantic error, e.g. the params parameter type isn't an array
1476
- && ! ats . ElementType . IsPointerOrFunctionPointer ( ) )
1474
+ && paramArrayType is ArrayTypeSymbol ats ) // could be false if there's a semantic error, e.g. the params parameter type isn't an array
1477
1475
{
1478
- MethodSymbol ? arrayEmpty = _compilation . GetWellKnownTypeMember ( WellKnownMember . System_Array__Empty ) as MethodSymbol ;
1479
- if ( arrayEmpty != null ) // will be null if Array.Empty<T> doesn't exist in reference assemblies
1476
+ BoundExpression ? arrayEmpty = CreateArrayEmptyCallIfAvailable ( syntax , ats . ElementType ) ;
1477
+ if ( arrayEmpty is { } )
1480
1478
{
1481
- _diagnostics . ReportUseSite ( arrayEmpty , syntax ) ;
1482
- // return an invocation of "Array.Empty<T>()"
1483
- arrayEmpty = arrayEmpty . Construct ( ImmutableArray . Create ( ats . ElementType ) ) ;
1484
- return new BoundCall (
1485
- syntax ,
1486
- null ,
1487
- arrayEmpty ,
1488
- ImmutableArray < BoundExpression > . Empty ,
1489
- default ( ImmutableArray < string > ) ,
1490
- default ( ImmutableArray < RefKind > ) ,
1491
- isDelegateCall : false ,
1492
- expanded : false ,
1493
- invokedAsExtensionMethod : false ,
1494
- argsToParamsOpt : default ( ImmutableArray < int > ) ,
1495
- defaultArguments : default ( BitVector ) ,
1496
- resultKind : LookupResultKind . Viable ,
1497
- type : arrayEmpty . ReturnType ) ;
1479
+ return arrayEmpty ;
1498
1480
}
1499
1481
}
1500
1482
1501
1483
return CreateParamArrayArgument ( syntax , paramArrayType , arrayArgs , _compilation , this ) ;
1502
1484
}
1503
1485
1486
+ private BoundExpression CreateEmptyArray ( SyntaxNode syntax , ArrayTypeSymbol arrayType )
1487
+ {
1488
+ BoundExpression ? arrayEmpty = CreateArrayEmptyCallIfAvailable ( syntax , arrayType . ElementType ) ;
1489
+ if ( arrayEmpty is { } )
1490
+ {
1491
+ return arrayEmpty ;
1492
+ }
1493
+ // new T[0]
1494
+ return new BoundArrayCreation (
1495
+ syntax ,
1496
+ ImmutableArray . Create < BoundExpression > (
1497
+ new BoundLiteral (
1498
+ syntax ,
1499
+ ConstantValue . Create ( 0 ) ,
1500
+ _compilation . GetSpecialType ( SpecialType . System_Int32 ) ) ) ,
1501
+ initializerOpt : null ,
1502
+ arrayType )
1503
+ { WasCompilerGenerated = true } ;
1504
+ }
1505
+
1506
+ private BoundExpression ? CreateArrayEmptyCallIfAvailable ( SyntaxNode syntax , TypeSymbol elementType )
1507
+ {
1508
+ if ( elementType . IsPointerOrFunctionPointer ( ) )
1509
+ {
1510
+ // Pointer types cannot be used as type arguments.
1511
+ return null ;
1512
+ }
1513
+
1514
+ MethodSymbol ? arrayEmpty = _compilation . GetWellKnownTypeMember ( WellKnownMember . System_Array__Empty ) as MethodSymbol ;
1515
+ if ( arrayEmpty is null ) // will be null if Array.Empty<T> doesn't exist in reference assemblies
1516
+ {
1517
+ return null ;
1518
+ }
1519
+
1520
+ _diagnostics . ReportUseSite ( arrayEmpty , syntax ) ;
1521
+ // return an invocation of "Array.Empty<T>()"
1522
+ arrayEmpty = arrayEmpty . Construct ( ImmutableArray . Create ( elementType ) ) ;
1523
+ return new BoundCall (
1524
+ syntax ,
1525
+ null ,
1526
+ arrayEmpty ,
1527
+ ImmutableArray < BoundExpression > . Empty ,
1528
+ default ( ImmutableArray < string > ) ,
1529
+ default ( ImmutableArray < RefKind > ) ,
1530
+ isDelegateCall : false ,
1531
+ expanded : false ,
1532
+ invokedAsExtensionMethod : false ,
1533
+ argsToParamsOpt : default ( ImmutableArray < int > ) ,
1534
+ defaultArguments : default ( BitVector ) ,
1535
+ resultKind : LookupResultKind . Viable ,
1536
+ type : arrayEmpty . ReturnType ) ;
1537
+ }
1538
+
1504
1539
private static BoundExpression CreateParamArrayArgument ( SyntaxNode syntax ,
1505
1540
TypeSymbol paramArrayType ,
1506
1541
ImmutableArray < BoundExpression > arrayArgs ,
0 commit comments