JS: add restrictions to external modifier according to KT-13893

This commit is contained in:
Alexey Andreev
2016-11-25 17:52:40 +03:00
parent c0c6992ea6
commit d0ffb5bd32
26 changed files with 298 additions and 290 deletions
@@ -33,9 +33,10 @@ import org.jetbrains.kotlin.types.DynamicTypesAllowed
object JsPlatformConfigurator : PlatformConfigurator(
DynamicTypesAllowed(),
additionalDeclarationCheckers = listOf(
NativeInvokeChecker(), NativeGetterChecker(), NativeSetterChecker(), NativeInnerClassChecker(),
NativeInvokeChecker(), NativeGetterChecker(), NativeSetterChecker(),
JsNameChecker, JsModuleChecker,
PlatformImplDeclarationChecker()
PlatformImplDeclarationChecker(),
JsExternalChecker()
),
additionalCallCheckers = listOf(ReifiedTypeParameterSubstitutionChecker(), JsModuleCallChecker),
additionalTypeCheckers = listOf(),
@@ -37,7 +37,7 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
put(ErrorsJs.NOT_SUPPORTED, "Cannot translate (not supported yet): ''{0}''", RenderFirstLineOfElementText)
put(ErrorsJs.REFERENCE_TO_BUILTIN_MEMBERS_NOT_SUPPORTED, "Callable references for builtin members are not supported yet: ''{0}''", RenderFirstLineOfElementText)
put(ErrorsJs.JSCODE_NO_JAVASCRIPT_PRODUCED, "Argument must be non-empty JavaScript code")
put(ErrorsJs.NATIVE_INNER_CLASS_PROHIBITED, "Native inner classes are prohibited")
put(ErrorsJs.NESTED_EXTERNAL_DECLARATION, "Non top-level `external` declaration")
put(ErrorsJs.JS_NAME_CLASH, "JavaScript name ({0}) generated for this declaration clashes with another declaration: {1}",
Renderers.STRING, Renderers.COMPACT)
put(ErrorsJs.JS_FAKE_NAME_CLASH, "JavaScript name {0} is generated for different inherited members: {1} and {2}",
@@ -41,7 +41,7 @@ public interface ErrorsJs {
DiagnosticFactory1<KtElement, KtElement> NOT_SUPPORTED = DiagnosticFactory1.create(ERROR, DEFAULT);
DiagnosticFactory1<KtElement, KtElement> REFERENCE_TO_BUILTIN_MEMBERS_NOT_SUPPORTED = DiagnosticFactory1.create(ERROR, DEFAULT);
DiagnosticFactory0<KtExpression> JSCODE_NO_JAVASCRIPT_PRODUCED = DiagnosticFactory0.create(ERROR, DEFAULT);
DiagnosticFactory0<KtExpression> NATIVE_INNER_CLASS_PROHIBITED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtExpression> NESTED_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory2<KtElement, String, DeclarationDescriptor> JS_NAME_CLASH = DiagnosticFactory2.create(
ERROR, DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory3<KtElement, String, DeclarationDescriptor, DeclarationDescriptor> JS_FAKE_NAME_CLASH =
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2016 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.js.resolve.diagnostics
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.js.PredefinedAnnotation
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.checkers.SimpleDeclarationChecker
class JsExternalChecker : SimpleDeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, diagnosticHolder: DiagnosticSink,
bindingContext: BindingContext) {
if (!AnnotationsUtils.isNativeObject(descriptor)) return
if (!DescriptorUtils.isTopLevelDeclaration(descriptor)) {
if (isDirectlyExternal(declaration, descriptor) && descriptor !is PropertyAccessorDescriptor) {
diagnosticHolder.report(ErrorsJs.NESTED_EXTERNAL_DECLARATION.on(declaration))
}
}
if (DescriptorUtils.isAnnotationClass(descriptor)) {
diagnosticHolder.report(Errors.WRONG_MODIFIER_TARGET.on(declaration, KtTokens.EXTERNAL_KEYWORD, "annotation class"))
}
else if (descriptor is PropertyAccessorDescriptor && isDirectlyExternal(declaration, descriptor)) {
diagnosticHolder.report(Errors.WRONG_MODIFIER_TARGET.on(declaration, KtTokens.EXTERNAL_KEYWORD, "property accessor"))
}
}
private fun isDirectlyExternal(declaration: KtDeclaration, descriptor: DeclarationDescriptor): Boolean {
if (declaration is KtProperty && descriptor is PropertyAccessorDescriptor) return false
return declaration.hasModifier(KtTokens.EXTERNAL_KEYWORD) ||
AnnotationsUtils.hasAnnotation(descriptor, PredefinedAnnotation.NATIVE)
}
}
@@ -1,37 +0,0 @@
/*
* Copyright 2010-2016 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.js.resolve.diagnostics
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.checkers.SimpleDeclarationChecker
import org.jetbrains.kotlin.resolve.DescriptorUtils
class NativeInnerClassChecker : SimpleDeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, diagnosticHolder: DiagnosticSink,
bindingContext: BindingContext) {
if (descriptor !is ClassDescriptor || !AnnotationsUtils.isNativeObject(descriptor)) return
if (descriptor.isInner && !AnnotationsUtils.isNativeObject(DescriptorUtils.getContainingClass(descriptor)!!)) {
diagnosticHolder.report(ErrorsJs.NATIVE_INNER_CLASS_PROHIBITED.on(declaration))
}
}
}
+1 -1
View File
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("WRONG_MODIFIER_TARGET")
package kotlin.js
import kotlin.annotation.AnnotationTarget.*
+7 -7
View File
@@ -20,28 +20,28 @@ import kotlin.annotation.AnnotationTarget.*
@Target(CLASS, FUNCTION, PROPERTY, CONSTRUCTOR, VALUE_PARAMETER, PROPERTY_GETTER, PROPERTY_SETTER)
@Deprecated("Use `external` modifier instead")
public external annotation class native(@Deprecated public val name: String = "")
public annotation class native(@Deprecated public val name: String = "")
@Target(FUNCTION)
@Deprecated("Use inline extension function with body using dynamic")
public external annotation class nativeGetter
public annotation class nativeGetter
@Target(FUNCTION)
@Deprecated("Use inline extension function with body using dynamic")
public external annotation class nativeSetter
public annotation class nativeSetter
@Target(FUNCTION)
@Deprecated("Use inline extension function with body using dynamic")
public external annotation class nativeInvoke
public annotation class nativeInvoke
@Target(CLASS, FUNCTION, PROPERTY)
internal external annotation class library(public val name: String = "")
internal annotation class library(public val name: String = "")
@Target(PROPERTY)
public external annotation class enumerable()
public annotation class enumerable()
@Target(CLASS)
internal external annotation class marker
internal annotation class marker
@Retention(AnnotationRetention.BINARY)
@Target(CLASS, FUNCTION, PROPERTY, CONSTRUCTOR, PROPERTY_GETTER, PROPERTY_SETTER)
+2 -2
View File
@@ -29,10 +29,10 @@ package kotlin
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
@Retention(AnnotationRetention.SOURCE)
public external annotation class Volatile
public annotation class Volatile
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(AnnotationRetention.SOURCE)
public external annotation class Synchronized
public annotation class Synchronized
public external inline fun <R> synchronized(lock: Any, crossinline block: () -> R): R = block()
+1 -1
View File
@@ -1,3 +1,3 @@
package org.junit
public external annotation class Test(val name: String = "")
public annotation class Test(val name: String = "")
+42 -18
View File
@@ -23,17 +23,22 @@ private external fun private_baz(a: String) {
public class PublicClass {
public fun public_baz(i: Int) {
}
public external fun public_baz(a: String) {
@JsName("public_baz")
public fun public_baz(a: String) {
}
internal fun internal_baz(i: Int) {
}
internal external fun internal_baz(a: String) {
@JsName("internal_baz")
internal fun internal_baz(a: String) {
}
private fun private_baz(i: Int) {
}
private external fun private_baz(a: String) {
@JsName("private_baz")
private fun private_baz(a: String) {
}
val call_private_baz = { private_baz(0) }
@@ -43,17 +48,23 @@ public class PublicClass {
internal class InternalClass {
public fun public_baz(i: Int) {
}
public external fun public_baz(a: String) {
@JsName("public_baz")
public fun public_baz(a: String) {
}
internal fun internal_baz(i: Int) {
}
internal external fun internal_baz(a: String) {
@JsName("internal_baz")
internal fun internal_baz(a: String) {
}
private fun private_baz(i: Int) {
}
private external fun private_baz(a: String) {
@JsName("private_baz")
private fun private_baz(a: String) {
}
val call_private_baz = { private_baz(0) }
@@ -63,17 +74,21 @@ internal class InternalClass {
private class PrivateClass {
public fun public_baz(i: Int) {
}
public external fun public_baz(a: String) {
@JsName("public_baz")
public fun public_baz(a: String) {
}
internal fun internal_baz(i: Int) {
}
internal external fun internal_baz(a: String) {
@JsName("internal_baz")
internal fun internal_baz(a: String) {
}
private fun private_baz(i: Int) {
}
private external fun private_baz(a: String) {
@JsName("private_baz")
private fun private_baz(a: String) {
}
val call_private_baz = { private_baz(0) }
@@ -83,17 +98,20 @@ private class PrivateClass {
open public class OpenPublicClass {
public fun public_baz(i: Int) {
}
public external fun public_baz(a: String) {
@JsName("public_baz")
public fun public_baz(a: String) {
}
internal fun internal_baz(i: Int) {
}
internal external fun internal_baz(a: String) {
@JsName("internal_baz")
internal fun internal_baz(a: String) {
}
private fun private_baz(i: Int) {
}
private external fun private_baz(a: String) {
@JsName("private_baz")
private fun private_baz(a: String) {
}
val call_private_baz = { private_baz(0) }
@@ -103,17 +121,20 @@ open public class OpenPublicClass {
internal open class OpenInternalClass {
public fun public_baz(i: Int) {
}
public external fun public_baz(a: String) {
@JsName("public_baz")
public fun public_baz(a: String) {
}
internal fun internal_baz(i: Int) {
}
internal external fun internal_baz(a: String) {
@JsName("internal_baz")
internal fun internal_baz(a: String) {
}
private fun private_baz(i: Int) {
}
private external fun private_baz(a: String) {
@JsName("private_baz")
private fun private_baz(a: String) {
}
val call_private_baz = { private_baz(0) }
@@ -123,17 +144,20 @@ internal open class OpenInternalClass {
open private class OpenPrivateClass {
public fun public_baz(i: Int) {
}
public external fun public_baz(a: String) {
@JsName("public_baz")
public fun public_baz(a: String) {
}
internal fun internal_baz(i: Int) {
}
internal external fun internal_baz(a: String) {
@JsName("internal_baz")
internal fun internal_baz(a: String) {
}
private fun private_baz(i: Int) {
}
private external fun private_baz(a: String) {
@JsName("private_baz")
private fun private_baz(a: String) {
}
val call_private_baz = { private_baz(0) }
+2 -2
View File
@@ -1,8 +1,8 @@
package foo
external class A(val c: Int) {
external companion object {
val g: Int = noImpl
companion object {
val g: Int
val c: String = noImpl
}
}