JS: prohibit runtime annotations on external declarations. Warn about runtime annotation in remaining cases. See KT-13895

This commit is contained in:
Alexey Andreev
2016-12-12 19:05:55 +03:00
parent dbe8edda5f
commit 9dc16f0564
8 changed files with 184 additions and 3 deletions
@@ -0,0 +1,59 @@
@Retention(AnnotationRetention.BINARY)
annotation class X
@Retention(AnnotationRetention.RUNTIME)
annotation class Y
@X
external class A {
@X
fun f()
@X
val p: Int
@get:X
val r: Int
}
<!RUNTIME_ANNOTATION_ON_EXTERNAL_DECLARATION!>@Y<!>
external class B {
<!RUNTIME_ANNOTATION_ON_EXTERNAL_DECLARATION!>@Y<!>
fun f()
<!RUNTIME_ANNOTATION_ON_EXTERNAL_DECLARATION!>@Y<!>
val p: Int
<!RUNTIME_ANNOTATION_ON_EXTERNAL_DECLARATION!>@get:Y<!>
val r: Int
}
@X
class C {
@X
fun f() {}
@X
val p: Int = 0
val q: Int
@X get() = 0
@get:X
val r: Int = 0
}
<!RUNTIME_ANNOTATION_NOT_SUPPORTED!>@Y<!>
class D {
<!RUNTIME_ANNOTATION_NOT_SUPPORTED!>@Y<!>
fun f() {}
<!RUNTIME_ANNOTATION_NOT_SUPPORTED!>@Y<!>
val p: Int = 0
val q: Int
<!RUNTIME_ANNOTATION_NOT_SUPPORTED!>@Y<!> get() = 0
<!RUNTIME_ANNOTATION_NOT_SUPPORTED!>@get:Y<!>
val r: Int = 0
}
@@ -0,0 +1,57 @@
package
@X public external final class A {
public constructor A()
@X public final val p: kotlin.Int
public final val r: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@X public final fun f(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@Y public external final class B {
public constructor B()
@Y public final val p: kotlin.Int
public final val r: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@Y public final fun f(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@X public final class C {
public constructor C()
@X public final val p: kotlin.Int = 0
public final val q: kotlin.Int
public final val r: kotlin.Int = 0
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@X public final fun f(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@Y public final class D {
public constructor D()
@Y public final val p: kotlin.Int = 0
public final val q: kotlin.Int
public final val r: kotlin.Int = 0
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@Y public final fun f(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@kotlin.annotation.Retention(value = AnnotationRetention.BINARY) public final annotation class X : kotlin.Annotation {
public constructor X()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@kotlin.annotation.Retention(value = AnnotationRetention.RUNTIME) public final annotation class Y : kotlin.Annotation {
public constructor Y()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -30,9 +30,9 @@ class TestBinary {
}
}
@AnnotationWithRuntimeRetention
<!RUNTIME_ANNOTATION_NOT_SUPPORTED!>@AnnotationWithRuntimeRetention<!>
class TestRuntime {
@AnnotationWithRuntimeRetention
<!RUNTIME_ANNOTATION_NOT_SUPPORTED!>@AnnotationWithRuntimeRetention<!>
fun baz(@AnnotationWithRuntimeRetention foo : Int) : Int {
return (<!NOT_SUPPORTED!>@AnnotationWithRuntimeRetention 1<!>)
}
@@ -36,6 +36,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithJsStdLib"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("runtimeAnnotations.kt")
public void testRuntimeAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/runtimeAnnotations.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -36,7 +36,8 @@ object JsPlatformConfigurator : PlatformConfigurator(
NativeInvokeChecker(), NativeGetterChecker(), NativeSetterChecker(),
JsNameChecker, JsModuleChecker,
PlatformImplDeclarationChecker(),
JsExternalChecker()
JsExternalChecker(),
JsRuntimeAnnotationChecker
),
additionalCallCheckers = listOf(ReifiedTypeParameterSubstitutionChecker(), JsModuleCallChecker, JsDynamicCallChecker),
additionalTypeCheckers = listOf(),
@@ -65,6 +65,9 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
put(ErrorsJs.NATIVE_INTERFACE_AS_REIFIED_TYPE_ARGUMENT, "Cannot pass native interface {0} for reified type parameter", RENDER_TYPE)
put(ErrorsJs.EXTERNAL_TYPE_EXTENDS_NON_EXTERNAL_TYPE, "External type extends non-external type")
put(ErrorsJs.WRONG_OPERATION_WITH_DYNAMIC, "Wrong operation with dynamic value: {0}", Renderers.STRING)
put(ErrorsJs.RUNTIME_ANNOTATION_ON_EXTERNAL_DECLARATION, "Runtime annotation can't be put on external declaration")
put(ErrorsJs.RUNTIME_ANNOTATION_NOT_SUPPORTED, "Reflection is not supported in JavaScript target, therefore you won't be able " +
"to read this annotation in run-time")
this
}
@@ -65,6 +65,9 @@ public interface ErrorsJs {
DiagnosticFactory0<KtElement> EXTERNAL_TYPE_EXTENDS_NON_EXTERNAL_TYPE = DiagnosticFactory0.create(
ERROR, PositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory1<PsiElement, String> WRONG_OPERATION_WITH_DYNAMIC = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> RUNTIME_ANNOTATION_ON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(
ERROR, DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory0<PsiElement> RUNTIME_ANNOTATION_NOT_SUPPORTED = DiagnosticFactory0.create(WARNING, DECLARATION_SIGNATURE_OR_DEFAULT);
@SuppressWarnings("UnusedDeclaration")
Object _initializer = new Object() {
@@ -0,0 +1,52 @@
/*
* 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.descriptors.MemberDescriptor
import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.checkers.SimpleDeclarationChecker
import org.jetbrains.kotlin.resolve.descriptorUtil.getAnnotationRetention
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
object JsRuntimeAnnotationChecker : SimpleDeclarationChecker {
override fun check(
declaration: KtDeclaration,
descriptor: DeclarationDescriptor,
diagnosticHolder: DiagnosticSink,
bindingContext: BindingContext
) {
for ((annotationDescriptor, _) in descriptor.annotations.getAllAnnotations()) {
val annotationClass = annotationDescriptor.type.constructor.declarationDescriptor as? ClassDescriptor ?: continue
if (annotationClass.getAnnotationRetention() != KotlinRetention.RUNTIME) continue
val annotationPsi = (annotationDescriptor.source as? PsiSourceElement)?.psi ?: declaration
if (descriptor is MemberDescriptor && DescriptorUtils.isEffectivelyExternal(descriptor)) {
diagnosticHolder.report(ErrorsJs.RUNTIME_ANNOTATION_ON_EXTERNAL_DECLARATION.on(annotationPsi))
}
else {
diagnosticHolder.report(ErrorsJs.RUNTIME_ANNOTATION_NOT_SUPPORTED.on(annotationPsi))
}
}
}
}