diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt index 54b9e313ecd..8ac971aeafd 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt @@ -99,8 +99,10 @@ class StatementGenerator( } override fun visitBlockExpression(expression: KtBlockExpression, data: Nothing?): IrStatement { - val irBlock = IrBlockImpl(expression.startOffset, expression.endOffset, - getReturnType(expression), isUsedAsExpression(expression)) + val isBlockBody = expression.parent is KtNamedFunction + val hasResult = if (isBlockBody) false else isUsedAsExpression(expression) + val returnType = if (isBlockBody || !hasResult) null else getReturnType(expression) + val irBlock = IrBlockImpl(expression.startOffset, expression.endOffset, returnType, hasResult) expression.statements.forEach { irBlock.addStatement(it.genStmt()) } return irBlock } diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt index 3349d5c5ce4..a370c552b2a 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt @@ -53,6 +53,16 @@ class InsertImplicitCasts(val builtIns: KotlinBuiltIns): IrElementVisitor hasResult=false operator=null BLOCK type=kotlin.Int hasResult=true operator=POSTFIX_INCR VAR val tmp0_array: kotlin.IntArray CALL . type=kotlin.IntArray operator=GET_PROPERTY diff --git a/compiler/testData/ir/irText/augmentedAssignment1.txt b/compiler/testData/ir/irText/augmentedAssignment1.txt index ef65438542a..4d4f8badf43 100644 --- a/compiler/testData/ir/irText/augmentedAssignment1.txt +++ b/compiler/testData/ir/irText/augmentedAssignment1.txt @@ -4,7 +4,7 @@ IrFile /augmentedAssignment1.kt CONST Int type=kotlin.Int value='0' IrFunction public fun testVariable(): kotlin.Unit IrExpressionBody - BLOCK type=kotlin.Int hasResult=true operator=null + BLOCK type= hasResult=false operator=null VAR var x: kotlin.Int CONST Int type=kotlin.Int value='0' SET_VAR x type= operator=PLUSEQ @@ -29,7 +29,7 @@ IrFile /augmentedAssignment1.kt other: CONST Int type=kotlin.Int value='5' IrFunction public fun testProperty(): kotlin.Unit IrExpressionBody - BLOCK type=kotlin.Int hasResult=true operator=null + BLOCK type= hasResult=false operator=null CALL . type=kotlin.Unit operator=PLUSEQ : CALL .plus type=kotlin.Int operator=PLUSEQ $this: CALL . type=kotlin.Int operator=PLUSEQ diff --git a/compiler/testData/ir/irText/safeCallWithIncrementDecrement.txt b/compiler/testData/ir/irText/safeCallWithIncrementDecrement.txt index 8937ec983d0..e5ce39ea5c3 100644 --- a/compiler/testData/ir/irText/safeCallWithIncrementDecrement.txt +++ b/compiler/testData/ir/irText/safeCallWithIncrementDecrement.txt @@ -33,7 +33,7 @@ IrFile /safeCallWithIncrementDecrement.kt BLOCK type= hasResult=false operator=null IrFunction public fun testProperty(/*0*/ nc: test.C?): kotlin.Unit IrExpressionBody - BLOCK type=kotlin.Int? hasResult=true operator=null + BLOCK type= hasResult=false operator=null BLOCK type=kotlin.Int? hasResult=true operator=SAFE_CALL VAR val tmp0_safe_receiver: test.C? GET_VAR nc type=test.C? operator=null @@ -51,3 +51,28 @@ IrFile /safeCallWithIncrementDecrement.kt value: CALL .inc type=kotlin.Int? operator=POSTFIX_INCR $receiver: GET_VAR tmp1 type=kotlin.Int operator=null GET_VAR tmp1 type=kotlin.Int operator=null + IrFunction public fun testArrayAccess(/*0*/ nc: test.C?): kotlin.Unit + IrExpressionBody + BLOCK type= hasResult=false operator=null + BLOCK type=kotlin.Int hasResult=true operator=POSTFIX_INCR + VAR val tmp3_array: kotlin.Int? + BLOCK type=kotlin.Int? hasResult=true operator=SAFE_CALL + VAR val tmp0_safe_receiver: test.C? + GET_VAR nc type=test.C? operator=null + WHEN type=kotlin.Int? operator=SAFE_CALL + if: CALL .EQEQ type=kotlin.Boolean operator=EQEQ + arg0: GET_VAR tmp0_safe_receiver type=test.C? operator=null + arg1: CONST Null type=kotlin.Nothing? value='null' + then: CONST Null type=kotlin.Nothing? value='null' + else: CALL . type=kotlin.Int operator=GET_PROPERTY + $this: GET_VAR tmp0_safe_receiver type=test.C? operator=null + VAR val tmp4: kotlin.Int + CALL .get type=kotlin.Int operator=POSTFIX_INCR + $receiver: GET_VAR tmp3_array type=kotlin.Int? operator=null + index: CONST Int type=kotlin.Int value='0' + CALL .set type=kotlin.Unit operator=POSTFIX_INCR + $receiver: GET_VAR tmp3_array type=kotlin.Int? operator=null + index: CONST Int type=kotlin.Int value='0' + value: CALL .inc type=kotlin.Int operator=POSTFIX_INCR + $this: GET_VAR tmp4 type=kotlin.Int operator=null + GET_VAR tmp4 type=kotlin.Int operator=null