Skip to content

Commit c28b701

Browse files
committed
JS: ignore annotation on expression when its retention is SOURCE
#KT-8258
1 parent 90927cf commit c28b701

File tree

9 files changed

+135
-32
lines changed

9 files changed

+135
-32
lines changed

compiler/testData/cli/js/diagnosticForUnhandledElements.args

Lines changed: 0 additions & 4 deletions
This file was deleted.

compiler/testData/cli/js/diagnosticForUnhandledElements.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

compiler/testData/cli/js/diagnosticForUnhandledElements.out

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// !DIAGNOSTICS: -UNUSED_PARAMETER
2+
3+
package foo
4+
5+
@Retention(AnnotationRetention.SOURCE)
6+
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION)
7+
annotation class AnnotationWithSourceRetention
8+
9+
@Retention(AnnotationRetention.BINARY)
10+
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION)
11+
annotation class AnnotationWithBinaryRetention
12+
13+
@Retention(AnnotationRetention.RUNTIME)
14+
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION)
15+
annotation class AnnotationWithRuntimeRetention
16+
17+
@AnnotationWithSourceRetention
18+
class TestSource {
19+
@AnnotationWithSourceRetention
20+
fun baz(@AnnotationWithSourceRetention foo : Int) : Int {
21+
return (@AnnotationWithSourceRetention 1)
22+
}
23+
}
24+
25+
@AnnotationWithBinaryRetention
26+
class TestBinary {
27+
@AnnotationWithBinaryRetention
28+
fun baz(@AnnotationWithBinaryRetention foo : Int) : Int {
29+
return (<!NOT_SUPPORTED!>@AnnotationWithBinaryRetention 1<!>)
30+
}
31+
}
32+
33+
@AnnotationWithRuntimeRetention
34+
class TestRuntime {
35+
@AnnotationWithRuntimeRetention
36+
fun baz(@AnnotationWithRuntimeRetention foo : Int) : Int {
37+
return (<!NOT_SUPPORTED!>@AnnotationWithRuntimeRetention 1<!>)
38+
}
39+
}
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package
2+
3+
package foo {
4+
5+
@kotlin.annotation.Retention(value = AnnotationRetention.BINARY) @kotlin.annotation.Target(allowedTargets = {AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION}) public final annotation class AnnotationWithBinaryRetention : kotlin.Annotation {
6+
public constructor AnnotationWithBinaryRetention()
7+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
8+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
9+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
10+
}
11+
12+
@kotlin.annotation.Retention(value = AnnotationRetention.RUNTIME) @kotlin.annotation.Target(allowedTargets = {AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION}) public final annotation class AnnotationWithRuntimeRetention : kotlin.Annotation {
13+
public constructor AnnotationWithRuntimeRetention()
14+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
15+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
16+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
17+
}
18+
19+
@kotlin.annotation.Retention(value = AnnotationRetention.SOURCE) @kotlin.annotation.Target(allowedTargets = {AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION}) public final annotation class AnnotationWithSourceRetention : kotlin.Annotation {
20+
public constructor AnnotationWithSourceRetention()
21+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
22+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
23+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
24+
}
25+
26+
@foo.AnnotationWithBinaryRetention() public final class TestBinary {
27+
public constructor TestBinary()
28+
@foo.AnnotationWithBinaryRetention() public final fun baz(/*0*/ @foo.AnnotationWithBinaryRetention() foo: kotlin.Int): kotlin.Int
29+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
30+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
31+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
32+
}
33+
34+
@foo.AnnotationWithRuntimeRetention() public final class TestRuntime {
35+
public constructor TestRuntime()
36+
@foo.AnnotationWithRuntimeRetention() public final fun baz(/*0*/ @foo.AnnotationWithRuntimeRetention() foo: kotlin.Int): kotlin.Int
37+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
38+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
39+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
40+
}
41+
42+
@foo.AnnotationWithSourceRetention() public final class TestSource {
43+
public constructor TestSource()
44+
@foo.AnnotationWithSourceRetention() public final fun baz(/*0*/ @foo.AnnotationWithSourceRetention() foo: kotlin.Int): kotlin.Int
45+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
46+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
47+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
48+
}
49+
}

compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibAndBackendCompilationGenerated.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 JetBrains s.r.o.
2+
* Copyright 2010-2016 JetBrains s.r.o.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,4 +67,19 @@ public void testRecursionCycleWithStdlibCall() throws Exception {
6767
doTest(fileName);
6868
}
6969
}
70+
71+
@TestMetadata("compiler/testData/diagnostics/testsWithJsStdLibAndBackendCompilation/unsupportedFeatures")
72+
@TestDataPath("$PROJECT_ROOT")
73+
@RunWith(JUnit3RunnerWithInners.class)
74+
public static class UnsupportedFeatures extends AbstractDiagnosticsTestWithJsStdLibAndBackendCompilation {
75+
public void testAllFilesPresentInUnsupportedFeatures() throws Exception {
76+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithJsStdLibAndBackendCompilation/unsupportedFeatures"), Pattern.compile("^(.+)\\.kt$"), true);
77+
}
78+
79+
@TestMetadata("annotations.kt")
80+
public void testAnnotations() throws Exception {
81+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLibAndBackendCompilation/unsupportedFeatures/annotations.kt");
82+
doTest(fileName);
83+
}
84+
}
7085
}

compiler/tests/org/jetbrains/kotlin/cli/KotlincExecutableTestGenerated.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 JetBrains s.r.o.
2+
* Copyright 2010-2016 JetBrains s.r.o.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -304,12 +304,6 @@ public void testDiagnosticForClassLiteral() throws Exception {
304304
doJsTest(fileName);
305305
}
306306

307-
@TestMetadata("diagnosticForUnhandledElements.args")
308-
public void testDiagnosticForUnhandledElements() throws Exception {
309-
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/diagnosticForUnhandledElements.args");
310-
doJsTest(fileName);
311-
}
312-
313307
@TestMetadata("diagnosticWhenReferenceToBuiltinsMember.args")
314308
public void testDiagnosticWhenReferenceToBuiltinsMember() throws Exception {
315309
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/diagnosticWhenReferenceToBuiltinsMember.args");

js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/ExpressionVisitor.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 JetBrains s.r.o.
2+
* Copyright 2010-2016 JetBrains s.r.o.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,9 +21,12 @@
2121
import com.intellij.psi.util.PsiTreeUtil;
2222
import org.jetbrains.annotations.NotNull;
2323
import org.jetbrains.annotations.Nullable;
24+
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
2425
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
2526
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
2627
import org.jetbrains.kotlin.descriptors.VariableDescriptor;
28+
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
29+
import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention;
2730
import org.jetbrains.kotlin.js.translate.context.Namer;
2831
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
2932
import org.jetbrains.kotlin.js.translate.declaration.ClassTranslator;
@@ -44,6 +47,7 @@
4447
import org.jetbrains.kotlin.resolve.constants.ConstantValue;
4548
import org.jetbrains.kotlin.resolve.constants.NullValue;
4649
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
50+
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
4751
import org.jetbrains.kotlin.resolve.inline.InlineUtil;
4852
import org.jetbrains.kotlin.types.KotlinType;
4953
import org.jetbrains.kotlin.types.TypeUtils;
@@ -496,4 +500,26 @@ public JsNode visitObjectDeclaration(@NotNull KtObjectDeclaration expression,
496500
JsExpression value = ClassTranslator.generateClassCreation(expression, context);
497501
return newVar(name, value).source(expression);
498502
}
503+
504+
@Override
505+
public JsNode visitAnnotatedExpression(@NotNull KtAnnotatedExpression expression, TranslationContext context) {
506+
for (KtAnnotationEntry entry : expression.getAnnotationEntries()) {
507+
AnnotationDescriptor descriptor = context.bindingContext().get(BindingContext.ANNOTATION, entry);
508+
if (descriptor == null) continue;
509+
510+
ClassifierDescriptor classifierDescriptor = descriptor.getType().getConstructor().getDeclarationDescriptor();
511+
if (classifierDescriptor == null) continue;
512+
513+
KotlinRetention retention = DescriptorUtilsKt.getAnnotationRetention(classifierDescriptor);
514+
515+
if (retention == KotlinRetention.SOURCE) {
516+
KtExpression baseExpression = expression.getBaseExpression();
517+
if (baseExpression == null) continue;
518+
519+
return baseExpression.accept(this, context);
520+
}
521+
}
522+
523+
return super.visitAnnotatedExpression(expression, context);
524+
}
499525
}

js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 JetBrains s.r.o.
2+
* Copyright 2010-2016 JetBrains s.r.o.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -103,9 +103,8 @@ public static JsNode translateExpression(@NotNull KtExpression expression, @NotN
103103
return result;
104104
}
105105

106-
//NOTE: use with care
107106
@NotNull
108-
public static JsNode doTranslateExpression(KtExpression expression, TranslationContext context) {
107+
private static JsNode doTranslateExpression(KtExpression expression, TranslationContext context) {
109108
return expression.accept(new ExpressionVisitor(), context);
110109
}
111110

0 commit comments

Comments
 (0)