KT-16618 IMPLICIT_INTEGER_COERCION isn't generated for global properties and default parameters

This commit is contained in:
Dmitry Petrov
2017-03-03 14:30:08 +03:00
parent ee6aae7219
commit 8e8f83656f
7 changed files with 124 additions and 8 deletions
@@ -18,6 +18,9 @@ package org.jetbrains.kotlin.psi2ir.transformations
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
@@ -122,6 +125,24 @@ class InsertImplicitCasts(val builtIns: KotlinBuiltIns): IrElementTransformerVoi
return declaration
}
override fun visitField(declaration: IrField): IrStatement {
declaration.transformChildrenVoid(this)
declaration.initializer?.coerce(declaration.descriptor.type)
return declaration
}
override fun visitFunction(declaration: IrFunction): IrStatement {
declaration.transformChildrenVoid(this)
declaration.descriptor.valueParameters.forEach {
declaration.getDefault(it)?.coerce(it.type)
}
return declaration
}
override fun visitWhen(expression: IrWhen): IrExpression {
expression.transformChildrenVoid(this)
@@ -184,6 +205,10 @@ class InsertImplicitCasts(val builtIns: KotlinBuiltIns): IrElementTransformerVoi
return expression
}
private fun IrExpressionBody.coerce(expectedType: KotlinType) {
expression = expression.cast(expectedType)
}
private fun IrExpression.cast(expectedType: KotlinType?): IrExpression {
if (expectedType == null) return this
if (expectedType.isError) return this
@@ -28,7 +28,7 @@ interface IrFunction : IrDeclaration {
override val declarationKind: IrDeclarationKind
get() = IrDeclarationKind.FUNCTION
fun putDefault(parameter: ValueParameterDescriptor, expressionBody: IrBody)
fun getDefault(parameter: ValueParameterDescriptor): IrBody?
fun putDefault(parameter: ValueParameterDescriptor, expressionBody: IrExpressionBody)
fun getDefault(parameter: ValueParameterDescriptor): IrExpressionBody?
}
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.ir.declarations
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
interface IrProperty : IrDeclaration {
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.ir.declarations.impl
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import java.util.*
@@ -29,12 +29,12 @@ abstract class IrFunctionBase(
endOffset: Int,
origin: IrDeclarationOrigin
) : IrGeneralFunctionBase(startOffset, endOffset, origin), IrFunction {
private val defaults = LinkedHashMap<ValueParameterDescriptor, IrBody>()
private val defaults = LinkedHashMap<ValueParameterDescriptor, IrExpressionBody>()
override fun getDefault(parameter: ValueParameterDescriptor): IrBody? =
override fun getDefault(parameter: ValueParameterDescriptor): IrExpressionBody? =
defaults[parameter]
override fun putDefault(parameter: ValueParameterDescriptor, expressionBody: IrBody) {
override fun putDefault(parameter: ValueParameterDescriptor, expressionBody: IrExpressionBody) {
defaults[parameter] = expressionBody
}
@@ -26,6 +26,9 @@ interface IrBody : IrElement {
interface IrExpressionBody : IrBody {
var expression: IrExpression
override fun <D> transform(transformer: IrElementTransformer<D>, data: D): IrExpressionBody =
accept(transformer, data) as IrExpressionBody
}
interface IrBlockBody : IrBody, IrStatementContainer
@@ -1,3 +1,10 @@
val test1: Long = 42
val test2: Short = 42
val test3: Byte = 42
val test4: Long = 42.unaryMinus()
val test5: Short = 42.unaryMinus()
val test6: Byte = 42.unaryMinus()
fun test() {
val test1: Int? = 42
val test2: Long = 42
@@ -6,4 +13,8 @@ fun test() {
val test5: Long? = 1.unaryMinus()
val test6: Short? = 1.unaryMinus()
val test7: Byte? = 1.unaryMinus()
}
}
fun testImplicitArguments(x: Long = 1.unaryMinus()) {}
class TestImplicitArguments(val x: Long = 1.unaryMinus())
@@ -1,4 +1,58 @@
FILE /primitivesImplicitConversions.kt
PROPERTY public val test1: kotlin.Long = 42.toLong()
FIELD PROPERTY_BACKING_FIELD public val test1: kotlin.Long = 42.toLong()
EXPRESSION_BODY
CONST Long type=kotlin.Long value='42'
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test1>(): kotlin.Long
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-test1>(): Long'
GET_FIELD 'test1: Long' type=kotlin.Long origin=null
PROPERTY public val test2: kotlin.Short = 42.toShort()
FIELD PROPERTY_BACKING_FIELD public val test2: kotlin.Short = 42.toShort()
EXPRESSION_BODY
CONST Short type=kotlin.Short value='42'
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test2>(): kotlin.Short
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-test2>(): Short'
GET_FIELD 'test2: Short' type=kotlin.Short origin=null
PROPERTY public val test3: kotlin.Byte = 42.toByte()
FIELD PROPERTY_BACKING_FIELD public val test3: kotlin.Byte = 42.toByte()
EXPRESSION_BODY
CONST Byte type=kotlin.Byte value='42'
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test3>(): kotlin.Byte
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-test3>(): Byte'
GET_FIELD 'test3: Byte' type=kotlin.Byte origin=null
PROPERTY public val test4: kotlin.Long = -42.toLong()
FIELD PROPERTY_BACKING_FIELD public val test4: kotlin.Long = -42.toLong()
EXPRESSION_BODY
TYPE_OP origin=IMPLICIT_INTEGER_COERCION typeOperand=kotlin.Long
CALL 'unaryMinus(): Int' type=kotlin.Int origin=null
$this: CONST Int type=kotlin.Int value='42'
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test4>(): kotlin.Long
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-test4>(): Long'
GET_FIELD 'test4: Long' type=kotlin.Long origin=null
PROPERTY public val test5: kotlin.Short = -42.toShort()
FIELD PROPERTY_BACKING_FIELD public val test5: kotlin.Short = -42.toShort()
EXPRESSION_BODY
TYPE_OP origin=IMPLICIT_INTEGER_COERCION typeOperand=kotlin.Short
CALL 'unaryMinus(): Int' type=kotlin.Int origin=null
$this: CONST Int type=kotlin.Int value='42'
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test5>(): kotlin.Short
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-test5>(): Short'
GET_FIELD 'test5: Short' type=kotlin.Short origin=null
PROPERTY public val test6: kotlin.Byte = -42.toByte()
FIELD PROPERTY_BACKING_FIELD public val test6: kotlin.Byte = -42.toByte()
EXPRESSION_BODY
TYPE_OP origin=IMPLICIT_INTEGER_COERCION typeOperand=kotlin.Byte
CALL 'unaryMinus(): Int' type=kotlin.Int origin=null
$this: CONST Int type=kotlin.Int value='42'
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test6>(): kotlin.Byte
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-test6>(): Byte'
GET_FIELD 'test6: Byte' type=kotlin.Byte origin=null
FUN public fun test(): kotlin.Unit
BLOCK_BODY
VAR val test1: kotlin.Int? = 42
@@ -21,3 +75,27 @@ FILE /primitivesImplicitConversions.kt
TYPE_OP origin=IMPLICIT_INTEGER_COERCION typeOperand=kotlin.Byte
CALL 'unaryMinus(): Int' type=kotlin.Int origin=null
$this: CONST Int type=kotlin.Int value='1'
FUN public fun testImplicitArguments(x: kotlin.Long = ...): kotlin.Unit
x: EXPRESSION_BODY
TYPE_OP origin=IMPLICIT_INTEGER_COERCION typeOperand=kotlin.Long
CALL 'unaryMinus(): Int' type=kotlin.Int origin=null
$this: CONST Int type=kotlin.Int value='1'
BLOCK_BODY
CLASS CLASS TestImplicitArguments
CONSTRUCTOR public constructor TestImplicitArguments(x: kotlin.Long = ...)
x: EXPRESSION_BODY
TYPE_OP origin=IMPLICIT_INTEGER_COERCION typeOperand=kotlin.Long
CALL 'unaryMinus(): Int' type=kotlin.Int origin=null
$this: CONST Int type=kotlin.Int value='1'
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
INSTANCE_INITIALIZER_CALL classDescriptor='TestImplicitArguments'
PROPERTY public final val x: kotlin.Long
FIELD PROPERTY_BACKING_FIELD public final val x: kotlin.Long
EXPRESSION_BODY
GET_VAR 'value-parameter x: Long = ...' type=kotlin.Long origin=INITIALIZE_PROPERTY_FROM_PARAMETER
FUN DEFAULT_PROPERTY_ACCESSOR public final fun <get-x>(): kotlin.Long
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-x>(): Long'
GET_FIELD 'x: Long' type=kotlin.Long origin=null
receiver: GET_VAR '<receiver: TestImplicitArguments>' type=TestImplicitArguments origin=null