diff --git a/build.xml b/build.xml index 059f7c8af97..786bca41db7 100644 --- a/build.xml +++ b/build.xml @@ -345,6 +345,7 @@ + @@ -1121,6 +1122,7 @@ + diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java index 0cf6eff824f..4d022f9fbfb 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java @@ -63,6 +63,9 @@ public abstract class CommonCompilerArguments implements Serializable { @ValueDescription("") public String repeat; + @Argument(value = "Xallow-kotlin-package", description = "Allow compiling code in package 'kotlin'") + public boolean allowKotlinPackage; + @Argument(value = "Xplugin", description = "Load plugins from the given classpath") @ValueDescription("") public String[] pluginClasspaths; diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java index b15448ef959..69c5a995ba3 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java @@ -94,9 +94,6 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments { @Argument(value = "Xmultifile-parts-inherit", description = "Compile multifile classes as a hierarchy of parts and facade") public boolean inheritMultifileParts; - @Argument(value = "Xallow-kotlin-package", description = "Allow compiling code in package 'kotlin'") - public boolean allowKotlinPackage; - @Argument(value = "Xskip-metadata-version-check", description = "Load classes with bad metadata version anyway (incl. pre-release classes)") public boolean skipMetadataVersionCheck; diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/utils.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/common/utils.kt new file mode 100644 index 00000000000..a8a36483eea --- /dev/null +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/utils.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.cli.common + +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity +import org.jetbrains.kotlin.cli.common.messages.MessageCollector +import org.jetbrains.kotlin.cli.common.messages.MessageUtil +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.name.isSubpackageOf +import org.jetbrains.kotlin.psi.KtFile + +fun checkKotlinPackageUsage(environment: KotlinCoreEnvironment, files: Collection): Boolean { + if (environment.configuration.getBoolean(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE)) { + return true + } + val messageCollector = environment.configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE) + val kotlinPackage = FqName.topLevel(Name.identifier("kotlin")) + files.forEach { + if (it.packageFqName.isSubpackageOf(kotlinPackage)) { + messageCollector.report(CompilerMessageSeverity.ERROR, + "Only the Kotlin standard library is allowed to use the 'kotlin' package", + MessageUtil.psiElementToMessageLocation(it.packageDirective!!)) + return false + } + } + return true +} + diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java index 2478aff6063..25751bb660e 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java @@ -68,6 +68,7 @@ import java.util.Map; import static org.jetbrains.kotlin.cli.common.ExitCode.COMPILATION_ERROR; import static org.jetbrains.kotlin.cli.common.ExitCode.OK; +import static org.jetbrains.kotlin.cli.common.UtilsKt.checkKotlinPackageUsage; import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation.NO_LOCATION; public class K2JSCompiler extends CLICompiler { @@ -112,6 +113,10 @@ public class K2JSCompiler extends CLICompiler { Project project = environmentForJS.getProject(); List sourcesFiles = environmentForJS.getSourceFiles(); + environmentForJS.getConfiguration().put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage); + + if (!checkKotlinPackageUsage(environmentForJS, sourcesFiles)) return ExitCode.COMPILATION_ERROR; + if (arguments.outputFile == null) { messageCollector.report(CompilerMessageSeverity.ERROR, "Specify output file via -output", CompilerMessageLocation.NO_LOCATION); return ExitCode.COMPILATION_ERROR; diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt index 06a437fdfb6..db52f85d751 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt @@ -16,8 +16,8 @@ package org.jetbrains.kotlin.cli.jvm.compiler -import com.intellij.openapi.project.Project import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.project.Project import com.intellij.openapi.util.io.JarUtil import com.intellij.openapi.vfs.VfsUtilCore import com.intellij.openapi.vfs.VirtualFile @@ -25,16 +25,15 @@ import com.intellij.psi.PsiManager import com.intellij.psi.impl.PsiModificationTrackerImpl import com.intellij.psi.search.DelegatingGlobalSearchScope import com.intellij.psi.search.GlobalSearchScope -import org.jetbrains.annotations.TestOnly import org.jetbrains.kotlin.analyzer.AnalysisResult import org.jetbrains.kotlin.asJava.FilteredJvmDiagnostics import org.jetbrains.kotlin.backend.common.output.OutputFileCollection import org.jetbrains.kotlin.backend.common.output.SimpleOutputFileCollection import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.ExitCode +import org.jetbrains.kotlin.cli.common.checkKotlinPackageUsage import org.jetbrains.kotlin.cli.common.messages.* import org.jetbrains.kotlin.cli.common.output.outputUtils.writeAll -import org.jetbrains.kotlin.utils.tryConstructClassFromStringArgs import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler import org.jetbrains.kotlin.cli.jvm.config.* import org.jetbrains.kotlin.codegen.ClassBuilderFactories @@ -52,8 +51,6 @@ import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager import org.jetbrains.kotlin.modules.Module import org.jetbrains.kotlin.modules.TargetId import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.name.isSubpackageOf import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM @@ -61,6 +58,7 @@ import org.jetbrains.kotlin.util.PerformanceCounter import org.jetbrains.kotlin.utils.KotlinPaths import org.jetbrains.kotlin.utils.PathUtil import org.jetbrains.kotlin.utils.newLinkedHashMapWithExpectedSize +import org.jetbrains.kotlin.utils.tryConstructClassFromStringArgs import java.io.File import java.io.IOException import java.lang.reflect.InvocationTargetException @@ -154,7 +152,7 @@ object KotlinToJVMBytecodeCompiler { } try { - for ((module, state) in outputs) { + for ((_, state) in outputs) { ProgressIndicatorAndCompilationCanceledStatus.checkCanceled() writeOutput(state.configuration, state.factory, null) } @@ -445,23 +443,6 @@ object KotlinToJVMBytecodeCompiler { return generationState } - private fun checkKotlinPackageUsage(environment: KotlinCoreEnvironment, files: Collection): Boolean { - if (environment.configuration.getBoolean(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE)) { - return true - } - val messageCollector = environment.configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE) - val kotlinPackage = FqName.topLevel(Name.identifier("kotlin")) - files.forEach { - if (it.packageFqName.isSubpackageOf(kotlinPackage)) { - messageCollector.report(CompilerMessageSeverity.ERROR, - "Only the Kotlin standard library is allowed to use the 'kotlin' package", - MessageUtil.psiElementToMessageLocation(it.packageDirective!!)) - return false - } - } - return true - } - private val KotlinCoreEnvironment.messageCollector: MessageCollector get() = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) diff --git a/compiler/testData/cli/js/jsExtraHelp.out b/compiler/testData/cli/js/jsExtraHelp.out index c1b07ed5a62..0747d3bc3c2 100644 --- a/compiler/testData/cli/js/jsExtraHelp.out +++ b/compiler/testData/cli/js/jsExtraHelp.out @@ -2,6 +2,7 @@ Usage: kotlinc-js where advanced options include: -Xno-inline Disable method inlining -Xrepeat Repeat compilation (for performance analysis) + -Xallow-kotlin-package Allow compiling code in package 'kotlin' -Xplugin Load plugins from the given classpath -Xmulti-platform Enable experimental language support for multi-platform projects -Xno-check-impl Do not check presence of 'impl' modifier in multi-platform projects diff --git a/compiler/testData/cli/js/kotlinPackage.args b/compiler/testData/cli/js/kotlinPackage.args new file mode 100644 index 00000000000..24abc1a4d02 --- /dev/null +++ b/compiler/testData/cli/js/kotlinPackage.args @@ -0,0 +1,4 @@ +$TESTDATA_DIR$/../kotlinPackage.kt +-no-stdlib +-output +$TEMP_DIR$/out.js diff --git a/compiler/testData/cli/js/kotlinPackage.out b/compiler/testData/cli/js/kotlinPackage.out new file mode 100644 index 00000000000..8c98d2dfb61 --- /dev/null +++ b/compiler/testData/cli/js/kotlinPackage.out @@ -0,0 +1,4 @@ +compiler/testData/cli/kotlinPackage.kt:1:1: error: only the Kotlin standard library is allowed to use the 'kotlin' package +package kotlin.mylibrary +^ +COMPILATION_ERROR \ No newline at end of file diff --git a/compiler/testData/cli/jvm/extraHelp.out b/compiler/testData/cli/jvm/extraHelp.out index bb7a096058a..dca6fa9f4cc 100644 --- a/compiler/testData/cli/jvm/extraHelp.out +++ b/compiler/testData/cli/jvm/extraHelp.out @@ -5,7 +5,6 @@ where advanced options include: -Xno-optimize Disable optimizations -Xreport-perf Report detailed performance statistics -Xmultifile-parts-inherit Compile multifile classes as a hierarchy of parts and facade - -Xallow-kotlin-package Allow compiling code in package 'kotlin' -Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes) -Xskip-runtime-version-check Allow Kotlin runtime libraries of incompatible versions in the classpath -Xdump-declarations-to Path to JSON file to dump Java to Kotlin declaration mappings @@ -15,6 +14,7 @@ where advanced options include: Load definitions of built-in declarations from module dependencies, instead of from the compiler -Xno-inline Disable method inlining -Xrepeat Repeat compilation (for performance analysis) + -Xallow-kotlin-package Allow compiling code in package 'kotlin' -Xplugin Load plugins from the given classpath -Xmulti-platform Enable experimental language support for multi-platform projects -Xno-check-impl Do not check presence of 'impl' modifier in multi-platform projects diff --git a/compiler/testData/cli/jvm/kotlinPackage.args b/compiler/testData/cli/jvm/kotlinPackage.args index 61d00a6b529..306fba0cf08 100644 --- a/compiler/testData/cli/jvm/kotlinPackage.args +++ b/compiler/testData/cli/jvm/kotlinPackage.args @@ -1,3 +1,3 @@ -$TESTDATA_DIR$/kotlinPackage.kt +$TESTDATA_DIR$/../kotlinPackage.kt -d $TEMP_DIR$ diff --git a/compiler/testData/cli/jvm/kotlinPackage.out b/compiler/testData/cli/jvm/kotlinPackage.out index b2770332251..8c98d2dfb61 100644 --- a/compiler/testData/cli/jvm/kotlinPackage.out +++ b/compiler/testData/cli/jvm/kotlinPackage.out @@ -1,4 +1,4 @@ -compiler/testData/cli/jvm/kotlinPackage.kt:1:1: error: only the Kotlin standard library is allowed to use the 'kotlin' package +compiler/testData/cli/kotlinPackage.kt:1:1: error: only the Kotlin standard library is allowed to use the 'kotlin' package package kotlin.mylibrary ^ COMPILATION_ERROR \ No newline at end of file diff --git a/compiler/testData/cli/jvm/kotlinPackage.kt b/compiler/testData/cli/kotlinPackage.kt similarity index 100% rename from compiler/testData/cli/jvm/kotlinPackage.kt rename to compiler/testData/cli/kotlinPackage.kt diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index 88f6a642bcc..f0a55897740 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -443,6 +443,12 @@ public class CliTestGenerated extends AbstractCliTest { doJsTest(fileName); } + @TestMetadata("kotlinPackage.args") + public void testKotlinPackage() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/kotlinPackage.args"); + doJsTest(fileName); + } + @TestMetadata("languageVersion.args") public void testLanguageVersion() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/languageVersion.args"); diff --git a/libraries/kotlin.test/js/pom.xml b/libraries/kotlin.test/js/pom.xml index c4e568ea8f3..1fd84e2e349 100644 --- a/libraries/kotlin.test/js/pom.xml +++ b/libraries/kotlin.test/js/pom.xml @@ -34,7 +34,7 @@ - + -Xallow-kotlin-package -Xmulti-platform umd diff --git a/libraries/tools/kotlin-js-tests/pom.xml b/libraries/tools/kotlin-js-tests/pom.xml index ccfa57d6d86..d8cc326d8ca 100644 --- a/libraries/tools/kotlin-js-tests/pom.xml +++ b/libraries/tools/kotlin-js-tests/pom.xml @@ -79,6 +79,13 @@ org.jetbrains.kotlin kotlin-maven-plugin ${project.version} + + + + -Xallow-kotlin-package + + + js