From c460593b7d2a83e79e855ff72cf9a14d98508550 Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Mon, 9 Jul 2018 22:02:33 +0300 Subject: [PATCH] Forbid coroutines in language version is 1.3 and api version is less --- .../calls/checkers/coroutineCallChecker.kt | 9 +++--- .../bytecodeListing/releaseCoroutines.kt | 1 + .../coroutines/release/coroutineContext.kt | 1 + .../languageVersionIsNotEqualToApiVersion.kt | 32 +++++++++++++++++++ .../coroutines/release/suspend.kt | 1 + .../kotlin/codegen/CodegenTestCase.java | 26 ++++++++++++--- .../DiagnosticsTestWithStdLibGenerated.java | 5 +++ ...ticsTestWithStdLibUsingJavacGenerated.java | 5 +++ 8 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithStdLib/coroutines/release/languageVersionIsNotEqualToApiVersion.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/coroutineCallChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/coroutineCallChecker.kt index f0ef2cfcb1f..8e688c91e52 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/coroutineCallChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/coroutineCallChecker.kt @@ -6,10 +6,7 @@ package org.jetbrains.kotlin.resolve.calls.checkers import com.intellij.psi.PsiElement -import org.jetbrains.kotlin.config.LanguageFeature -import org.jetbrains.kotlin.config.LanguageVersionSettings -import org.jetbrains.kotlin.config.isBuiltInCoroutineContext -import org.jetbrains.kotlin.config.restrictsSuspensionFqName +import org.jetbrains.kotlin.config.* import org.jetbrains.kotlin.coroutines.hasSuspendFunctionType import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.diagnostics.DiagnosticSink @@ -64,6 +61,10 @@ object CoroutineSuspendCallChecker : CallChecker { else -> return } + if (context.languageVersionSettings.supportsFeature(LanguageFeature.ReleaseCoroutines) && context.languageVersionSettings.apiVersion < ApiVersion.KOTLIN_1_3) { + context.trace.report(Errors.UNSUPPORTED.on(reportOn, "cannot use release coroutines API version less than 1.3")) + } + val enclosingSuspendFunction = context.scope .parentsWithSelf.firstOrNull { it is LexicalScope && it.kind in ALLOWED_SCOPE_KINDS && diff --git a/compiler/testData/codegen/bytecodeListing/releaseCoroutines.kt b/compiler/testData/codegen/bytecodeListing/releaseCoroutines.kt index 3a79c14a406..475a575d6a5 100644 --- a/compiler/testData/codegen/bytecodeListing/releaseCoroutines.kt +++ b/compiler/testData/codegen/bytecodeListing/releaseCoroutines.kt @@ -1,3 +1,4 @@ +// !API_VERSION: 1.3 // LANGUAGE_VERSION: 1.3 suspend fun named() {} diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/coroutineContext.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/coroutineContext.kt index 514f72e58b8..5463b65b80f 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/coroutineContext.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/coroutineContext.kt @@ -1,3 +1,4 @@ +// !API_VERSION: 1.3 // !LANGUAGE: +ReleaseCoroutines // SKIP_TXT diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/languageVersionIsNotEqualToApiVersion.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/languageVersionIsNotEqualToApiVersion.kt new file mode 100644 index 00000000000..e893e405100 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/languageVersionIsNotEqualToApiVersion.kt @@ -0,0 +1,32 @@ +// !API_VERSION: 1.2 +// !DIAGNOSTICS: -PRE_RELEASE_CLASS +// !LANGUAGE: +ReleaseCoroutines +// SKIP_TXT + +suspend fun dummy() {} + +suspend fun test1() { + kotlin.coroutines.coroutineContext + + kotlin.coroutines.experimental.coroutineContext + + suspend {}() + + dummy() + + val c: suspend () -> Unit = {} + c() +} + +fun test2() { + kotlin.coroutines.experimental.buildSequence { + yield(1) + } + kotlin.sequences.buildSequence { + yield(1) + } +} + +suspend fun test3(): Unit = kotlin.coroutines.experimental.suspendCoroutine { _ -> Unit } + +suspend fun test4(): Unit = kotlin.coroutines.suspendCoroutine { _ -> Unit } \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/suspend.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/suspend.kt index f9e661b90a9..6e3fc00c236 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/suspend.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/release/suspend.kt @@ -1,3 +1,4 @@ +// !API_VERSION: 1.3 // !LANGUAGE: +ReleaseCoroutines // SKIP_TXT 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..962a2731d82 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java @@ -61,10 +61,7 @@ import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -663,6 +660,19 @@ public abstract class CodegenTestCase extends KtUsefulTestCase { classpath.add(ForTestCompileRuntime.androidAnnotationsForTests()); } + ApiVersion av = ApiVersion.LATEST_STABLE; + boolean explicitApiVersion = false; + + for (TestFile tf : files) { + Map directives = KotlinTestUtils.parseDirectives(tf.content); + if (directives.containsKey(API_VERSION_DIRECTIVE)) { + assert !explicitApiVersion : "multiple API_VERSIONs"; + explicitApiVersion = true; + av = ApiVersion.Companion.parse(directives.get(API_VERSION_DIRECTIVE)); + assert av != null : "incorrect API_VERSION"; + } + } + CompilerConfiguration configuration = createConfiguration( configurationKind, getJdkKind(files), classpath, @@ -670,6 +680,14 @@ public abstract class CodegenTestCase extends KtUsefulTestCase { files ); + if (explicitApiVersion) { + CommonConfigurationKeysKt.setLanguageVersionSettings(configuration, new LanguageVersionSettingsImpl( + CommonConfigurationKeysKt.getLanguageVersionSettings(configuration).getLanguageVersion(), av, + new HashMap, Object>() {{ + put(AnalysisFlag.getExplicitApiVersion(), true); + }})); + } + myEnvironment = KotlinCoreEnvironment.createForTests( getTestRootDisposable(), configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES ); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index de0d999112c..53f87c5f7a6 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -1872,6 +1872,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/release/coroutineContext.kt"); } + @TestMetadata("languageVersionIsNotEqualToApiVersion.kt") + public void testLanguageVersionIsNotEqualToApiVersion() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/release/languageVersionIsNotEqualToApiVersion.kt"); + } + @TestMetadata("suspend.kt") public void testSuspend() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/release/suspend.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java index bbfb892bd6e..b5d62d1fde0 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java @@ -1872,6 +1872,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/release/coroutineContext.kt"); } + @TestMetadata("languageVersionIsNotEqualToApiVersion.kt") + public void testLanguageVersionIsNotEqualToApiVersion() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/release/languageVersionIsNotEqualToApiVersion.kt"); + } + @TestMetadata("suspend.kt") public void testSuspend() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/release/suspend.kt");