FIR2IR: fix inverted types for array/element in arrayOf call.

This commit is contained in:
Jinseong Jeon
2020-05-18 11:47:13 -07:00
committed by Mikhail Glukhikh
parent b93868c30b
commit ff509bdcd3
3 changed files with 25 additions and 7 deletions
@@ -41,9 +41,7 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.makeNotNull
import org.jetbrains.kotlin.ir.types.makeNullable
import org.jetbrains.kotlin.ir.util.constructors import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.defaultType import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
@@ -760,10 +758,22 @@ class Fir2IrVisitor(
override fun visitArrayOfCall(arrayOfCall: FirArrayOfCall, data: Any?): IrElement { override fun visitArrayOfCall(arrayOfCall: FirArrayOfCall, data: Any?): IrElement {
return arrayOfCall.convertWithOffsets { startOffset, endOffset -> 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( IrVarargImpl(
startOffset, endOffset, startOffset, endOffset,
type = arrayOfCall.arguments.firstOrNull()?.typeRef?.toIrType() ?: createErrorType(), type = arrayType,
varargElementType = arrayOfCall.typeRef.toIrType(), varargElementType = elementType,
elements = arrayOfCall.arguments.map { elements = arrayOfCall.arguments.map {
convertToIrExpression(it) convertToIrExpression(it)
} }
@@ -88,7 +88,7 @@ class ExpressionsConverter(
CALL_EXPRESSION -> convertCallExpression(expression) CALL_EXPRESSION -> convertCallExpression(expression)
WHEN -> convertWhenExpression(expression) WHEN -> convertWhenExpression(expression)
ARRAY_ACCESS_EXPRESSION -> convertArrayAccessExpression(expression) ARRAY_ACCESS_EXPRESSION -> convertArrayAccessExpression(expression)
COLLECTION_LITERAL_EXPRESSION -> convertCollectionLiteralExpresion(expression) COLLECTION_LITERAL_EXPRESSION -> convertCollectionLiteralExpression(expression)
STRING_TEMPLATE -> convertStringTemplate(expression) STRING_TEMPLATE -> convertStringTemplate(expression)
is KtConstantExpressionElementType -> convertConstantExpression(expression) is KtConstantExpressionElementType -> convertConstantExpression(expression)
REFERENCE_EXPRESSION -> convertSimpleNameExpression(expression) REFERENCE_EXPRESSION -> convertSimpleNameExpression(expression)
@@ -808,7 +808,7 @@ class ExpressionsConverter(
/** /**
* @see org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseCollectionLiteralExpression * @see org.jetbrains.kotlin.parsing.KotlinExpressionParsing.parseCollectionLiteralExpression
*/ */
private fun convertCollectionLiteralExpresion(expression: LighterASTNode): FirExpression { private fun convertCollectionLiteralExpression(expression: LighterASTNode): FirExpression {
val firExpressionList = mutableListOf<FirExpression>() val firExpressionList = mutableListOf<FirExpression>()
expression.forEachChildren { expression.forEachChildren {
if (it.isExpression()) firExpressionList += getAsFirExpression<FirExpression>(it, "Incorrect collection literal argument") if (it.isExpression()) firExpressionList += getAsFirExpression<FirExpression>(it, "Incorrect collection literal argument")
@@ -108,3 +108,11 @@ fun IrType.getArrayElementType(irBuiltIns: IrBuiltIns): IrType =
irBuiltIns.primitiveArrayElementTypes[classifier] irBuiltIns.primitiveArrayElementTypes[classifier]
?: throw AssertionError("Primitive array expected: $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)
}