FIR2IR: fix inverted types for array/element in arrayOf call.
This commit is contained in:
committed by
Mikhail Glukhikh
parent
b93868c30b
commit
ff509bdcd3
@@ -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<Any>
|
||||
// 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)
|
||||
}
|
||||
|
||||
+2
-2
@@ -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<FirExpression>()
|
||||
expression.forEachChildren {
|
||||
if (it.isExpression()) firExpressionList += getAsFirExpression<FirExpression>(it, "Incorrect collection literal argument")
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user