diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/JvmBackendErrors.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/JvmBackendErrors.kt index ced0ba58c87..2e4af0d0e0a 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/JvmBackendErrors.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/JvmBackendErrors.kt @@ -7,11 +7,8 @@ package org.jetbrains.kotlin.resolve.jvm.diagnostics import com.intellij.psi.PsiElement import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap +import org.jetbrains.kotlin.diagnostics.* import org.jetbrains.kotlin.diagnostics.SourceElementPositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT -import org.jetbrains.kotlin.diagnostics.error0 -import org.jetbrains.kotlin.diagnostics.error1 -import org.jetbrains.kotlin.diagnostics.error2 import org.jetbrains.kotlin.diagnostics.rendering.* import org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers.STRING import org.jetbrains.kotlin.resolve.MemberComparator @@ -35,6 +32,7 @@ object JvmBackendErrors { val SCRIPT_CAPTURING_ENUM_ENTRY by error1() val EXCEPTION_IN_CONST_VAL_INITIALIZER by error1() + val EXCEPTION_IN_CONST_EXPRESSION by warning1() init { RootDiagnosticRendererFactory.registerFactory(KtDefaultJvmErrorMessages) @@ -77,5 +75,6 @@ object KtDefaultJvmErrorMessages : BaseDiagnosticRendererFactory() { map.put(JvmBackendErrors.SCRIPT_CAPTURING_ENUM_ENTRY, "Enum entry {0} captures the script class instance. Try to use class or anonymous object instead", STRING) map.put(JvmBackendErrors.EXCEPTION_IN_CONST_VAL_INITIALIZER, "Cannot evaluate constant expression: {0}", STRING) + map.put(JvmBackendErrors.EXCEPTION_IN_CONST_EXPRESSION, "Constant expression will throw an exception at runtime: {0}", STRING) } } diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstEvaluationLowering.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstEvaluationLowering.kt index 51962e5580b..8ea21b1521b 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstEvaluationLowering.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstEvaluationLowering.kt @@ -8,7 +8,9 @@ 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.IrElement import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.expressions.IrErrorExpression import org.jetbrains.kotlin.ir.interpreter.IrInterpreter import org.jetbrains.kotlin.ir.interpreter.IrInterpreterConfiguration import org.jetbrains.kotlin.ir.interpreter.IrInterpreterEnvironment @@ -28,10 +30,17 @@ class ConstEvaluationLowering(val context: JvmBackendContext) : FileLoweringPass val interpreter = IrInterpreter(IrInterpreterEnvironment(context.irBuiltIns, configuration), emptyMap()) override fun lower(irFile: IrFile) { - val transformer = IrConstTransformer(interpreter, irFile, mode = EvaluationMode.ONLY_INTRINSIC_CONST) { element, error -> + fun onError(element: IrElement, error: IrErrorExpression) { context.ktDiagnosticReporter.at(element, irFile) .report(JvmBackendErrors.EXCEPTION_IN_CONST_VAL_INITIALIZER, error.description) } + + fun onWarning(element: IrElement, warning: IrErrorExpression) { + context.ktDiagnosticReporter.at(element, irFile) + .report(JvmBackendErrors.EXCEPTION_IN_CONST_EXPRESSION, warning.description) + } + + val transformer = IrConstTransformer(interpreter, irFile, mode = EvaluationMode.ONLY_INTRINSIC_CONST, ::onWarning, ::onError) irFile.transformChildren(transformer, null) } } diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt index d3cd14f9ca2..6b5dfbaa41f 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt @@ -21,10 +21,15 @@ class IrConstTransformer( private val interpreter: IrInterpreter, private val irFile: IrFile, private val mode: EvaluationMode, + private val onWarning: (IrElement, IrErrorExpression) -> Unit = { _, _ -> }, 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.warningIfError(original: IrExpression): IrExpression { + if (this is IrErrorExpression) { + onWarning(original, this) + return original + } + return this } private fun IrExpression.reportIfError(original: IrExpression): IrExpression { @@ -41,7 +46,7 @@ class IrConstTransformer( override fun visitCall(expression: IrCall): IrExpression { if (expression.accept(IrCompileTimeChecker(mode = mode), null)) { - return interpreter.interpret(expression, irFile).replaceIfError(expression) + return interpreter.interpret(expression, irFile).warningIfError(expression) } return super.visitCall(expression) } @@ -55,7 +60,7 @@ class IrConstTransformer( val isConst = declaration.correspondingPropertySymbol?.owner?.isConst == true if (isConst && expression.accept(IrCompileTimeChecker(declaration, mode), null)) { val result = interpreter.interpret(expression, irFile) - initializer.expression = if (isConst) result.reportIfError(expression) else result.replaceIfError(expression) + initializer.expression = result.reportIfError(expression) } return super.visitField(declaration) diff --git a/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.diag.txt b/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.diag.txt index 4b1f802bb26..3924c674be2 100644 --- a/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.diag.txt +++ b/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.diag.txt @@ -1,5 +1,7 @@ /exceptionFromInterpreter_ir.kt:6:26: error: Cannot evaluate constant expression: / by zero -/exceptionFromInterpreter_ir.kt:7:39: error: Cannot evaluate constant expression: marginPrefix must be non-blank string. +/exceptionFromInterpreter_ir.kt:7:26: warning: Constant expression will throw an exception at runtime: / by zero -/exceptionFromInterpreter_ir.kt:11:4: error: Cannot evaluate constant expression: / by zero \ No newline at end of file +/exceptionFromInterpreter_ir.kt:8:39: error: Cannot evaluate constant expression: marginPrefix must be non-blank string. + +/exceptionFromInterpreter_ir.kt:12:4: error: Cannot evaluate constant expression: / by zero \ No newline at end of file diff --git a/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.kt b/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.kt index ae057e9f45e..c07176dc7cd 100644 --- a/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.kt +++ b/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.kt @@ -4,9 +4,10 @@ // WITH_STDLIB const val divideByZero = 1 / 0 +val disivionByZeroWarn = 1 / 0 const val trimMarginException = "123".trimMargin(" ") annotation class A(val i: Int, val b: Int) @A(1 / 0, 2) -fun foo() {} \ No newline at end of file +fun foo() {}