Report warning from backend if constant expression cannot be evaluated
This commit is contained in:
+10
-1
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+9
-4
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user