From 4e0d2bc23cbefceaf9c6184b94697ade608b7cba Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Mon, 20 Jul 2015 19:57:43 +0300 Subject: [PATCH] CompileTimeConstantUtils: use getConstant instead of evaluate Hacky way to avoid passing builtins into this code --- .../kotlin/resolve/CompileTimeConstantUtils.java | 9 ++++++--- compiler/testData/codegen/box/when/exhaustiveBoolean.kt | 4 ++++ .../codegen/generated/BlackBoxCodegenTestGenerated.java | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/when/exhaustiveBoolean.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/CompileTimeConstantUtils.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/CompileTimeConstantUtils.java index bd8c2912b9f..14e7f2ee65c 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/CompileTimeConstantUtils.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/CompileTimeConstantUtils.java @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.psi.JetExpression; import org.jetbrains.kotlin.psi.JetParameter; +import org.jetbrains.kotlin.psi.JetPsiUtil; import org.jetbrains.kotlin.psi.JetTypeReference; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.constants.BooleanValue; @@ -131,9 +132,11 @@ public class CompileTimeConstantUtils { @NotNull BindingTrace trace, @Nullable Boolean expectedValue ) { - if (expression == null) return false; - CompileTimeConstant compileTimeConstant = - ConstantExpressionEvaluator.evaluate(expression, trace, KotlinBuiltIns.getInstance().getBooleanType()); + JetExpression effectiveExpression = JetPsiUtil.deparenthesize(expression); + + if (effectiveExpression == null) return false; + + CompileTimeConstant compileTimeConstant = ConstantExpressionEvaluator.getConstant(effectiveExpression, trace.getBindingContext()); if (!(compileTimeConstant instanceof TypedCompileTimeConstant) || compileTimeConstant.getUsesVariableAsConstant()) return false; ConstantValue constantValue = ((TypedCompileTimeConstant) compileTimeConstant).getConstantValue(); diff --git a/compiler/testData/codegen/box/when/exhaustiveBoolean.kt b/compiler/testData/codegen/box/when/exhaustiveBoolean.kt new file mode 100644 index 00000000000..4ac2633da11 --- /dev/null +++ b/compiler/testData/codegen/box/when/exhaustiveBoolean.kt @@ -0,0 +1,4 @@ +fun box() : String = when (true) { + ((true)) -> "OK" + (1 == 2) -> "Not ok" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java index e3921f3bf3f..02c7a073fcc 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -7717,6 +7717,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("exhaustiveBoolean.kt") + public void testExhaustiveBoolean() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/when/exhaustiveBoolean.kt"); + doTest(fileName); + } + @TestMetadata("is.kt") public void testIs() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/when/is.kt");