Add jvm diagnostic tests to check report of exceptions from interpreter

This commit is contained in:
Ivan Kylchik
2021-11-22 23:32:43 +03:00
parent 20d8e6607d
commit 1431d4b356
8 changed files with 78 additions and 37 deletions
@@ -1,29 +0,0 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.phaser.makeIrModulePhase
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
import org.jetbrains.kotlin.ir.interpreter.checker.IrConstTransformer
val constEvaluationPhase = makeIrModulePhase(
::ConstEvaluationLowering,
name = "ConstEvaluationLowering",
description = "Evaluate functions that are marked as `Foldable`"
)
class ConstEvaluationLowering(val context: CommonBackendContext) : FileLoweringPass {
val interpreter = IrInterpreter(context.irBuiltIns)
override fun lower(irFile: IrFile) {
irFile.transformChildren(IrConstTransformer(interpreter, irFile, mode = EvaluationMode.ONLY_INTRINSIC_CONST), null)
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.phaser.makeIrModulePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
import org.jetbrains.kotlin.ir.interpreter.checker.IrConstTransformer
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmBackendErrors
val constEvaluationPhase = makeIrModulePhase(
::ConstEvaluationLowering,
name = "ConstEvaluationLowering",
description = "Evaluate functions that are marked as `IntrinsicConstEvaluation`"
)
// TODO make context common
class ConstEvaluationLowering(val context: JvmBackendContext) : FileLoweringPass {
val interpreter = IrInterpreter(context.irBuiltIns)
override fun lower(irFile: IrFile) {
val transformer = IrConstTransformer(interpreter, irFile, mode = EvaluationMode.ONLY_INTRINSIC_CONST) { element, error ->
context.ktDiagnosticReporter.at(element, irFile)
.report(JvmBackendErrors.EXCEPTION_IN_CONST_VAL_INITIALIZER, error.description)
}
irFile.transformChildren(transformer, null)
}
}
@@ -5,11 +5,9 @@
package org.jetbrains.kotlin.ir.interpreter.checker
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
import org.jetbrains.kotlin.ir.declarations.IrDeclarationBase
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
@@ -19,12 +17,23 @@ import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
class IrConstTransformer(
private val interpreter: IrInterpreter, private val irFile: IrFile, private val mode: EvaluationMode
private val interpreter: IrInterpreter,
private val irFile: IrFile,
private val mode: EvaluationMode,
private val onError: (IrElement, IrErrorExpression) -> Unit = { _, _ -> }
) : IrElementTransformerVoid() {
private fun IrExpression.replaceIfError(original: IrExpression): IrExpression {
return if (this !is IrErrorExpression) this else original
}
private fun IrExpression.reportIfError(original: IrExpression): IrExpression {
if (this is IrErrorExpression) {
onError(original, this)
return original
}
return this
}
override fun visitCall(expression: IrCall): IrExpression {
if (expression.accept(IrCompileTimeChecker(mode = mode), null)) {
return interpreter.interpret(expression, irFile).replaceIfError(expression)
@@ -40,7 +49,8 @@ class IrConstTransformer(
if (expression is IrConst<*>) return declaration
val isConst = declaration.correspondingPropertySymbol?.owner?.isConst == true
if (isConst && expression.accept(IrCompileTimeChecker(declaration, mode), null)) {
initializer.expression = interpreter.interpret(expression, irFile).replaceIfError(expression)
val result = interpreter.interpret(expression, irFile)
initializer.expression = if (isConst) result.reportIfError(expression) else result.replaceIfError(expression)
}
return declaration
@@ -86,7 +96,7 @@ class IrConstTransformer(
private fun IrExpression.transformSingleArg(expectedType: IrType): IrExpression {
if (this.accept(IrCompileTimeChecker(mode = mode), null)) {
val const = interpreter.interpret(this, irFile).replaceIfError(this)
val const = interpreter.interpret(this, irFile).reportIfError(this)
return const.convertToConstIfPossible(expectedType)
} else if (this is IrConstructorCall) {
transformAnnotation(this)