diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt index 315bf9f13f2..83fc9d42d72 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/GenerationState.kt @@ -229,10 +229,13 @@ class GenerationState private constructor( val shouldInlineConstVals = languageVersionSettings.supportsFeature(LanguageFeature.InlineConstVals) - val constructorCallNormalizationMode = configuration.get( - JVMConfigurationKeys.CONSTRUCTOR_CALL_NORMALIZATION_MODE, - JVMConstructorCallNormalizationMode.DEFAULT - ) + val constructorCallNormalizationMode = + configuration.get(JVMConfigurationKeys.CONSTRUCTOR_CALL_NORMALIZATION_MODE) ?: run { + if (languageVersionSettings.supportsFeature(LanguageFeature.NormalizeConstructorCalls)) + JVMConstructorCallNormalizationMode.ENABLE + else + JVMConstructorCallNormalizationMode.DISABLE + } val jvmDefaultMode = languageVersionSettings.getFlag(AnalysisFlag.jvmDefaultMode) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt index 88b8b3f6d02..7fc0487d9e1 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt @@ -112,7 +112,7 @@ class K2JVMCompilerArguments : CommonCompilerArguments() { valueDescription = "{disable|enable}", description = "Normalize constructor calls (disable: don't normalize; enable: normalize), default is disable" ) - var constructorCallNormalizationMode: String? by FreezableVar(JVMConstructorCallNormalizationMode.DEFAULT.description) + var constructorCallNormalizationMode: String? by FreezableVar(null) @Argument( value = "-Xassertions", valueDescription = "{always-enable|always-disable|jvm|legacy}", diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index ab6d1f523f7..6951704968f 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -359,19 +359,22 @@ class K2JVMCompiler : CLICompiler() { ) configuration.put(JVMConfigurationKeys.DISABLE_OPTIMIZATION, arguments.noOptimize) - val constructorCallNormalizationMode = - JVMConstructorCallNormalizationMode.fromStringOrNull(arguments.constructorCallNormalizationMode) - if (constructorCallNormalizationMode == null) { + if (!JVMConstructorCallNormalizationMode.isSupportedValue(arguments.constructorCallNormalizationMode)) { configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).report( ERROR, "Unknown constructor call normalization mode: ${arguments.constructorCallNormalizationMode}, " + "supported modes: ${JVMConstructorCallNormalizationMode.values().map { it.description }}" ) } - configuration.put( - JVMConfigurationKeys.CONSTRUCTOR_CALL_NORMALIZATION_MODE, - constructorCallNormalizationMode ?: JVMConstructorCallNormalizationMode.DEFAULT - ) + + val constructorCallNormalizationMode = + JVMConstructorCallNormalizationMode.fromStringOrNull(arguments.constructorCallNormalizationMode) + if (constructorCallNormalizationMode != null) { + configuration.put( + JVMConfigurationKeys.CONSTRUCTOR_CALL_NORMALIZATION_MODE, + constructorCallNormalizationMode + ) + } val assertionsMode = JVMAssertionsMode.fromStringOrNull(arguments.assertionsMode) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/config/JVMConstructorCallNormalizationMode.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/config/JVMConstructorCallNormalizationMode.kt index cabb0f4bce6..d2b8b3176e8 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/config/JVMConstructorCallNormalizationMode.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/config/JVMConstructorCallNormalizationMode.kt @@ -17,9 +17,9 @@ package org.jetbrains.kotlin.config enum class JVMConstructorCallNormalizationMode( - val description: String, - val isEnabled: Boolean, - val shouldPreserveClassInitialization: Boolean + val description: String, + val isEnabled: Boolean, + val shouldPreserveClassInitialization: Boolean ) { DISABLE("disable", false, false), ENABLE("enable", true, false), @@ -27,10 +27,12 @@ enum class JVMConstructorCallNormalizationMode( ; companion object { - @JvmField - val DEFAULT = DISABLE + @JvmStatic + fun isSupportedValue(string: String?) = + string == null || values().any { it.description == string } @JvmStatic - fun fromStringOrNull(string: String?) = values().find { it.description == string } + fun fromStringOrNull(string: String?) = + values().find { it.description == string } } } \ No newline at end of file diff --git a/compiler/testData/codegen/box/constructorCall/breakInConstructorArguments.kt b/compiler/testData/codegen/box/constructorCall/breakInConstructorArguments.kt index 13e8ce2d951..37c69cf0334 100644 --- a/compiler/testData/codegen/box/constructorCall/breakInConstructorArguments.kt +++ b/compiler/testData/codegen/box/constructorCall/breakInConstructorArguments.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: -NormalizeConstructorCalls // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: test.kt diff --git a/compiler/testData/codegen/box/constructorCall/continueInConstructorArguments.kt b/compiler/testData/codegen/box/constructorCall/continueInConstructorArguments.kt index 52d81e4f22c..89dec1e45f8 100644 --- a/compiler/testData/codegen/box/constructorCall/continueInConstructorArguments.kt +++ b/compiler/testData/codegen/box/constructorCall/continueInConstructorArguments.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: -NormalizeConstructorCalls // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: test.kt diff --git a/compiler/testData/codegen/box/constructorCall/earlyReturnInConstructorArguments.kt b/compiler/testData/codegen/box/constructorCall/earlyReturnInConstructorArguments.kt index 3497418bfb0..a862c46bfc2 100644 --- a/compiler/testData/codegen/box/constructorCall/earlyReturnInConstructorArguments.kt +++ b/compiler/testData/codegen/box/constructorCall/earlyReturnInConstructorArguments.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: -NormalizeConstructorCalls // TARGET_BACKEND: JVM // WITH_RUNTIME inline fun ok(): String { diff --git a/compiler/testData/codegen/box/constructorCall/inlineFunInConstructorCallEvaluationOrder.kt b/compiler/testData/codegen/box/constructorCall/inlineFunInConstructorCallEvaluationOrder.kt index 6a07675236f..bc9984d4c0e 100644 --- a/compiler/testData/codegen/box/constructorCall/inlineFunInConstructorCallEvaluationOrder.kt +++ b/compiler/testData/codegen/box/constructorCall/inlineFunInConstructorCallEvaluationOrder.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: -NormalizeConstructorCalls // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: test.kt diff --git a/compiler/testData/codegen/box/constructorCall/inlineFunInInnerClassConstructorCall.kt b/compiler/testData/codegen/box/constructorCall/inlineFunInInnerClassConstructorCall.kt index 9292242e2b6..1edef67d6ad 100644 --- a/compiler/testData/codegen/box/constructorCall/inlineFunInInnerClassConstructorCall.kt +++ b/compiler/testData/codegen/box/constructorCall/inlineFunInInnerClassConstructorCall.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: -NormalizeConstructorCalls // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: test.kt diff --git a/compiler/testData/codegen/box/constructorCall/inlineFunInLocalClassConstructorCall.kt b/compiler/testData/codegen/box/constructorCall/inlineFunInLocalClassConstructorCall.kt index 0a7fba87b9e..b75d10dfab8 100644 --- a/compiler/testData/codegen/box/constructorCall/inlineFunInLocalClassConstructorCall.kt +++ b/compiler/testData/codegen/box/constructorCall/inlineFunInLocalClassConstructorCall.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: -NormalizeConstructorCalls // IGNORE_BACKEND: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/constructorCall/loopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13.kt b/compiler/testData/codegen/box/constructorCall/loopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13.kt new file mode 100644 index 00000000000..0ff06f56586 --- /dev/null +++ b/compiler/testData/codegen/box/constructorCall/loopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13.kt @@ -0,0 +1,15 @@ +// !LANGUAGE: +NormalizeConstructorCalls +// IGNORE_BACKEND: JVM_IR +// TARGET_BACKEND: JVM +// WITH_RUNTIME +open class A(val s: String) + +inline fun test(crossinline z: () -> String): String { + return object : A(listOf(1).map { it.toString() }.joinToString()) { + val value = z() + }.value +} + +fun box(): String { + return test { "OK" } +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/constructorCall/nonLocalReturnInConstructorArguments.kt b/compiler/testData/codegen/box/constructorCall/nonLocalReturnInConstructorArguments.kt index a70e2a8a4ec..a2c8602af59 100644 --- a/compiler/testData/codegen/box/constructorCall/nonLocalReturnInConstructorArguments.kt +++ b/compiler/testData/codegen/box/constructorCall/nonLocalReturnInConstructorArguments.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: -NormalizeConstructorCalls // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: test.kt diff --git a/compiler/testData/codegen/box/constructorCall/tryCatchInConstructorCallEvaluationOrder.kt b/compiler/testData/codegen/box/constructorCall/tryCatchInConstructorCallEvaluationOrder.kt index 4e3e586d5c4..5b346276e62 100644 --- a/compiler/testData/codegen/box/constructorCall/tryCatchInConstructorCallEvaluationOrder.kt +++ b/compiler/testData/codegen/box/constructorCall/tryCatchInConstructorCallEvaluationOrder.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: -NormalizeConstructorCalls // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: test.kt diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java index 344c10afdc4..31628b03c05 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java @@ -243,7 +243,7 @@ public abstract class CodegenTestCase extends KtUsefulTestCase { private static final Pattern BOOLEAN_FLAG_PATTERN = Pattern.compile("([+-])(([a-zA-Z_0-9]*)\\.)?([a-zA-Z_0-9]*)"); private static final Pattern CONSTRUCTOR_CALL_NORMALIZATION_MODE_FLAG_PATTERN = Pattern.compile( - "CONSTRUCTOR_CALL_NORMALIZATION_MODE=([a-zA-Z_0-9]*)"); + "CONSTRUCTOR_CALL_NORMALIZATION_MODE=([a-zA-Z_\\-0-9]*)"); private static final Pattern ASSERTIONS_MODE_FLAG_PATTERN = Pattern.compile("ASSERTIONS_MODE=([a-zA-Z_0-9-]*)"); private static void updateConfigurationWithFlags(@NotNull CompilerConfiguration configuration, @NotNull List flags) { diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index f6115c3d53e..3df792b7192 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -4215,6 +4215,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/constructorCall/loopInInlineFunInSuperConstructorCallWithEnabledNormalization.kt"); } + @TestMetadata("loopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13.kt") + public void testLoopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13() throws Exception { + runTest("compiler/testData/codegen/box/constructorCall/loopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13.kt"); + } + @TestMetadata("loopInInlineFunWithEnabledNormalization.kt") public void testLoopInInlineFunWithEnabledNormalization() throws Exception { runTest("compiler/testData/codegen/box/constructorCall/loopInInlineFunWithEnabledNormalization.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index b314011b890..90abef0e139 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -4215,6 +4215,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/constructorCall/loopInInlineFunInSuperConstructorCallWithEnabledNormalization.kt"); } + @TestMetadata("loopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13.kt") + public void testLoopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13() throws Exception { + runTest("compiler/testData/codegen/box/constructorCall/loopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13.kt"); + } + @TestMetadata("loopInInlineFunWithEnabledNormalization.kt") public void testLoopInInlineFunWithEnabledNormalization() throws Exception { runTest("compiler/testData/codegen/box/constructorCall/loopInInlineFunWithEnabledNormalization.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index cd2f59d6974..d9bf5259028 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -4215,6 +4215,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/constructorCall/loopInInlineFunInSuperConstructorCallWithEnabledNormalization.kt"); } + @TestMetadata("loopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13.kt") + public void testLoopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13() throws Exception { + runTest("compiler/testData/codegen/box/constructorCall/loopInInlineFunInSuperConstructorCallWithEnabledNormalizationSince13.kt"); + } + @TestMetadata("loopInInlineFunWithEnabledNormalization.kt") public void testLoopInInlineFunWithEnabledNormalization() throws Exception { runTest("compiler/testData/codegen/box/constructorCall/loopInInlineFunWithEnabledNormalization.kt"); diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 2d10fc58eb4..843ce85d1f2 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -82,6 +82,7 @@ enum class LanguageFeature( FunctionTypesWithBigArity(KOTLIN_1_3, sinceApiVersion = ApiVersion.KOTLIN_1_3), RestrictRetentionForExpressionAnnotations(KOTLIN_1_3, kind = BUG_FIX), NoConstantValueAttributeForNonConstVals(KOTLIN_1_3, kind = BUG_FIX), + NormalizeConstructorCalls(KOTLIN_1_3), RestrictReturnStatementTarget(KOTLIN_1_4, kind = BUG_FIX),