Implement new assert semantics in back-end

Previously, assert was just a regular function and its argument used to
be computed on each call (even if assertions are disabled on JVM).
This change adds support for 3 new behaviours of assert:
* always-enable (independently from -ea on JVM)
* always-disable (independently from -ea JVM)
* runtime/jvm (compile the calls like javac generates assert-operator)
* legacy (leave current eager semantics) - this already existed

Default behaviour is legacy for now.

The behavior is changed based on -Xassertions flag.
 #KT-7540: Fixed
This commit is contained in:
Ilmir Usmanov
2018-04-28 22:15:29 +03:00
parent 3f5a2c6427
commit f568149863
44 changed files with 2685 additions and 33 deletions
@@ -17,10 +17,7 @@
package org.jetbrains.kotlin.cli.common.arguments
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.AnalysisFlag
import org.jetbrains.kotlin.config.JVMConstructorCallNormalizationMode
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.*
class K2JVMCompilerArguments : CommonCompilerArguments() {
companion object {
@@ -124,6 +121,17 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
)
var constructorCallNormalizationMode: String? by FreezableVar(JVMConstructorCallNormalizationMode.DEFAULT.description)
@Argument(
value = "-Xassertions", valueDescription = "{always-enable|always-disable|jvm|legacy}",
description = "Assert calls behaviour\n" +
"-Xassertions=always-enable: enable, ignore jvm assertion settings;\n" +
"-Xassertions=always-disable: disable, ignore jvm assertion settings;\n" +
"-Xassertions=jvm: enable, depend on jvm assertion settings;\n" +
"-Xassertions=legacy: calculate condition on each call, check depends on jvm assertion settings in the kotlin package;\n" +
"default: legacy"
)
var assertionsMode: String? by FreezableVar(JVMAssertionsMode.DEFAULT.description)
@Argument(
value = "-Xbuild-file",
deprecatedName = "-module",
@@ -361,6 +361,20 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
constructorCallNormalizationMode ?: JVMConstructorCallNormalizationMode.DEFAULT
)
val assertionsMode =
JVMAssertionsMode.fromStringOrNull(arguments.assertionsMode)
if (assertionsMode == null) {
configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).report(
ERROR,
"Unknown assertions mode: ${arguments.assertionsMode}, " +
"supported modes: ${JVMAssertionsMode.values().map { it.description }}"
)
}
configuration.put(
JVMConfigurationKeys.ASSERTIONS_MODE,
assertionsMode ?: JVMAssertionsMode.DEFAULT
)
configuration.put(JVMConfigurationKeys.INHERIT_MULTIFILE_PARTS, arguments.inheritMultifileParts)
configuration.put(JVMConfigurationKeys.USE_TYPE_TABLE, arguments.useTypeTable)
configuration.put(JVMConfigurationKeys.SKIP_RUNTIME_VERSION_CHECK, arguments.skipRuntimeVersionCheck)