Add flag to allow using kotlin.Result as a return type

#KT-26659 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2018-09-10 09:25:10 +03:00
parent 864e21dd1b
commit 169599abcc
14 changed files with 57 additions and 2 deletions
@@ -190,6 +190,12 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
)
var commonSources: Array<String>? by FreezableVar(null)
@Argument(
value = "-Xallow-result-return-type",
description = "Allow compiling code when `kotlin.Result` is used as a return type"
)
var allowResultReturnType: Boolean by FreezableVar(false)
open fun configureAnalysisFlags(collector: MessageCollector): MutableMap<AnalysisFlag<*>, Any> {
return HashMap<AnalysisFlag<*>, Any>().apply {
put(AnalysisFlag.skipMetadataVersionCheck, skipMetadataVersionCheck)
@@ -198,6 +204,7 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
put(AnalysisFlag.experimental, experimental?.toList().orEmpty())
put(AnalysisFlag.useExperimental, useExperimental?.toList().orEmpty())
put(AnalysisFlag.explicitApiVersion, apiVersion != null)
put(AnalysisFlag.allowResultReturnType, allowResultReturnType)
}
}
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.resolve.checkers
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.AnalysisFlag
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce
@@ -19,6 +20,8 @@ class ResultClassInReturnTypeChecker : DeclarationChecker {
}
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (context.languageVersionSettings.getFlag(AnalysisFlag.allowResultReturnType)) return
if (declaration !is KtCallableDeclaration || descriptor !is CallableMemberDescriptor) return
val returnType = descriptor.returnType ?: return
+1
View File
@@ -4,6 +4,7 @@ where advanced options include:
-Xfriend-modules-disabled Disable internal declaration export
-Xtyped-arrays Translate primitive arrays to JS typed arrays
-Xallow-kotlin-package Allow compiling code in package 'kotlin' and allow not requiring kotlin.stdlib in module-info
-Xallow-result-return-type Allow compiling code when `kotlin.Result` is used as a return type
-Xcommon-sources=<path> Sources of the common module that need to be compiled together with this module in the multi-platform mode.
Should be a subset of sources passed as free arguments
-Xcoroutines={enable|warn|error}
+1
View File
@@ -62,6 +62,7 @@ where advanced options include:
-Xuse-old-class-files-reading Use old class files reading implementation (may slow down the build and should be used in case of problems with the new implementation)
-Xuse-type-table Use type table in metadata serialization
-Xallow-kotlin-package Allow compiling code in package 'kotlin' and allow not requiring kotlin.stdlib in module-info
-Xallow-result-return-type Allow compiling code when `kotlin.Result` is used as a return type
-Xcommon-sources=<path> Sources of the common module that need to be compiled together with this module in the multi-platform mode.
Should be a subset of sources passed as free arguments
-Xcoroutines={enable|warn|error}
@@ -0,0 +1,4 @@
$TESTDATA_DIR$/flagAllowingResultAsReturnType.kt
-Xallow-result-return-type
-d
$TEMP_DIR$
@@ -0,0 +1 @@
fun foo(): Result<Int> = TODO()
@@ -0,0 +1 @@
OK
@@ -0,0 +1,8 @@
// !ALLOW_RESULT_RETURN_TYPE
fun result(): Result<Int> = TODO()
val resultP: Result<Int> = result()
fun f(r1: Result<Int>?) {
r1 <!RESULT_CLASS_WITH_NULLABLE_OPERATOR!>?:<!> <!UNUSED_EXPRESSION!>0<!>
}
@@ -0,0 +1,5 @@
package
public val resultP: kotlin.Result<kotlin.Int>
public fun f(/*0*/ r1: kotlin.Result<kotlin.Int>?): kotlin.Unit
public fun result(): kotlin.Result<kotlin.Int>
@@ -21,6 +21,7 @@ const val USE_EXPERIMENTAL_DIRECTIVE = "USE_EXPERIMENTAL"
const val IGNORE_DATA_FLOW_IN_ASSERT_DIRECTIVE = "IGNORE_DATA_FLOW_IN_ASSERT"
const val JVM_DEFAULT_MODE = "JVM_DEFAULT_MODE"
const val SKIP_METADATA_VERSION_CHECK = "SKIP_METADATA_VERSION_CHECK"
const val ALLOW_RESULT_RETURN_TYPE = "ALLOW_RESULT_RETURN_TYPE"
data class CompilerTestLanguageVersionSettings(
private val initialLanguageFeatures: Map<LanguageFeature, LanguageFeature.State>,
@@ -58,8 +59,13 @@ fun parseLanguageVersionSettings(directiveMap: Map<String, String>): CompilerTes
val ignoreDataFlowInAssert = AnalysisFlag.ignoreDataFlowInAssert to directiveMap.containsKey(IGNORE_DATA_FLOW_IN_ASSERT_DIRECTIVE)
val enableJvmDefault = directiveMap[JVM_DEFAULT_MODE]?.let { AnalysisFlag.jvmDefaultMode to JvmDefaultMode.fromStringOrNull(it)!! }
val skipMetadataVersionCheck = AnalysisFlag.skipMetadataVersionCheck to directiveMap.containsKey(SKIP_METADATA_VERSION_CHECK)
val allowResultReturnType = AnalysisFlag.allowResultReturnType to directiveMap.containsKey(ALLOW_RESULT_RETURN_TYPE)
if (apiVersionString == null && languageFeaturesString == null && experimental == null && useExperimental == null && !ignoreDataFlowInAssert.second) return null
if (apiVersionString == null && languageFeaturesString == null && experimental == null &&
useExperimental == null && !ignoreDataFlowInAssert.second && !allowResultReturnType.second
) {
return null
}
val apiVersion = (if (apiVersionString != null) ApiVersion.parse(apiVersionString) else ApiVersion.LATEST_STABLE)
?: error("Unknown API version: $apiVersionString")
@@ -72,7 +78,7 @@ fun parseLanguageVersionSettings(directiveMap: Map<String, String>): CompilerTes
languageFeatures, apiVersion, languageVersion,
mapOf(
*listOfNotNull(
experimental, useExperimental, enableJvmDefault, ignoreDataFlowInAssert, skipMetadataVersionCheck
experimental, useExperimental, enableJvmDefault, ignoreDataFlowInAssert, skipMetadataVersionCheck, allowResultReturnType
).toTypedArray()
)
)
@@ -1433,6 +1433,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/coroutines"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("allowResultInReturnTypeWithFlag.kt")
public void testAllowResultInReturnTypeWithFlag() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowResultInReturnTypeWithFlag.kt");
}
@TestMetadata("callableReferences.kt")
public void testCallableReferences() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/callableReferences.kt");
@@ -1433,6 +1433,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/coroutines"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("allowResultInReturnTypeWithFlag.kt")
public void testAllowResultInReturnTypeWithFlag() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowResultInReturnTypeWithFlag.kt");
}
@TestMetadata("callableReferences.kt")
public void testCallableReferences() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/callableReferences.kt");
@@ -251,6 +251,11 @@ public class CliTestGenerated extends AbstractCliTest {
runTest("compiler/testData/cli/jvm/fileClassClashMultipleFiles.args");
}
@TestMetadata("flagAllowingResultAsReturnType.args")
public void testFlagAllowingResultAsReturnType() throws Exception {
runTest("compiler/testData/cli/jvm/flagAllowingResultAsReturnType.args");
}
@TestMetadata("help.args")
public void testHelp() throws Exception {
runTest("compiler/testData/cli/jvm/help.args");
@@ -71,5 +71,8 @@ class AnalysisFlag<out T> internal constructor(
@JvmStatic
val jvmDefaultMode by Flag.JvmDefaultModeDisabledByDefaul
@JvmStatic
val allowResultReturnType by Flag.Boolean
}
}