@@ -7455,17 +7455,33 @@ export class Compiler extends DiagnosticEmitter {
7455
7455
switch ( expression . literalKind ) {
7456
7456
case LiteralKind . ARRAY : {
7457
7457
assert ( ! implicitlyNegate ) ;
7458
- let classType = contextualType . classReference ;
7459
- if ( classType ) {
7460
- if ( classType . prototype == this . program . arrayPrototype ) {
7461
- return this . compileArrayLiteral (
7462
- assert ( classType . typeArguments ) [ 0 ] ,
7463
- ( < ArrayLiteralExpression > expression ) . elementExpressions ,
7464
- constraints ,
7465
- expression
7466
- ) ;
7458
+ let elementExpressions = ( < ArrayLiteralExpression > expression ) . elementExpressions ;
7459
+
7460
+ // Infer from first element in auto contexts
7461
+ if ( contextualType == Type . auto ) {
7462
+ return this . compileArrayLiteral (
7463
+ Type . auto ,
7464
+ elementExpressions ,
7465
+ constraints ,
7466
+ expression
7467
+ ) ;
7468
+ }
7469
+
7470
+ // Use contextual type if an array
7471
+ if ( contextualType . is ( TypeFlags . REFERENCE ) ) {
7472
+ let classType = contextualType . classReference ;
7473
+ if ( classType ) {
7474
+ if ( classType . prototype == this . program . arrayPrototype ) {
7475
+ return this . compileArrayLiteral (
7476
+ assert ( classType . typeArguments ) [ 0 ] ,
7477
+ elementExpressions ,
7478
+ constraints ,
7479
+ expression
7480
+ ) ;
7481
+ }
7467
7482
}
7468
7483
}
7484
+
7469
7485
this . error (
7470
7486
DiagnosticCode . The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly ,
7471
7487
expression . range , "T"
@@ -7544,17 +7560,42 @@ export class Compiler extends DiagnosticEmitter {
7544
7560
var module = this . module ;
7545
7561
var program = this . program ;
7546
7562
var arrayPrototype = assert ( program . arrayPrototype ) ;
7547
- var arrayInstance = assert ( this . resolver . resolveClass ( arrayPrototype , [ elementType ] ) ) ;
7548
7563
var arrayBufferInstance = assert ( program . arrayBufferInstance ) ;
7549
- var arrayType = arrayInstance . type ;
7550
7564
var flow = this . currentFlow ;
7551
7565
7552
7566
// block those here so compiling expressions doesn't conflict
7553
- var tempThis = flow . getTempLocal ( arrayType ) ;
7567
+ var tempThis = flow . getTempLocal ( this . options . usizeType ) ;
7554
7568
var tempDataStart = flow . getTempLocal ( arrayBufferInstance . type ) ;
7555
7569
7556
- // compile value expressions and find out whether all are constant
7570
+ // infer common element type in auto contexts
7557
7571
var length = expressions . length ;
7572
+ if ( elementType == Type . auto ) {
7573
+ for ( let i = 0 ; i < length ; ++ i ) {
7574
+ let expression = expressions [ i ] ;
7575
+ if ( expression ) {
7576
+ let currentType = this . resolver . resolveExpression ( expression , this . currentFlow , elementType ) ;
7577
+ if ( ! currentType ) return module . unreachable ( ) ;
7578
+ if ( elementType == Type . auto ) elementType = currentType ;
7579
+ else if ( currentType != elementType ) {
7580
+ let commonType = Type . commonDenominator ( elementType , currentType , false ) ;
7581
+ if ( commonType ) elementType = commonType ;
7582
+ // otherwise triggers error further down
7583
+ }
7584
+ }
7585
+ }
7586
+ if ( elementType /* still */ == Type . auto ) {
7587
+ this . error (
7588
+ DiagnosticCode . The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly ,
7589
+ reportNode . range , "T"
7590
+ ) ;
7591
+ return module . unreachable ( ) ;
7592
+ }
7593
+ }
7594
+
7595
+ var arrayInstance = assert ( this . resolver . resolveClass ( arrayPrototype , [ elementType ] ) ) ;
7596
+ var arrayType = arrayInstance . type ;
7597
+
7598
+ // compile value expressions and find out whether all are constant
7558
7599
var values = new Array < ExpressionRef > ( length ) ;
7559
7600
var isStatic = true ;
7560
7601
var nativeElementType = elementType . toNativeType ( ) ;
0 commit comments