KJS: allow to use packages with names starting with "kotlin" only if the -Xallow-kotlin-package command line option is specified

#KT-14668 Fixed
This commit is contained in:
Zalim Bashorov
2017-01-27 04:14:00 +03:00
parent 7195e26ae2
commit 29ac01f2e2
16 changed files with 84 additions and 30 deletions
+2
View File
@@ -345,6 +345,7 @@
<arg value="-kjsm"/>
<arg line="-main noCall"/>
<arg line="-module-kind commonjs"/>
<arg value="-Xallow-kotlin-package"/>
</java>
</sequential>
</macrodef>
@@ -1121,6 +1122,7 @@
<arg line="-main noCall"/>
<arg line="-module-kind umd"/>
<arg value="-Xmulti-platform"/>
<arg value="-Xallow-kotlin-package"/>
<arg value="libraries/kotlin.test/common/src/main/kotlin"/>
<arg value="libraries/kotlin.test/js/src/main/kotlin"/>
</java>
@@ -63,6 +63,9 @@ public abstract class CommonCompilerArguments implements Serializable {
@ValueDescription("<count>")
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("<path>")
public String[] pluginClasspaths;
@@ -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;
@@ -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<KtFile>): 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
}
@@ -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<K2JSCompilerArguments> {
@@ -112,6 +113,10 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
Project project = environmentForJS.getProject();
List<KtFile> 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;
@@ -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<KtFile>): 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)
+1
View File
@@ -2,6 +2,7 @@ Usage: kotlinc-js <options> <source files>
where advanced options include:
-Xno-inline Disable method inlining
-Xrepeat <count> Repeat compilation (for performance analysis)
-Xallow-kotlin-package Allow compiling code in package 'kotlin'
-Xplugin <path> 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
+4
View File
@@ -0,0 +1,4 @@
$TESTDATA_DIR$/../kotlinPackage.kt
-no-stdlib
-output
$TEMP_DIR$/out.js
+4
View File
@@ -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
+1 -1
View File
@@ -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> 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 <count> Repeat compilation (for performance analysis)
-Xallow-kotlin-package Allow compiling code in package 'kotlin'
-Xplugin <path> 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
+1 -1
View File
@@ -1,3 +1,3 @@
$TESTDATA_DIR$/kotlinPackage.kt
$TESTDATA_DIR$/../kotlinPackage.kt
-d
$TEMP_DIR$
+1 -1
View File
@@ -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
@@ -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");
+1 -1
View File
@@ -34,7 +34,7 @@
<configuration>
<args>
<!--<arg>-Xallow-kotlin-package</arg>-->
<arg>-Xallow-kotlin-package</arg>
<arg>-Xmulti-platform</arg>
</args>
<moduleKind>umd</moduleKind>
+7
View File
@@ -79,6 +79,13 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<args>
<arg>-Xallow-kotlin-package</arg>
</args>
</configuration>
<executions>
<execution>
<id>js</id>