diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index bf0dcf40726..6722e8f54f6 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -41,9 +41,7 @@ import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* -import org.jetbrains.kotlin.ir.types.IrType -import org.jetbrains.kotlin.ir.types.makeNotNull -import org.jetbrains.kotlin.ir.types.makeNullable +import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.constructors import org.jetbrains.kotlin.ir.util.defaultType import org.jetbrains.kotlin.lexer.KtTokens @@ -760,10 +758,22 @@ class Fir2IrVisitor( override fun visitArrayOfCall(arrayOfCall: FirArrayOfCall, data: Any?): IrElement { return arrayOfCall.convertWithOffsets { startOffset, endOffset -> + lateinit var elementType: IrType + lateinit var arrayType: IrType + if (arrayOfCall.typeRef is FirResolvedTypeRef) { + arrayType = arrayOfCall.typeRef.toIrType() + elementType = arrayType.getArrayElementType(irBuiltIns) + } else { + // TODO: The element type should be the least upper bound of all arguments' types, e.g., ["4", 2u, 0.42f] => Array + // Currently, the type of elements in array literals still has integer literal type, which shouldn't be at this stage. + // elementType = arrayOfCall.arguments.map { it.typeRef.toIrType() }.commonSupertype(irBuiltIns) + elementType = arrayOfCall.arguments.firstOrNull()?.typeRef?.toIrType() ?: createErrorType() + arrayType = elementType.toArrayOrPrimitiveArrayType(irBuiltIns) + } IrVarargImpl( startOffset, endOffset, - type = arrayOfCall.arguments.firstOrNull()?.typeRef?.toIrType() ?: createErrorType(), - varargElementType = arrayOfCall.typeRef.toIrType(), + type = arrayType, + varargElementType = elementType, elements = arrayOfCall.arguments.map { convertToIrExpression(it) } diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt index 2293b5670ae..7206fb2bf9c 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt @@ -88,7 +88,7 @@ class ExpressionsConverter( CALL_EXPRESSION -> convertCallExpression(expression) WHEN -> convertWhenExpression(expression) ARRAY_ACCESS_EXPRESSION -> convertArrayAccessExpression(expression) - COLLECTION_LITERAL_EXPRESSION -> convertCollectionLiteralExpresion(expression) + COLLECTION_LITERAL_EXPRESSION -> convertCollectionLiteralExpression(expression) STRING_TEMPLATE -> convertStringTemplate(expression) is KtConstantExpressionElementType -> convertConstantExpression(expression) REFERENCE_EXPRESSION -> convertSimpleNameExpression(expression) @@ -808,7 +808,7 @@ class ExpressionsConverter( /** * @see org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseCollectionLiteralExpression */ - private fun convertCollectionLiteralExpresion(expression: LighterASTNode): FirExpression { + private fun convertCollectionLiteralExpression(expression: LighterASTNode): FirExpression { val firExpressionList = mutableListOf() expression.forEachChildren { if (it.isExpression()) firExpressionList += getAsFirExpression(it, "Incorrect collection literal argument") diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt index d8c981d8ed6..7083e1c3d92 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt @@ -108,3 +108,11 @@ fun IrType.getArrayElementType(irBuiltIns: IrBuiltIns): IrType = irBuiltIns.primitiveArrayElementTypes[classifier] ?: throw AssertionError("Primitive array expected: $classifier") } + +fun IrType.toArrayOrPrimitiveArrayType(irBuiltIns: IrBuiltIns): IrType = + if (isPrimitiveType()) { + irBuiltIns.primitiveArrayForType[this]?.defaultType + ?: throw AssertionError("$this not in primitiveArrayForType") + } else { + irBuiltIns.arrayClass.typeWith(this) + }